WSL2 - The dark knight arrives
As some of you may know that Microsoft announced Windows Subsystem for Linux back in 2016. Since then, they have been iterating and improving that product and I’ve been waiting for it to become a Linux substitute. In this post, I explain why that time has come and why should you care.
Provide Linux environment inside Windows so people can benefit from the best of both worlds
WSL1 was an attempt to provide a Linux like feel inside windows. In plain terms, they provided an interface which looked like Ubuntu or any other Linux distro of your choice and under-the-hood it was converting the commands to what windows understood. There are many technical issues with this approach but for a user it meant the following 1) Slow execution 2) No native call support for other than usual commands 3) Reliability issues.
They learned that having a translation layer to convert everything is neither scalable nor fail-safe. Below is the WSL1 architecture.
It makes complete sense to remove the bottleneck, that is the translation layer and then still make things work. This gave birth to WSL2. The solution is as simple as you can imagine, provide an actual Linux kernel that can support any distribution inside a virtual machine. This is what we have all been doing so far using VMWare, so what makes it so exciting? It’s the elegance with which they are doing it. Let’s take a look at the architecture and understand the magic.
- Lightweight Linux utility VM is indeed super light. It takes around 500 to 600 MB and boots in under 2 seconds on my ThinkPad (with base Ubuntu 18.04 installation), your mileage may vary depending upon your hardware.
- It’s Linux. The kernel is Linux 4.19 and you can choose any distribution like Ubuntu, SUSE, Kali etc from the Microsoft store.
- It’s crazy fast, I found the systems calls and speed-ups range from 5x - 20x depending on the task. It also creates a dedicated virtual hard drive (VHD) which makes I/O calls much faster than previous implementation.
- Interoperability is amazing, the platform 9 based filesystem translation works like a charm. Simply, anything you do on windows or inside Linux is immediately reflected on both environments.
- Paths are connected and so are applications. Both terminal now natively support system calls to applications on either side of the world.
That’s a lot to digest and some of it needs a demo. I recommend watching this video from Microsoft which covers most of the above points. I’ll specifically focus on the last 2 points around interoperability and environment path access.
Once you have installed WSL2 and Ubuntu on your Windows 10, you can launch Ubuntu and it will provide you with the familiar terminal. Let’s take a look at how Ubuntu talks to Windows.
This works out of the box because paths are shared.
Some of the windows applications are not included in this path but because the underneath layer (Hyper-V) allows full interoperability, we can simply call any application with an absolute path. For example, let’s call Word.
As soon as you close Word on Windows side, it reflects immediately on the terminal.
I installed the latest anaconda3 and fastai2 in my base virtual environment inside Ubuntu. Of course, I love using VS code but just to test the awesomeness of the WSL2 support, I installed VS Code and Docker Desktop application on the Windows side.
Now if I go to my root folder inside fastaiv2 on Ubuntu and call
code . it starts the VS Code UI on Windows and connects to the WSL backend automatically as shown below.
For clarification, this means that only the VS code UI is available on the Windows side and everything is running inside Ubuntu. So no auto-completion delays, no slowing down and no compilation errors.
If we look at the binaries, say python, CMD and Ubuntu terminal both are linked and work instantly. I can’t emphasize enough on how fast the connection is between the VM and base Windows.
If you think this is what you wanted then you can follow the installation instructions available here.
Here is the catch, WSL2 is only available under the preview release for Windows 10. So it is not recommended to install it on your main machine but I did anyway and haven’t faced any problems so far.
I think it’s the dark knight moment for Windows because it can still live the Bruce Wayne life as Windows, supporting all good things under it’s compatibility belt but at the same time, it can be the dark knight opening access to native support and feel for various distros.
You may have a simple question as an ML enthusiast, Does it support GPU access and control?.
The answer is, I don’t know yet because I have to buy another license for my desktop to try it but I’ll answer that soon.
With the newly found dark knight powers, I added a docker container running Debian inside my Ubuntu inside my Windows. I used the Docker Desktop app on Windows to scale to 3 containers to test the limits for my laptop and it worked smooth!
I hope it helps people who want to stick to Windows but still want to use Ubuntu to ease things up.