The Complete Developer’s Guide to Windows Subsystem for Linux 2


The Complete Guide to the Windows Subsystem for Linux 2

This tutorial demonstrates how to install, manage, and use a full Linux environment on your Windows 10 PC with WSL2. You’ll be running a Linux kernel and executing native applications while seamlessly sharing files and services with Windows code editors and browsers.

Why Use Linux?

Your host almost certainly uses Linux. It’s fast, open source, and runs the majority of web software including:

  • servers (Apache, NGINX, LiteSpeed, etc.)
  • language runtimes (PHP, Python, Node.js, Ruby, etc.)
  • utility libraries (image manipulation, ZIP creation, emailers, etc.)
  • databases (MySQL, MongoDB, Redis, etc.)
  • other dependencies (Elastic Search, RabbitMQ, proxies, etc.)

Some of these dependencies may be available on Windows and macOS, but installing and maintaining identical versions is difficult. They’re often slower and you may encounter subtle differences which will not become apparent until you deploy.

Some developers install Linux on their desktop, but that may not be viable if you regularly require non-Linux software such as Microsoft or Adobe products. Alternative options:

  1. Use two PCs or dual boot on a single device. Switching between systems can be cumbersome.
  2. Run a Linux virtual machine (VM) using Hyper-V, VirtualBox, Parallels, WMware, or Vagrant. This requires considerable OS resources and reasonable IT knowledge. Sharing files between systems can be awkward.
  3. Run web applications in Docker containers. It’s another topic to learn and Docker itself runs natively on Linux. Both the Windows and macOS editions use Linux below the surface.

What is the Windows Subsystem for Linux?

The Windows Subsystem for Linux (WSL) lets developers run a Linux environment directly on Windows 10. It’s an unmodified OS, but highly integrated into Windows and without the overhead of a virtual machine.

WSL1 was groundbreaking and translated Linux system calls to Windows equivalents. This could be slow and several features weren’t available. WSL2 uses Hyper-V virtual machine technology so Linux compatibility is excellent and file access is up to 20x faster than before.

Windows 10 Requirements

WSL2 is compatible with the Home, Pro, or Server editions of Windows but not Windows 10 S (although you can often upgrade to the Home edition for free).

The Windows May 2020 update is essential — that’s version 2004 or above. Choose the Settings cog from the Start menu, then select System followed by About. The Windows specifications are shown at the bottom of the panel:

Windows specifications

The update has been slowly rolling out since the end of May 2020. If you have a version below 2004, you may be able to trigger the update by clicking the Check for updates button in Settings, then Update & Security.

If 2004 remains stubbornly unavailable, you may be able to download and install it manually from microsoft.com/software-download/windows10

However, be aware some PCs report that version 2004 is currently unavailable in the Windows Update panel. You won’t be able to upgrade until Microsoft has released a fix for your device.

PC Requirements

WSL2 (and Hyper-V) require hardware virtualization support to be enabled in your BIOS.

WARNING: fiddling with your BIOS settings can trash your PC! Be careful when making changes. Consult your manufacturer’s help pages or search for online advice about your specific make and model.

Hardware virtualization will be active on most devices, but you can check by rebooting, and opening the BIOS panels — typically by hitting the DEL, F2, or F10 key as the system starts. Look for Virtualization Technology, VTx or similar options. Ensure they are enabled, save, and reboot the device.

Finally, ensure your C: drive has at least 1GB of spare disk space — ideally more. You may be able to free up some space in Settings, System, Storage or using the Disk clean-up tool in Windows Administrative Tools.

At the time of writing, WSL2 will install Linux to your C: drive, although it’s possible to move it elsewhere after installation.

Enable WSL2

The Virtual Machine Platform and Windows Subsystem for Linux features must be enabled in Turn Windows features on or off. The panel can be accessed by hitting Start and typing “features” or from the Programs and Features icon in the classic Control Panel.

enable WSL2

Alternatively, you can execute the following commands in a Windows Powershell terminal run as an Administrator:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Reboot Windows 10, then enable WSL2 as the default by entering the following command in a Windows Powershell or command prompt:

wsl --set-default-version 2

Download Linux

You can install any number of Linux distros from the Microsoft Store accessed in the Start menu. This includes Ubuntu, Debian, SUSE, Kali, and Alpine. Enter “Linux” in the search box:

Microsoft Store

Ubuntu is a good choice unless you have specific requirements. The latest edition will be installed if you click the Ubuntu icon followed the Get button.

