We're live-coding on YouTube! Join us!

Trying the New WSL 2. It's Fast! (Windows Subsystem for Linux)

Trying the New WSL 2. It's Fast! (Windows Subsystem for Linux)

Windows Subsystem for Linux is the tool that was released by Microsoft to get a full UNIX system inside of Windows. Microsoft has put in some good initiatives for developers after purchasing GitHub, making VS Code, open sourcing .NET Core, and more.

WSL 2 was recently announced as available as part of the Fast Ring of Windows updates. I am normally not into trying out the Windows Fast Ring of updates, but WSL 2 is a very exciting prospect. I wanted to see how much faster my development would be vs WSL 1.

TLDR; WSL is fast for normal everyday web development tasks

I haven't done any exhaustive, scientific, or precise tests by any means. What I have found though is that WSL is about *5 times faster for everyday web development tasks like npm or yarn. *

Also, hot reloading and working with create-react-app or the vue-cli is faster!

Read on for installation, getting started, gotchas, and specific timing on npx create-react-app

What does WSL 2 bring?

I don't mind installing WSL 2 on my laptop, but I wouldn't do this on my main dev machine (desktop) just yet. Maybe I will when WSL 2 comes to the Slow branch of updates.

We'll be going into a walkthrough of installing and using WSL 2. For the official instructions, check Microsoft's site.

Essential Reading: Learn React from Scratch! (2019 Edition)

This is the process I went through and the curve balls that I learned while installing.

We'll be using the new Windows Terminal which was recently released. We recently did a walkthrough of playing around with the Terminal.

Overall Process

Here's a quick rundown:

  1. Make sure we have all the requirements
  2. Enable WSL 2
  3. Install a Linux distro like Ubuntu
  4. Profit!

Requirements

There are only a few requirements for installing WSL 2. We need a Windows 10 version 18917 or greater. This means that currently we need to be on the Fast Ring.

  • Windows version XYZ or greater (Fast Ring)
  • WSL installed already
  • Hyper-V capable computer

To get on the Fast Ring

The Fast Ring of updates is not the safest place to be. Windows Fast Ring updates can happen once a week and may not be the most stable. This is your warning! If this is your main dev machine, it may not be the best option to update your machine to Fast Ring.

To get on the Fast Ring, go into Settings > Update > Windows Insider Program. You can also search for Windows Insider Program from the Start screen.

Enabling WSL

We'll need to have WSL enabled. We can do this by going into Turn Windows Features on and off from the Start screen.

Once we have that turned on, follow the instructions and do any restarts if you have to.

Installing WSL

Now that we have all the requirements out of the way, let's install WSL 2!

Open up Powershell with admin privileges. Run the installation command:

Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

In the future I'm hoping these will be able to get installed without the command line tools. The overall process will get streamlined as we get closer to the official release in 2020.

Go ahead and restart your computer if asked. The next step is to pick a Linux distro!

Picking a Linux Distro

The Linux distro we pick will be the Linux distro that is installed and the one that we use through WSL. We'll be using Ubuntu since that is one of (if not the most) popular. By installing Ubuntu, we'll be able to go into our command line and run all the Linux commands we know like:

cd
mkdir
rm -rf
ls -al

Go into the Windows Store and pick the distro you want:

We'll be installing Ubuntu.

Checking installed distros

To see which distros we have installed, we can run:

wsl -l

Now we'll set the distro to Ubuntu since that's what I have installed on my computer:

wsl --set-version Ubuntu 2

Setting WSL 2 as the Default

To set the default to WSL 2 whenever you install a distro, run the following command:

wsl --set-default-version 2

This means that whenever you go into the Windows Store to install a new distro, the WSL 2 version will be installed.

Checking if the Install Worked

The following command will show us what distros we have installed and what version they are on:

wsl --list --verbose

# or shorthand
wsl -l -v

Installation Failure

Warning! If you already had Ubuntu installed on WSL 1, you may have to completely uninstall and reinstall it.

If your computer is having trouble running the update command (like my computer), then you may want to completely uninstall and reinstall your distro.

I went into the Windows settings under Apps > Apps and Features and uninstalled Ubuntu:

Then I made sure that the default version I wanted for all new installs was version 2:

wsl --set-default-version 2

Now we can go into the Windows Store to install it:

Once we're installed, then we can check to be sure that version 2 was installed. Open up Powershell and run:

wsl -l -v

Running WSL

You can open up Windows Terminal and use the Ubuntu dropdown:

Alternatively you can open up either cmd or Powershell and run:

# open up windows subsystem for linux
wsl

Gotchas with WSL 2

Speed when transferring between file systems is a little slow right now. That's not a worry for me since I never transferred. I worked solely inside of my Windows files which were mounted into the Linux system.

Here's a guide on User Experience changes from WSL 1 to WSL 2. The two big gotchas are:

  1. Move your files into the Linux file system instead of your Windows system
  2. You will need to access localhost apps like [localhost:3000](http://localhost:3000) via an IP address like 192.168.28.2:3000

Move all files into Linux

To take advantage of all the new speed improvements in WSL 2, our files will need to be moved into the Linux filesystem.

The best way to figure out where to move your files is to find the home directory in Linux, then open it in Windows explorer. Run the following:

# find the home folder
cd ~

# open up windows explorer for this folder
explorer.exe .

Notice the path as a Network path:

We can now move our files from our Windows folders into this new networked folder. We'll be able to access it from our WSL and interact with it as if it were still in the same Windows files.

For instance, we can install Node using a tool like n and then run npm start to run a local server for let's say a React app.

Network Isn't localhost Yet

When we create an app using create-react-app or the vue-cli, we will usually run a command like yarn start or yarn serve to run a local server.

Normally we would be able to check on our application right in our browser using [localhost:3000](http://localhost:3000) for React or [localhost:8080](http://localhost:8080) for Vue.

In the early stages of WSL 2, we can't use localhost. We need to use an IP since Linux is inside a VM.

To access your application currently, we will use an IP.

Notice the 192.168.28.2:3000. We'll use that to access our application:

A Quick Comparison and Speed Test

This entire exercise was to get a setup that was faster than the previous one. Let's see if this setup is any faster!

I don't have anything too scientific. I'll go back and run more tests, but I was frustrated how long a new React app took WSL 1 to make.

For now, our test will be to run npx create-react-app my-new-app.

We'll be comparing the following machines. I know this isn't scientific at all. I wish I had more computers with similar setups. I wish I had done these speed tests before I upgraded to WSL 2. Oh well. We have what we have!

  • My desktop machine (super powerful, more than the laptops for sure)
  • Surface Book 2 15" i7
  • MacBook Pro 15" 2015
npx create-react-app my-new-app

Here are the numbers I got from running the test a few times on each platform.

  • Surface Book 2 WSL 1: 257 seconds
  • Surface Book 2 WSL 2: 52 seconds
  • 2015 MacBook Pro: 45 seconds
  • 2018 MacBook Pro: 38 seconds

While still not as fast as the latest MacBook Pro, WSL 2 has made significant improvements over WSL 1.

What's Next?

We'll keep getting updates and I'll keep testing to see if performance is improving. I would love to get to a point where WSL 2 on Windows could be as fast as a 2018 MacBook Pro.

I love both Windows and Mac platforms and with WSL 2, my tools on both platforms has gotten even closer:

  • Chrome
  • VS Code
  • Terminal

Happy coding!

Like this article? Follow @chrisoncode on Twitter