Today I had the pleasure of getting a fresh new Windows image on my work laptop. Mind you, I still prefer a MacOS or Linux distro, but you can’t have it all. Setting up WSL in Windows is I guess the next best thing. This post provides a guide on how-to setup WSL and integrate it into VS Code for development purposes.
What is WSL
For those that are not familiar with WSL, this is the Windows Subsystem Linux. It allows you to run a Linux system within Windows without the need of spinning up a separate VM or losing valuable resources doing so.
WSL is not particularly new as the first version has been out for a few years. However, Microsoft has now released version 2 of WSL, which has now a full Linux kernel running. Furthermore, a big difference is the interaction between Windows and WSL and the very noticeable speed increase.
WSL 2 is available free of charge for any Windows version of 2004 (build 19041) and up. That’s the 2020 Spring update. If you open run (windows key + R) and type winver
, you can find your build.
Benefits of running WSL (2):
- no more need for VMs or Hyper-V and the overhead that comes with that
- running Linux code directly on your device, which allows you to interact with your Windows directory structure
- Technically, no need for Docker Desktop for Windows (with a VM), but you can run it directly in your favorite Linux distro.
- No need to install packages/software that are common to Linux distros, such as Python, git etc.
- Developing within WSL allows you to package containers and software the same way as it would run on a Linux host. No Windows adaptations necessary.
Caveats:
- If you want to run docker within WSL 2, then it’s recommended to put the files that are available to containers via docker volumes, directly within the Linux distros system and not in the Windows directory.
- There is a known high CPU bug in WSL 2 in conjunction with Ubuntu 20.04. There is now a patch available, but keep this in mind when you run into this. Task manager typically shows high CPU usage for VMMem and Node processes. I stayed with 18.04 for now, which does not have this issue.
- In VS Code, Git clone in WSL with SSH might cause issues if the SSH key is password protected. Use HTTPS to clone or an SSH key without a password.
How to install
You have to enable a couple of features within Windows. I’m going to assume that like me, you want to get the latest and greatest, so we’ll be aiming for WSL 2.
First, we install the Windows Subsystem for Linux feature. Open Powershell in administrator mode:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
To enable support for WSL 2, you also need the Virtual Machine Platform component as WSL 2 runs the linux distro in a VM, but without the need for the entire Hyper-V set up. It’s somewhat comparable to some of the sandbox features within Windows.
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Now reboot the machine.
After reboot, open Powershell in Administrator mode again and issue the following command:
wsl --set-default-version 2
If you get an message regarding the kernel component (WSL 2 requires an update to its kernel component. For information please visit https://aka.ms/wsl2kernel
), please follow the link and download the .msi file from the top link. Then run the command again.
From here on, you can open the Windows Store and browse for Linux images and install them. To list the images on your computer, use the following command:
wsl --list --verbose
If you already had an image installed in WSL 1, you can upgrade it to WSL 2:
wsl --set-version <IMAGE name> 2
Example:
wsl --set-version Ubuntu-20.04 2
Setting up a Dev environment in VS Code
Assuming you already have VS Code installed (otherwise, get it here), you will need the WSL add-on from the add-on store. It’s called the Remote Development Extension Pack and like the name suggests, you can indeed develop on other machines, not just WSL. However, that’s an whole other topic to dive into.
After installing this package, in the bottom left corner, there is a green icon with two arrows ><
pointing at each other. You can click that icon, or use the F1 keyboard shortcut and choose for Remote-WSL: New Window using Distro
which then prompts you to select the desired distro that you previously installed.
This will then prompt an installation of VS Code components within the distro to allow VS Code to connect.
Alternatively, you can also open a WSL prompt of your distro and from the desired location run code .
which will fire up VS Code and initiate the same process.
After this setup, VS Code can run terminals directly in the linux distro and execute your code there. Git operations now also run within WSL. I would recommend to add folders now to your workspace and save that workspace, so you can easily open this workspace with the WSL linux image attached at a later date.
Additional tips
If you work on files both in Windows as well as Linux, you might end up with problems in Git over line endings. It’s possible to set this setting per file in VS Code by using the bottom right part of your screen.
CRLF = Windows LF = Linux & MacOS
It’s possible to set this per project in a .gitattributes file, which will save you some time and potential headaches. It’s probably the most reliable way of fixing this issue. Put the following in your .gitattributes file in the root to have all files converted to LF, except .cmd and .bat (Windows) files.
* text=auto eol=lf
*.{cmd,[cC][mM][dD]} text eol=crlf
*.{bat,[bB][aA][tT]} text eol=crlf
To upload only UNIX line endings, you can use the following (global) command:
git config --global core.autocrlf input
and false
to disable line conversion:
git config --global core.autocrlf false
Installing Docker in for use in WSL
There are two ways of installing Docker at this point. The official route is using the Docker Desktop for Windows program and follow the official Docker documentation here. This installs Docker in Windows and let’s it utilize the WSL 2 subsystem.
Alternatively, you can follow this blog post on how to install Docker into the Linux image instead.
Read more on how to get docker containers, via WSL, into VS Code here.