The download could take a while depending on your network speed.

Launch Linux

Following download, click the Launch button on the Microsoft Store app, or the new Ubuntu icon in the Start menu. This will complete the installation process and can take several minutes.

Note: the first time you launch a WSL2 distro, you may see a message in the terminal about a kernel update. Copy the link into your browser, then download and install the update. You will need to launch Ubuntu again to continue with the installation.

You’ll be prompted to enter a username and password. These are the credentials for Linux administration: they are completely separate from your Windows username and password (although choosing the same ones may be practical).

Linux will eventually be ready and your terminal will show content similar to this:

Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: ******
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 20.04 LTS (GNU/Linux 4.19.104-microsoft-standard x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Mon Jul 6 12:07:38 BST 2020

  System load:  0.27               Processes:             8
  Usage of /:   0.4% of 250.98GB   Users logged in:       0
  Memory usage: 0%                 IPv4 address for eth0: 172.21.232.173
  Swap usage:   0%

0 updates can be installed immediately.
0 of these updates are security updates.

The list of available updates is more than a week old.
To check for new updates run: sudo apt update

Update Linux

There are likely to be several Linux updates. To update Ubuntu, enter the following commands in the Linux terminal:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get autoremove

Other Linux distros will have a similar process, but check the documentation for assistance.

Switch Between WSL1 and WSL2

Existing WSL1 distros can be converted to WSL2. Entering the following command in a Windows Powershell terminal to display the installed Linux distros and their WSL version:

PS C:> wsl --list --verbose

  NAME    STATE    VERSION
* Ubuntu  Running  1

(The * asterisk highlights the default Linux installation.)

To switch Ubuntu to WSL2, enter:

wsl --set-version Ubuntu 2

Similarly, to switch back to WSL1, enter:

wsl --set-version Ubuntu 1

Set a Default Linux Distribution

When you have multiple Linux distributions installed, one must be set as the default. It will be used when wsl is entered in a Windows Powershell terminal.

To set the default distro, list your installations:

wsl --list

and set a default with:

wsl --setdefault <DistributionName>

Ubuntu example: wsl --setdefault Ubuntu.

Run Linux as a Specific User

To run your default distribution as a specific user, enter the following command in a Windows Powershell terminal:

wsl --user <username>

A default user is defined during installation and you can create others. A root user is also created, but you should avoid using it under normal circumstances. It’s too easy to enter a dangerous command!

Move Your Linux Disk Image

The Linux disk image is installed on your C: drive. Optionally, you can move it to another drive to free up space. The following instructions presume you’ll move it to D:wsl.

In a Windows Powershell terminal (not the Ubuntu terminal), enter wsl --list to view your Linux distributions, then export one by name to a back-up .tar file, e.g. D:backupubuntu.tar:

mkdir D:backup
wsl --export Ubuntu D:backupubuntu.tar

Unregister the same distribution to remove it from the C: drive:

wsl --unregister Ubuntu

Enter wsl --list to verify the distribution has been removed.

Import the backup into a new WSL2 distribution at another location, such as D:wsl:

mkdir D:wsl
wsl --import Ubuntu D:wsl D:backupubuntu.tar

Verify it has been successfully created by entering wsl --list and launching the Ubuntu app from the Start menu.

Unfortunately, Ubuntu will now use root as the default user. To use your own account, enter the following command:

ubuntu config --default-user <yourname>

where <yourname> is the username you defined during installation.

Presuming all has gone well, you can safely delete the backup file (D:backupubuntu.tar).

Install Windows Terminal

To launch the Linux terminal, you can use the Ubuntu icon, enter wsl or bash in Powershell, or use any third-party option such as Cmder, ConEmu, or Hyper. Profiles can be defined to launch %windir%system32bash.exe ~.

Another option is Windows Terminal, which is also available from the Microsoft Store or its repository at github.com/microsoft/terminal/.

Windows Terminal

Windows Terminal will automatically add your WSL2 Linux distros and offers a highly configurable range of options including tabs, split views, themes, transparency, and key bindings.

Options are defined in the settings.json file accessed from the Settings menu or Ctrl + , (comma). Default values and theme settings can be viewed in defaults.json, accessed by holding down Alt while clicking the Settings menu.

Continue reading
The Complete Developer’s Guide to Windows Subsystem for Linux 2
on SitePoint.

Leave a Reply

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