Windows Subsystem for Linux (WSL)

Windows Subsystem for Linux (WSL)

The Windows Subsystem for Linux (WSL) is an optional feature added by Microsoft in Windows 10 that supports running a Linux environment directly on Windows without having to use traditional virtual machine or dual-boot configuration.

WSL2 is the latest version of Windows Subsystem for Linux offering two main benefits over previous version (i.e. WSL1)

  • Increased file system performance
  • Supports full system call compatibility

Difference between WSL1 and WSL2

WSL1 used a translation layer to convert Windows system calls to Linux and vice-versa. There is a limited system call compatibility in WSL version 1.

WSL2 now runs a real Linux kernel (built and maintained by Microsoft) inside of a lightweight utility VM. This allows us to run applications such as Docker, FUSE etc. more efficiently with WSL2. You can download and install the WSL2 kernel from following link:

https://aka.ms/wsl2kernel

After one-time installation, the future kernel updates will be served along with Windows updates, which means you will get the latest security fixes and kernel improvements without needing to manage it yourself.

The kernel is open-source with its source code available on following GitHub link.

https://github.com/microsoft/WSL2-Linux-Kernel

Difference WSL and Traditional VM

With the help of hypervisor, a traditional VM creates an isolated environment in your host system that acts as a separate virtual computer with all the hardware components like CPU, RAM, Storage. This architecture allows a user to have a completely isolated system with a guest operating system.

This is not the case with WSL. While it is true that WSL version 2 runs a real Linux kernel in a Hyper-V based utility VM but it works completely behind the scenes and user does not need to install and manage the operating system and any other resources like a traditional VM.

Both WSL and traditional VM have their own pros and cons and it totally depends upon your interest and use case whether you want to run a Linux distribution with WSL2 or using a traditional virtualization platform such as VirtualBox, Hyper-V or VMware.

Choose WSL2 if:

  • You have a less powerful computer
  • You need smaller memory or storage footprint
  • You need faster I/O performance
  • You don’t want to waste your time installing and managing the virtual machine
  • You need to run a Linux distribution for development and testing purpose.

Choose traditional VM if:

  • You have a powerful computer where resources (such as memory, CPU and storage) are not a concern
  • You need to run an operating system other than GNU/Linux
  • You need a completely isolated and full-fledged system environment
  • You want to manage the operating system, applications, and resources
  • You want to run production-level workloads
  • You need advanced virtualization features such as snapshots, load-balancing etc.

Requirements for WSL2

To be able to install and run WSL2, your system must be running:

  • Windows 10 version 1903 or higher (with Build 18362 or higher) for x64 based systems
  • Windows 10 version 2004 or higher (with Build 19041 or higher) for ARM64 systems

To check your Windows version and build, just type “winver” command without quotes in Run dialog and hit enter.

Windows 10 builds lower than 18362 do not support WSL 2.

Installing WSL2 on Windows 10

If you have a supported Windows 10 version, you can follow the steps outlined below to install the WSL2.

  1. Launch Windows PowerShell console with administrator privilege.
  2. Type the following commands and hit enter:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  1. Restart your computer to make sure everything is installed properly.
  2. Now download and install the Linux Kernel from this link: https://aka.ms/wsl2kernel.
  3. To verify the WSL2 installation, open cmd.exe or PowerShell console and type following command:
wsl --list --verbose

This command is used to display the Name, State, and Version of all the Linux distributions installed on your computer. If you just finished installing WSL2, the command won’t show anything.

  1. To install any Linux distribution, open Run dialog (Windows key + R), then type “ms-windows-store:” without quotes and hit enter. This will launch Microsoft Store app.
  2. Click on the search, type “Linux” and hit enter. You will see all the popular Linux apps.

ms-windows-store

  1. Select your favorite Linux distribution and click on Install (or Get) button. If it asks you to login with Microsoft account, just click on No, thanks. This will start downloading and installing the selected Linux app.
  1. To launch the distribution, click on start and search by typing the name of your Linux distribution.

launch-wsl-distro

Tip: The most recently installed item will probably be displayed on top of start menu. In my case, it is “Ubuntu 20.04 LTS”

  1. Click on your Linux distribution name and it will display a terminal window with a text “Installing, this may take a few minutes”. This will happen only first time after installation.
    1. Once done, you will be asked to type a user name and then set a password. After typing password twice, you will be logged in to your Linux environment. Now you can start using your favorite Linux tools.

initialize-wsl-distro

    1. The good thing is that your Windows C: drive is automatically mounted to /mnt/c directory in Ubuntu. So you can browse and seamlessly transfer the files across Windows and Ubuntu. Take a look at the following image:

linux-terminal

Important WSL Commands

wsl --list --verbose

This command displays Name, State and Version of WSL distributions installed on your machine. The following image shows that I have only Ubuntu-20.04 installed, which is currently in running state and the distribution is in WSL version 1.

wsl-commands-1

If you want to see the running WSL distributions only, use wsl --list --running command.

wsl --set-version Ubuntu-20.04 2

This command will convert the “Ubuntu-20.04” distribution from WSL version 1 to version 2. Your distribution will stop and it might take a while for conversion to complete. To revert it back to version 1, just type wsl --set-version Ubuntu-20.04 1 command and hit enter.
Once the conversion is complete, you can directly start it using wsl -d Ubuntu-20.04 command. wsl-commands-2

wsl --set-default-version 2

Every Linux distribution you install will be in WSL version 1 by default. This command changes the default WSL version for all new distributions installed to that system to version 2.

wsl --terminate Ubuntu-20.04

This command terminates (Stops) the “Ubuntu-20.04” WSL distribution.

wsl --shutdown

This command terminates all running WSL distributions immediately.

If you want to learn how to run Kali Linux in WSL2 in Windows 10, please visit the following page:

Setup Kali Linux in Windows 10 WSL2



Microsoft Certified | Cisco Certified

Leave a Reply