How to setup WSL2 on Windows 10
Posted July 20, 2021 by ‐ 3 min read
- Make sure you have hardware virtualization enabled on your computer.
- Check if you are running the latest version of Windows 10. Version 1903 with build 18362 or higher is required.
Enabling and configuring WSL2
Open the Windows PowerShell as Administrator and paste the following commands to enable the Virtual Machine Platform and the WSL2 features.
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
After executing the commands above restart your computer, then download and install the WSL2 Linux kernel update package for x64 machines.
Open the Windows PowerShell and execute the commands below to set the default WSL to the version 2.
wsl --set-default-version 2 wsl --shutdown
Limiting WSL2 resource usage
On the Windows PowerShell execute the command below to open the WSL configration file:
The .wslconfig file allows to limit the resorce usage and set other WSL configuration params. For a computer with 16GB of RAM and 8 logical cores on the configuration below, I’m assigning half the memory, and half the number of cores to the WSL2, but that can vary depending on you hardware configuration and workload. I’m also disabling the swap space. The default swap space is 25% of the size of your memory.
[wsl2] memory=8GB processors=4 swap=0
Installing a Linux distribution
You can install a Linux distribution from the Microsoft Store.
Below are some options:
After the distribution is installed, click on Launch to finish the installation. The installer will ask for a username and password to be used on the Linux environment.
File operations are slow
The performance of WSL2 is worse than WSL1 when working with files across OS file systems. Therefore, it’s recommended that you store and work with your files inside WSL file system. Comparison between WSL 1 and WSL 2.
Shell tab completion is slow
By default, WSL appends Windows paths to the shell PATH environment variable. It does that so you can run Windows commands from the Linux command line. The problem is that WSL2 is slow when reading files on the Windows file system. You can disable WSL from automatically appending the Windows paths and add only the paths you need. To disable you can set the following setting on the .wslconfig:
[interop] appendWindowsPath = false
WSL is consuming all memory
Make sure you limit the resource usage on the .wslconfig as explained above.
Recommended additional software
Starting WSL on the linux user home directory
To start the Microsoft Terminal on the linux user home directoy you can set the Windows Terminal starting directory configuration. Note that it only works with Windows paths. So for example, for the user
udutra on a Ubuntu 20.04 distro it would be:
Visual Studio Code
After the installation, you can open the VS Code from inside the WSL Ubuntu terminal using the command code. For example, to edit the files on the current directory, you can execute: