Ever since Microsoft changed the CEO, it starts to embrace the open source world by annoucing new tools (VS code) as well as adding new features to windows. Now windows 10 becomes more friendly for developers. The feature we are going to test drive today is one of them.

Background on WSL1/WSL2/Docker

Thanks to the architecture of windows NT, it can actually “Simulate” any operation systems (OS) in the system level. So theoretically, windows is just a “Frontend”, it can be changed to any other systems. Indeed, when it was first released in 1993, windows NT shipped with “Frontends” of a win32, a POSIX, an OS/2 and a security subsystems. (check the figure below) So it shouldn’t be a huge issue if we want Linux to be part of the frontends.

Results from above bash

Patically, we will have to resolve some difficulties before putting it to use, like thread model, file I/O etc. However, it can be done so long as Microsoft wants to do it. Now we have two schemes from Microsoft so far, namely WSL1 and WSL2. They achieved sub system based on totally different mechanisms. WSL1 and WSL2 have their own cons and pros, so you need to make your own decision as to which one you want to use. Refer to [1] if you are interested in some of the history behind WSL.

WSL 1

WSL 1 uses a layer to simualte all Linux system calls. (See figure below) We can run most of the linux application on it, but there are some application that heavily depends on the Linux kernal, say Docker, will fail. But the good side is WSL 1 and windows will use the same file system, therefore, sharing file between two OS is fast (unlike WSL2, whose file I/O is very slow). This is mostly the same as windows NT’s orignial idea, but just add Linux to be one of the “Frontends”. (Ref[2][3])

Results from above bash

WSL2

WSL 2 is diferent. Instead of translating system calls from Linux to windows, WSL 2 merged a part of Linux kernel into windows kernel directly to improve the compatibility and performance (check the figure below). The Linux kernel it used is 4.19, which is a long term support version (LTS). It improve the performace a lot, say extracting a compressed file will be 20 times faster than WSL 1. However, the downside is the file system is much slower than WSL 1. But Linux sub system in WSL 2 will be allowed to access/control the GPU directly. This is very important for data science and Machine Learning community. (Ref [4]) This implementation results in a “new” OS kernel and the Linux part is available in github in GPL2 license:

https://github.com/microsoft/WSL2-Linux-Kernel

Results from above bash

Docker

As you can see from WSL2 architecture, WSL2 is based on Hyper-V. So it is an essentially another virtual machine. So you can actually choose among WSL2, Docker and other virtual machine (say Virtual Box). If you have the need to package docker image, it is better to use Docker directly. Particularly, in the recent version of Docker (18.02), Docker provides an option to use windoes container directly (instead of Linux containers). So you won’t need to run a Linux VM first on hyper-V. But, unlike WSL1, to run docker on WSL2 is also no problem.

On the other hand, Installing a Linux as the native OS and run a windows VM is also an option if you have to use a windows. Check out:

https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/

Steps to enable WSL 2 in windoes 10

  • Step 1, check windows 10 version, you have to use version later than build 18917. If you windows is even not activated, not a problem, you can download the 2020 May update package manully from Microsoft and install it youself:

https://www.microsoft.com/en-gb/software-download/windows10

  • Step 2: Enable the virtual machine platform. Start the powershell under Admin privilege and run below command, after which you would need to restart the computer.

“nable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform”

  • Step 3: After a restart, let’s enable the Linux Subsystem function. Start a Admin powershell like in Step 2 and runn below command:

“Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux”

  • Step 4: We now ready to choose a linux distribution from Microsoft store. Here I chose Ubuntu. (I do like Centos, but it is not availabe yet) And you need to setup a new username and password for this linux OS.

Results from above bash

  • Step 5: Run “wsl -l” to list all subsystems. If you got an error saying “wsl” command is not available as I did, you need to go back to step2 to redo the workflow or some restart is needed. If everything is good, you should see “Ubuntu-20.14” listed.

Then for using WSL2, please run “wsl –set-version Ubuntu-20.14 2”

  • Step 6: Start the WSL 2. Now if you checked out the Microsoft store again for the Linux you have installed, there will be a Launch option (which was Install previously). Next time you can launch the WSL from there.

You can set a default version of WSL to launch by “wsl –set-default-version 2”

Some common questions on WSL2

  1. The root of Linux file is under “C:\Users{WindowsUserName}\AppData\Local\Packages{LinuxDistro}\LocalState\rootfs”;

  2. We can access windows file in WSL via /mnt/c, /mnt/d, etc;

  3. If you want to ssh to WSL, you have to change the ssh port in WSL, since the same port is occupied by windows already. Also if you want to access WSL from external, remember to open a port on the firewall.

Reference:

[1] https://medium.com/microsoft-open-source-stories/when-linux-came-to-windows-204cf9abb3d6

[2] https://docs.microsoft.com/en-us/archive/blogs/wsl/wsl-system-calls

[3] https://docs.microsoft.com/en-us/archive/blogs/wsl/windows-subsystem-for-linux-overview

[4] https://docs.microsoft.com/en-us/windows/wsl/wsl2-index

[5] https://docs.microsoft.com/en-us/windows/wsl

[6] https://docs.microsoft.com/en-us/windows/wsl/compare-versions