Installing WSL2

Written July 18, 2021, Updated April 8, 2023

Enable the Windows Feature

# these two commands must be run as an Administrator
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux -NoRestart
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

Download Microsoft's Linux Kernel

$downloadUrl="https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi"
if ((Get-CimInstance Win32_operatingsystem).OSArchitecture.StartsWith('ARM 64')) {
    $downloadUrl="https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_arm64.msi"
}
$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri $downloadUrl -OutFile .\Downloads\wsl_kernel.msi
# run it
.\Downloads\wsl_kernel.msi

Notice I used $ProgressPreference = 'SilentlyContinue'. It significantly increases the download performance using Invoke-WebRequest. Maybe "increases" is the wrong word, it makes the download performance normal.

Install the latest WSL

wsl --update
wsl --version

should return something like: WSL version: 1.1.6.0

Set the default version for WSL distributions to version 2.

wsl --set-default-version 2

The original WSL (version 1) was quite an amazing feat. Linux syscalls could execute against a Windows NT Kernel. While it worked with almost everything, it didn't work with everything, thus version 2 was born, when Microsoft decided to bundle Windows (10 and 11) with TWO kernels, the Windows NT one, and a real Linux one.

Install a Linux Distribution

wsl --list -v

You'll notice while WSL is now installed, you have no Linux distributions in your list, so you need to install one.

There are lots to choose from, but I would recommend Ubuntu 20.04 LTS. Odds of things working using arguably the most popular Linux distribution are higher than not.

$downloadUrl="https://aka.ms/wslubuntu2204"
# download it:
$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri $downloadUrl -OutFile .\Downloads\wslubuntu2204.AppxBundle
# install the exe:
If ($PSVersionTable.PSEdition -eq "Core") {
    Import-Module Appx -UseWindowsPowerShell
}
Add-AppxPackage .\Downloads\wslubuntu2204.AppxBundle

While you could navigate to the Windows Store, and search for Ubuntu, this method is just a bit faster and nerdier.

# run the exe
ubuntu.exe
# when prompted, enter a username of your choice
# when prompted for a password, enter a password of your choice

Set the root password

It might be handy to have the root password in case something happens to your sudoers file. Set it like so:

sudo passwd
# exit when you're done

Upgrade all packages

sudo apt update
sudo apt upgrade
# clean up later
sudo apt autoremove

Enable systemd

sudo tee /etc/wsl.conf <<EOF
[boot]
systemd=true
EOF
exit

Restart

Shutdown WSL

wsl --shutdown

Relaunch, check if it works

ps -p 1 -o comm --noheaders | grep -q systemd && echo 'Congratulations, systemd is enabled' || echo "Still using the 'ol init"

What could possibly go wrong?

If you happen to be running Windows on ARM, you might get this error:

Installing, this may take a few minutes... WslRegisterDistribution failed with error: 0x80370109 Error: 0x80370109 The operation timed out because a response was not received from the virtual machine or container.

If you are running Windows 10 on ARM, give up now and install Windows 11. Seriously, on ARM don't even think about running WSL2 on Windows 10. Save yourself a world of pain. Windows 11 has significantly improved support for ARM.

Source

This is a simplied version of https://docs.microsoft.com/en-us/windows/wsl/install