The Windows Subsystem for Linux 2 is the first time Microsoft will ever ship a full Linux kernel supporting native system calls with Windows 10. Since CEO Satya Nadella took the helm from Steve Ballmer with the mission of making Microsoft a “devices and services company” and the vision of making Windows the best environment to develop for any platform, the historical animosity between Microsoft and the open source community has all but thawed.
The original Windows Subsystem for Linux (WSL), released in 2016, added a Bash shell and the ability to run a user space based on Ubuntu, openSUSE, SUSE Enterprise Linux, Debian, or Kali Linux within Windows. However no true Linux kernel was running on the operating system. The Linux system calls were translated into Windows system calls by Microsoft’s emulation layer.
The WSL was met with much fanfare by developers who wanted to test and compile Linux applications on Windows, as it meant they could use GNU tools and utilities without dual-booting or a VM. Compared to a VM, the WSL had near-native CPU performance with the exception of file system I/O. Since WSL did not have a true Linux kernel, Linux applications running in WSL were bottlenecked by the Windows hooks for handling file metadata, which were much slower than native Linux filesystems. Moreover, some applications had compatibility problems because the kernel services they depended on were not available.
Windows Subsystem for Linux 2 (WSL2) launched for Fast Ring users enrolled in the Windows Insider Program in May 2019. It ships with a full Linux kernel that is open source, and instead of relying on kernel emulation, WSL2 is a utility VM that runs atop of Windows. Doesn’t a VM mean that WSL2 will suffer a performance penalty like running a Linux VM on Windows using Hyper-V? To address this, Microsoft conceived WSL2 as a highly optimized VM using a subset of Hyper-V features, that delivers near-native CPU and filesystem I/O performance. So far most beta testers have observed that WSL2 performance is comparable to WSL1.
With the native Linux kernel, WSL2 boasts near-native filesystem performance which led Docker to announce that its upcoming version of Docker for Windows will leverage WSL2 instead of the Moby Linux VM it currently relies on to run Linux containers within Hyper-V. No longer will you be waiting for Docker for Windows to have its VM booted up. According to Docker, WSL2 and the Docker daemon launches in 2 seconds on their development laptops, meaning WSL2 can launch at the same time a container is started (instead of at bootup). As an added benefit, bind mounts from a container to the Windows host will no longer rely on a Samba service, but be natively handled by WSL2 with much better performance.
Installing WSL2 does not require Hyper-V to be enabled, only Windows Subsystem for Linux and Virtual Machine Platform in Windows optional features. As long as you’re running Insider Preview build 18917 or higher, you’ll see these features available — including on Windows 10 Home.
Although WSL2 is intended as a command line tool for developers, a desktop environment can be run inside WSL2 and you can use an X Server for Windows such as Xming or VcXSrv to listen for X11 (graphical) programs in Linux. Xfce4 is a lightweight desktop environment that developers have been able to get working with both WSL and WSL2.
WSL2 requires Insider Preview build 18917 and above. We used build 18922.
To check your current running build version of Windows, open a Powershell window and run the command winver. If the build number is > 18917, you’re ready to go.
Next, open an Administrator Powershell Window and run the following commands.
Enabling each feature requires a reboot, so save all work and close other applications.
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
Next open the Microsoft Store and install your choice of distribution. We went with Ubuntu 18.04 LTS but you can pick any distribution compatible with WSL.
Once the download is complete, open the Ubuntu shell from the Start Menu to finish installing Ubuntu. You’ll be prompted to provide a Linux username and password. Initially, WSL will be running under WSL1, the original version which does not leverage the new features.
Now open a Powershell window and run the following commands.
The first command lists the installed WSL distributions of Linux, and the second command converts Ubuntu from WSL1 to WSL2. The third command is optional, if you want all future WSL distros installed to use WSL2 features by default.
wsl --list --verbose
wsl --set-version Ubuntu-18.04 2
wsl --set-default version 2
Once done converting, listing the WSL distributions again should say that it’s version 2.
If the WSL conversion to version 2 failed with an error message such as “Conversion failed” or “Error 0x03a001a The requested operation could not be completed due to a virtual disk system limitation. Virtual disk files must be uncompressed and unencrypted and must not be sparse.”, try checking the following.
- Reboot the system.
- Virtualization extensions, such as Intel VT-x or AMD-V must be enabled in your BIOS.
- Compression and encryption must be disabled for the Ubuntu AppData folder (see Github Issue #4103). The path will look something like this.
Right click this folder and click Properties from the context menu. Then, click the “Advanced” button beside Attributes. Make sure “Compress contents to save disk space” and “Encrypt contents to secure data” are both unchecked
Try converting the distribution to version 2 again and the operation should succeed.
Now open the WSL shell again from the Start Menu (the Ubuntu app).
Notice that the kernel running is the Microsoft Linux kernel.
Linux hostname 4.19.43-microsoft-standard #1 SMP Mon May 20 19:35:22 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
ifconfig Linux command to find the LAN address of the virtual network adapter for the WSL2 virtual machine. In our particular case, it was 192.168.104.109. This is the Ubuntu network address.
Then, open a Powershell window and run the
ipconfig Windows command. In our particular case, it was 192.168.104.97. This is the Windows network address for the vEthernet adapter used for communicating with the Ubuntu VM.
In the WSL shell, use the apt package manager to install the xfce4 desktop environment.
sudo apt-get update && sudo apt-get upgrade -y
sudo apt-get install xfce4
Next, create an entry in the .bashrc file which tells graphical applications at what address to find the locally running X server. Append the following to the last line of the file, using the nano text editor (where 192.168.104.97 is the address of the Windows vEthernet adapter)
Save & exit then use the following command to reload .bashrc.
Now download and install the VcXSrv X server for Windows.
Once installed, launch VcXSrv using the “XLaunch” entry in the Start Menu.
We suggest using the following settings to configure VcXSrv.
Display settings: One large window
How to start clients: Start no client
Disable access control: Yes
Once complete, click “Finish” to launch the X server. If prompted by Windows Firewall, be sure to allow connections to VcXSrv from both Public and Private networks.
Double click the X icon in the system tray to bring VcXSrv into the foreground.
For now you will see a black screen because no graphical X11 apps are running in Linux yet.
Reopen the Ubuntu shell and run the command
The default web browser does not work, but you can install Firefox by opening up a Terminal window and typing sudo apt-get install firefox. Once Firefox is installed, you can launch it using the window manager from the Applications menu.
Featured Image Credit: Larry Ewing (Tux Penguin)