Published in Articles on by Michiel van Oosterhout ~ 6 min read

In recent years the Windows Subsystem for Linux (WSL) has been gaining traction. Developers that use Windows to write software targeting Linux servers can use WSL to get access to a Linux environment that is integrated with Windows in a way that is otherwise just not possible. In this article we'll see exactly how to install WSL 1 or WSL 2. There are two versions of WSL, and they differ quite a lot, so we will also look at those differences, so you can make an informed decision to install one or the other (or both).

source

Installing Windows Subsystem for Linux

There are 2 versions of WSL: WSL 1, which emulates a Linux kernel, and WSL 2, which is an actual Linux kernel running inside a virtual machine. Both run unmodified ELF executables built for Linux.

WSL 1

A big part of what makes WSL 1 work is actually already present in Windows: pico processes. These are processes whose system calls are forwarded by the Windows kernel to a pico provider, a Windows kernel driver that handles the system calls .

WSL 1 is an optional Windows feature, and as we've seen in Managing optional Windows features using Windows PowerShell, those can be enabled using a simple Windows PowerShell cmdlet, if you know the name of the feature. For WSL 1 the feature name is Microsoft-Windows-Subsystem-Linux. The Windows PowerShell command below enables the WSL 1 feature:

(This command requires an elevated Windows PowerShell session, and will prompt to restart Windows.)

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

After running this command and restarting Windows you can verify the feature is enabled:

WSL 1 enabled
WSL 1 enabled

You will also find a new Windows Service, called LxssManager (more information on this below), and you will find the Windows kernel drivers lxss.sys and lxcore.sys in %SystemRoot%\System32\drivers. These implement the pico provider that emulates a Linux kernel.

The %SystemRoot%\System32\bash.exe executable has also been added. This acts like a placeholder for the bash executable in WSL's default distribution, but at this time no distribution has been registered with WSL yet.

Enabling the Windows Subsystem for Linux also will have updated %SystemRoot%\System32\wsl.exe, which now supports both WSL 1 and WSL 2. At this time, WSL 2 is not yet installed, but wsl.exe can bootstrap WSL 2. To prevent this, and use WSL 1, you must set WSL 1 as the default:

wsl --set-default-version 1
wsl --status

The --status command should print Default Version: 1 and also ask you to enable virtualization, just in case you are planning to use WSL 2. You can safely ignore this for now.

WSL 2

Whereas WSL 1 uses pico processes and a Windows kernel driver to emulate a Linux kernel, WSL 2 uses a virtual machine to run a real Linux kernel. The Windows Subsystem for Linux feature shown above is not WSL 2.

To install WSL 2, we need to enable the Virtual Machine Platform feature, install a Linux virtual machine (kernel only) via Windows Update, and install an AppX package with a newer version of wsl.exe. All of this can be done with a single command:

(This command will prompt for elevation.)

wsl --install --no-distribution

The AppX package that is installed is MicrosoftCorporationII.WindowsSubsystemForLinux_8wekyb3d8bbwe, which can also be installed through the Microsoft Store.

Windows Subsystem for Linux in the Microsoft Store
Windows Subsystem for Linux in the Microsoft Store

The AppX package registers several app execution aliases, including bash.exe as well as wsl.exe, effectively hiding the built-in %SystemRoot%\System32\wsl.exe, as well as %SystemRoot%\System32\bash.exe which was added when we installed WSL 1. These can be updated via wsl.exe --update --web-download or via the Microsoft Store, so you don't have to wait for Windows updates to get the latest features.

App execution alias for `wsl.exe` in effect after installing WSL 2
App execution alias for wsl.exe in effect after installing WSL 2

With the virtual machine in place, one or more Linux distributions (sans kernel) can be installed (see next section). When using WSL 2 each distribution will share the same virtual machine instance and Linux kernel.

Normally a guest operating system in a virtual machine is isolated from its host (the hypervisor), but that would defeat the purpose of WSL. So the WSL 2 virtual machine is allowed to be 'integrated' with Windows:

  • WSL 2's Linux kernel can delegate execution of PE executables to Windows. This means that you can execute cmd.exe from a Linux command-line shell like bash.
  • File access between the virtual machine and Windows is made possible using a file-sharing protocol.
  • Hardware access to serial devices from Linux has recently been made possible.

The degree of integration is evolving, and Microsoft is working on making the Linux network interface more integrated with Windows (e.g. sharing the same IP-address and localhost).

Installing and registering a Linux distribution

With the Windows feature enabled, a Linux distribution (sans kernel) can be installed manually (see next section), using wsl.exe --install --distribution, or via the Microsoft Store. These distributions are AppX packages, so we can use Windows PowerShell to install one:

# Download the package
$downloadsPath = "$Env:USERPROFILE\Downloads"
$packageFileName = "Debian.appx"
$packageFilePath = "$downloadsPath\$packageFileName"
$url = "https://aka.ms/wsl-debian-gnulinux"
Invoke-WebRequest -Uri $url -OutFile $packageFilePath

# Install the package
Add-AppxPackage -Path $packageFilePath

## Delete the package
Remove-Item -Force -Path $packageFilePath

The package will have added an app execution alias debian.exe, which can be used to run the distribution's default shell executable (bash). The first time you run it, the distribution is registered with WSL (this happens automatically if you run wsl --install), and you will then be asked to create a Linux account.

Bash on Debian on Windows using WSL 1
Bash on Debian on Windows using WSL 1

With a default distribution registered, you can also start its default shell using wsl.exe. At this time running bash.exe will start the default distribution's Bash shell. Windows Terminal adds a profile for every WSL distribution, unless you disable the Windows.Terminal.Wsl profile source.

A distribution can be converted to WSL 2, which moves the Linux files into a virtual hard drive. Either way, you can access Linux files of a distribution via the network share \\wsl.localhost. Linux executables can access Windows drives like C: via /mnt/c.

Conclusion

When you install Windows Subsystem for Linux you will inevitably end up with a wsl.exe that supports both WSL 1 and WSL 2. WSL 1 is Linux kernel emulation, while WSL 2 is a real Linux kernel running in a virtual machine, albeit with some integration with Windows. You can set WSL 1 as the default, thereby preventing wsl.exe from installing features you don't need. Linux distributions (without a Linux kernel) can be installed manually, via wsl.exe, or from the Microsoft Store.