In mid-2019, Microsoft announced version 2 of the Windows Subsystem for Linux (WSL 2). It uses a lightweight virtual machine (VM) in the background and includes a complete Linux kernel. This should lead to higher performance and improve compatibility with Linux programs.
Avatar

The first version of WSL already introduced interesting options such as running unmodified ELF64 programs that can also access the Windows file system. At the same time, it provided a large part of the Unix toolbox without the need for ports like Cygwin.

In version 1, Microsoft explicitly pointed out that WSL is based on an extension of the Windows kernel. It therefore offers a closer integration between the two systems than if you were running Linux in a VM under Hyper-V. With WSL 2, Microsoft now tries to combine the best of both approaches.

Linux kernel runs in a VM

Microsoft will ship the Linux kernel with Windows in the near future. It runs in a lightweight VM you don't have to create manually and does not require you to install Hyper-V. This concept of a utility VM is already familiar from other features such as the Windows Sandbox or virtualization-based security (e.g., Credential Guard).

Instead of translating many system calls from Linux programs to the Windows API, as was previously the case, they can now use the native interfaces of the Linux kernel. This allows more applications to run under WSL than before, including Docker containers.

Ext4 file system in VHDX

Another effect of this architecture change is that Microsoft no longer needs a hybrid file system where the one for Linux is mounted under %localappdata%. Instead, WSL 2 uses a native ext4 within the virtual hard drive file (VHDX). You can determine the location of this virtual drive in the case of Ubuntu with:

"$env:LOCALAPPDATA\Packages\" + (Get-AppxPackage -Name Canonical*).PackageFamilyName

According to Microsoft, file access in WSL 2 should be significantly faster because of the new architecture—as long as the Linux programs stick with the root file system. You'll lose this performance gain as you execute file operations on Windows drives mounted under /mnt (e.g., /mnt/c for C:).

You can also use Explorer for the Linux file system

You can also use Explorer for the Linux file system

Another improvement is that Windows programs can now access the Linux file system. For example, you can use Explorer to copy and delete files on your Linux home drive.

Disadvantages of the new architecture

However, the use of virtualization technology also has disadvantages. They do not manifest themselves in a looser integration between the two worlds, as one might assume. But executing a VM imposes higher demands on the hardware. However, most physical computers with modern CPUs easily meet these requirements because support for Intel VT or AMD-V is standard nowadays.

The situation is different if you run Windows 10 or a Semi-Annual Channel (SAC) server in a VM and want to activate WSL 2. In this case, you would need nested virtualization so that the subsystem can use a VM within the VM.

Especially under Windows 10, the increased use of utility VMs blocks other hypervisors such as VirtualBox or VMware Workstation. The latter is also supposed to run on Hyper-V in the next version. VirtualBox has supported a fallback to Hyper-V since version 6.0, albeit at the expense of performance.

If you have a problem with this side effect, you can continue to use WSL 1 for the foreseeable future, and it is also possible to switch between the two versions.

Installing WSL 2

The current previews for Windows 10 20H1 and SAC servers include WSL 2, and it will be part of the final release. There will be no support for WSL 2 on Windows Server 2019.

The installation has not changed fundamentally compared to WSL 1. You can add WSL 2 as an optional feature via the Control Panel, but this does not activate the necessary virtualization function. You have to do this via PowerShell:

Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform

However, this step is not necessary if Hyper-V or the sandbox feature is already active on the computer.

Enabling WSL via the Control Panel as an optional feature

Enabling WSL via the Control Panel as an optional feature

You can also do the whole process entirely from the command line. This is necessary under Windows Server in the SAC anyway because it only supports a server core installation.

The first step is to activate the actual WSL feature via PowerShell. To do this, call:

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

On the server, the following command also works:

Install-WindowsFeature -Name Microsoft-Windows-Subsystem-Linux

Then, as shown here, activate the virtualization functions.

Enabling virtualization functions for WSL 2 from the command line

Enabling virtualization functions for WSL 2 from the command line

Setting the WSL version

After the restart, the command line tool wsl.exe is available. It lets you switch between WSL 1 and 2, among other things. Before installing the first distribution, you should activate WSL 2 by calling:

wsl.exe --set-default-version 2

However, it is also possible to select the WSL version for each Linux distribution individually using this command:

wsl.exe --set-version <Name-of-Distribution> 2

You can determine the exact name of the distributions with this:

wsl.exe -l -v

Adding a Linux distribution

Now you install a Linux distribution. Under Windows 10, you can easily do this via the Microsoft Store. Alternatively, you can download a package via your browser from the address given on this page from Microsoft Docs.

For example, to add Ubuntu, use this command:

Add-AppxPackage CanonicalGroupLimited.Ubuntu18.04onWindows_[…].Appx
Installing Ubuntu 18.04 as a distribution for WSL using the Add AppxPackage

Installing Ubuntu 18.04 as a distribution for WSL using the Add AppxPackage

Get-AppxPackage Canonical*

The command above tells you where the package was installed and what the executable file is called. In the case of Ubuntu 18.04, the name is ubuntu1804.exe. When you start it for the first time, Linux will be installed, and it expects you to enter a user name and a password.

Setting up distribution under Server Core

This procedure does not work under Server Core because there is no web browser, and you cannot install AppX normally. Instead, open a PowerShell session, download the distribution by calling Invoke-WebRequest, and pass it a URL from the source mentioned above. For Ubuntu, this would be:

Invoke-WebRequest https://aka.ms/wsl-ubuntu-1804 -OutFile ubuntu-1804.zip ‑UseBasicParsing

When downloading a distribution with Invoke-WebRequest on Server Core, you should add the UseBasicParsing switch

The .appx format is just a .zip archive with a different file extension. The download in the example above saves the package with the .zip extension immediately. You must use this trick or rename the package to .zip afterward; otherwise, the Expand-Archive cmdlet would refuse to unpack it.

Expand-Archive -Path .\ubuntu-1804.zip

You can then find the contents of the archive in the subdirectory .\ubuntu-1804, where you start the .exe file to set up Linux.

Subscribe to 4sysops newsletter!

Ubuntu 18.04 on WSL 2 in Windows Terminal

Ubuntu 18.04 on WSL 2 in Windows Terminal

If you use Windows Terminal on your computer, it will automatically generate an entry for the newly installed distribution, and you can start Bash directly from there.

0 Comments

Leave a reply

Please enclose code in pre tags: <pre></pre>

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account