Windows Subsystem for Linux (WSL) is a Windows feature that allows developers or testers to run native Linux apps, write scripts, and run bash commands directly from Windows. Starting with Windows 10 (2004) WSL 2 is available which uses a full-featured Linux kernel. It allows running apps or Docker containers, has a high load speed, consumes fewer resources, and supports background management, and kernel updates. Thus, you can run ELF64 apps able to access the Windows file system without using third-party ports (like Cygwin).
A Linux kernel (v4.19) image in Windows 10 is a lightweight virtual machine. To run it, you don’t need to install a full Hyper-V role. Linux system calls are translated into Windows calls on the fly without using any emulator (unlike WSL1).
By default, WSL is disabled. To enable it, open Start -> Control Panel -> All Control Panel Items -> Programs and Features -> Turn Windows features on or off, check Windows Subsystem for Linux, click OK, and restart your computer.
You can enable WSL feature on Windows 10/11 using DISM:
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
or PowerShell:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
Install-WindowsFeature -Name Microsoft-Windows-Subsystem-Linux
Then you need to restart your computer.
Then update WSL to version 2. To do it, go to https://docs.microsoft.com/windows/wsl/wsl2-kernel, download wsl_update_x64.msi, and install it.
To make WSL2 a default architecture for new Linux distros, run the following command:
wsl --set-default-version 2
Then open Microsoft Store and enter “Linux” in the search bar. Select the distro you need from the list. Ubuntu, Debian, Kali Linux, SUSE Linux Enterprise Server15, openSUSE Leap 15-1, Fedora Remix for WSL, and others are available. In our example, we will use Ubuntu 20.04 LTS. Then click Get.
Invoke-WebRequest https://aka.ms/wslubuntu2004 -OutFile ubuntu-2004.zip –UseBasicParsing
Extract the archive with PowerShell:
Expand-Archive -Path .\ubuntu-2004.zip
Run the installation of the Linux image using the Ubuntu.exe file.
You can also download the image as an appx file and install it using Add-AppxPackage
cmdlet.
After the installation, you can view the installed WSL version using this command:
wsl --list –-verbose
If your Linux environment has version 1, you need to change it to WSL2:
wsl --set-version Ubuntu-20.04 2
A virtual hard disk file with the OS Linux Ubuntu 20.04 is located in the user profile: C:\Users\username\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState
.
After the distribution has been installed, its icon appears in the Start menu. To start Ubuntu, open the Start menu, click its icon and the Bash shell will open. You can also start WSL using the wsl
command. The first time you run the image, you will be prompted to create a user and password. To run commands as root, use an additional key (prefix): sudo
. WSL has common commands for Bash and CMD, and you should remember that Linux is case-sensitive.
You can run Linux commands in CMD. To do it, you must first specify WSL. For example, to view the list of files and folders in your Windows directory, run the following commands:
wsl
ls /mnt
ls/mnt/c
wsl ls ‑la /proc/cpuinfo
wsl ls ‑la “/mnt/c/Program Files”
You can also open Windows Explorer using explorer.exe, Calculator (calc.exe), Notepad (notepad.exe), Paint (mspaint.exe), Calendar (cal), and Weather (curl wttr.in
).
Another useful feature: you can open a WSL file from Windows using its system path. To do it, enter the file path in CMD:
notepad \\wsl$\Ubuntu-20.04\home\1122.txt
You can update the list of Ubuntu packages from the console using the following commands:
sudo apt-get update
sudo apt-get upgrade
After the update, the …/LocalState folder will take up 1.5 GB.
From the Linux command prompt, you can view Windows files and folders, and access them. To make it more convenient to copy files, view directories and their contents, install Midnight Commander using the following command:
sudo apt-get install mc
You can run Midnight Commander both in Bash and in CMD. The screenshot below shows that both MC panels display the list of files from both OSs.
You can display network settings (an IP address) of your WSL subsystem:
ip addr | grep eth0
Apps with graphic interfaces don’t work in WSL. However, you can try to install and use them. To run graphic apps in Linux, download and install VcXsrv Windows X Server (https://sourceforge.net/projects/vcxsrv/) on Windows.
Use the apt-get package manager to install graphic apps. For example, a browser, a text editor, or anything else:
sudo apt-get install gedit
sudo apt-get install firefox
sudo apt-get install x11-app
Then create a file in the root directory:
cd /~
vim .bash_login
Add the following line:
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0
and save the file:
Esc -> :wr -> :q
Then you can run graphic Linux apps in WSL using the commands:
firefox
or
gedit
You can install multiple Linux distros on Windows 10/11 and run them simultaneously in different WSL spaces. You can display the list of all installed WSL distros in Windows with the command:
wsl --list –all
To stop all running Linux distributions and the WSL2 kernel, run the following command:
wsl --shutdown