How to setup WSL2 on Windows 10

Posted July 20, 2021 by  ‐ 3 min read

Before Start

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:

notepad "$env:USERPROFILE/.wslconfig"

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.

Configuration example:

[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.

Common issues

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.

Microsoft Terminal

Windows Terminal is a modern terminal that can be used with Command Prompt, PowerShell, and Windows Subsystem for Linux (WSL). You can install Windows Terminal from the Microsoft Store.

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: \\wsl$\Ubuntu-20.04\home\udutra .

Windows Terminal Settings

Visual Studio Code

Visual Studio Code is a source code editor that offers excellent integration with WSL. The first time you open the editor, it will ask if you want to install the Remote - WSL extension.

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:

code .

References