I leverage a combination of Windows Subsystem for Linux (WSL), Linux Virtual Machines running on VMware Workstation Pro and Docker. Unfortunately, I have yet to find a perfect workflow, with each technology introducing different limitations regarding performance, battery life, interoperability, stability, etc.
With this in mind, I have been eagerly anticipating the general availability of Windows Subsystem for Linux 2 (WSL 2), which for the first time brings a native Linux kernel to Windows. On paper, this mitigates many of my concerns, whilst also removing the need for heavy-weight virtualisation technologies to run Docker, etc.
In the annoucement blog post, Microsoft made some bold claims regarding potential performance improvements.
“Initial tests that we’ve run have WSL 2 running up to 20x faster compared to WSL 1 when unpacking a zipped tarball, and around 2-5x faster when using git clone, npm install and cmake on various projects.”
I hope these performance improvements translate to my build process, specifically when using Docker.
This article will cover the WSL 2 installation process, as well as the results from my initial testing.
Installing WSL 2
To get started, ensure you are running Windows 10 2004 (AKA 20H1). If not, it can be downloaded via Windows Update and/or the “Download Windows 10” website.
Although not required, I would also recommend installing the new Windows Terminal, which offers native WSL support.
Once ready, open PowerShell as an administrator and run the following commands.
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Restart your system, allowing the installation of WSL and the Windows Virtual Machine Platform to complete.
Install the latest version of the WSL 2 Linux Kernal from the Microsoft website using the link below.
Next, re-open PowerShell as an administrator and set WSL 2 as the default using the following command.
wsl --set-default-version 2
WSL 2 is now setup, meaning it is time to install a Linux Distrbution from the Windows Store. There are many options (Ubuntu, Fedora, Debian, OpenSUSE, Kali), but I generally select the latest LTS release of Ubuntu. At the time of writting this is Ubuntu 20.04.
Once installed, you will be prompted to create a username and password for the Linux distribution.
Finally, it is worth confirming that your new Linux distribution is running WSL 2. This can be achieved by running the following PowerShell command as an administrator.
wsl --list --verbose
If the version of WSL is incorrect, it can be easily modified by running the following PowerShell command as an administrator.
wsl --set-version <distribution name> <versionNumber>
You can now access WSL 2 by opening the Windows Terminal and selecting your Linux distribution (e.g. Ubuntu 20.04) as a new tab.
Testing WSL 2
With WSL 2 up and running, I installed the latest version of Docker Desktop and confirmed that it was configured to utilise WSL 2 as the backend.
Running Docker on Hyper-V with Windows 1909, the Jekyll build process would take on average 175 seconds to complete. Outlined below was the result for WSL 2.
As you can see, not what I had hoped. To rule out a one-time anomaly, I re-ran the build process several times, achieving an average of 503 seconds.
At this point, I knew I had something configured incorrectly and hit the forums.
Thankfully, the answer was fairly obvious, I had cloned my Git repository on the Windows filesystem, which meant that WSL 2 was using a mount point to access the files. This approach has a significant I/O performance impact, actually worse than running Docker on Hyper-V.
To remediate, I cloned my Git repository again, this time natively within the Linux filesystem. The improvement was immediate and significant.
At 73 seconds, the build process is now over 50% faster than Docker on Hyper-V with Windows 1909.
Thanks to the new Windows Terminal, accessing and manipulating files within the Linux filesystem is very simple. You can also quickly jump to an explorer view if you prefer a GUI. Simply navigate to the required folder and run the following command.
This will open a Windows Explorer at the desired location.
Overall, I have been impressed with WSL 2. Not only does it demonstrate Microsoft’s commitment to Linux on Windows, but also makes the developer experience comparable to using macOS and/or a native Linux distribution.
This is an impressive outcome, arguably positioning Windows as the most versatile operating system for developers.