Install WSL 2 on Windows 10

Install WSL 2 on Windows 10

You will need to start PowerShell as an Administrator, you might need a restart of your computer afterwards.

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

Download and install WSL Linux distribution based on Ubuntu 18.04 from the Microsoft Store. After that, type:

# if you don't have WSL 2 this command will not recognize "-v" switch
wsl -l -v

When you get WSL 2, you can do the following:

# to convert existing distribution (in my case: Ubuntu-18.04) to WSL 2
wsl --set-version Ubuntu-18.04 2

# to set WSL 2 as default for future distros
wsl --set-default-version 2

I also suggest to install Windows Terminal (Preview) from Microsoft Store, and Docker Desktop WSL 2 Tech Preview. Don’t forget docker context use wsl.

Best explanation so far: WSL2, zsh, and docker. Linux through Windows Docker Desktop version for WSL2 and was very slow compared from installing Docker by command line inside WSL2.

Nice resource for WSL2 Tricks: shayne/wsl2-hacks. For example, to access IP of WSL2: hostname -I | awk '{print $1}' but localhost also should work. Also sudo sysctl -w fs.inotify.max_user_watches=524288 is needed for Visual Studio Code.

Finally, test with this:

docker run --rm -p 80:80 tutum/hello-world (or nginxdemos/hello)

or even smaller:

docker run --rm -p 80:8000 crccheck/hello-world

localhost:80 should work from host.

I had a problem wiping my environment variables, so here they are:

System:
C:\ProgramData\DockerDesktop\version-bin;C:\Program Files\Docker\Docker\Resources\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\PROGRAMDATA\ORACLE\JAVA\JAVAPATH;C:\PROGRAM FILES (X86)\COMMON FILES\INTEL\SHARED FILES\CPP\BIN\INTEL64;C:\PROGRAM FILES\DELL\DW WLAN CARD;C:\WINDOWS\SYSTEM32;C:\WINDOWS;C:\WINDOWS\SYSTEM32\WBEM;C:\WINDOWS\SYSTEM32\WINDOWSPOWERSHELL\V1.0\;C:\PROGRAM FILES\IDM COMPUTER SOLUTIONS\ULTRAFTP;C:\PROGRAM FILES (X86)\MICROSOFT EMULATOR MANAGER\1.0\;C:\WINDOWS\SYSTEM32\OPENSSH\;C:\PROGRAM FILES\INTEL\WIFI\BIN\;C:\PROGRAM FILES\COMMON FILES\INTEL\WIRELESSCOMMON\;C:\PROGRAM FILES\GIT\CMD;C:\ProgramData\chocolatey\bin;C:\Program Files (x86)\AOMEI Backupper;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files (x86)\ZeroTier\One\;c:\bin\php7;C:\ProgramData\ComposerSetup\bin;c:\bin\exoscale 

User:
%HomeDrive%%HomePath%\cvladan\.babun;%LocalAppData%\Microsoft\WindowsApps;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;%LocalAppData%\atom\bin;%LocalAppData%\Programs\Microsoft VS Code\bin;C:\Program Files (x86)\Nmap;%LocalAppData%\Microsoft\WindowsApps;%LocalAppData%\Programs\Microsoft VS Code Insiders\bin;%AppData%\Composer\vendor\bin

(ignore) Make systemd permanent on WSL2

This is needed for snaps to work

sudo apt-get update && sudo apt-get install -yqq daemonize dbus-user-session fontconfig

cat << EOF | sudo tee /etc/profile.d/00-wsl2-systemd.sh
SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
if [ -z "$SYSTEMD_PID" ] || [ "$SYSTEMD_PID" != "1" ]; then
    exec sudo /usr/sbin/enter-systemd-namespace
fi
if [ -n "$PRE_NAMESPACE_PATH" ]; then
    export PATH="$PRE_NAMESPACE_PATH"
fi
EOF

cat << EOF | sudo tee /usr/sbin/enter-systemd-namespace
#!/bin/bash

if [ "$UID" != 0 ]; then
    echo "You need to run $0 through sudo"
    exit 1
fi

SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
if [ -z "$SYSTEMD_PID" ]; then
    /usr/sbin/daemonize /usr/bin/unshare --fork --pid --mount-proc /lib/systemd/systemd --system-unit=basic.target
    while [ -z "$SYSTEMD_PID" ]; do
        SYSTEMD_PID=$(ps -ef | grep '/lib/systemd/systemd --system-unit=basic.target$' | grep -v unshare | awk '{print $2}')
    done
fi

if [ -n "$SYSTEMD_PID" ] && [ "$SYSTEMD_PID" != "1" ]; then
    exec /usr/bin/nsenter -t "$SYSTEMD_PID" -a env \
        DISPLAY="$DISPLAY" \
        PRE_NAMESPACE_PATH="$PRE_NAMESPACE_PATH" \
        WSL_INTEROP="$WSL_INTEROP" \
        WSL_DISTRO_NAME="$WSL_DISTRO_NAME" \
        WSLENV="$WSLENV" \
        WSLPATH="$WSLPATH" \
        /bin/login -p -f "$LOGNAME"
fi
EOF

sudo chmod +x /usr/sbin/enter-systemd-namespace

cat << EOF | sudo tee /etc/sudoers.d/wsl2-systemd
Defaults        env_keep += WSLENV
Defaults        env_keep += WSL_INTEROP
Defaults        env_keep += WSL_DISTRO_NAME
Defaults        env_keep += LOGNAME
Defaults        env_keep += PRE_NAMESPACE_PATH
%sudo ALL=(ALL) NOPASSWD: /usr/sbin/enter-systemd-namespace
EOF

sudo chmod 0440 /etc/sudoers.d/wsl2-systemd

Now every time you start the terminal/bash it will automatically put you inside a pid namespace with snapd running.

date 11. Oct 2019 | modified 10. Jun 2024
filename: Windows » WSL » Essentials