Windows Subsystem for Linux 2.0 Guide
The Windows Subsystem for Linux (WSL and WSL 2) is a project to provide a Linux shell in Windows 10. With version 2, WSL provides a full blown Linux Virtual Machine with a Linux kernel. Therefore, after installation, you have a full Linux installation BUILT-IN to Windows 10. This makes your Windows PC or laptop on par with a Mac for running Linux. In fact it might be better.
WSL 2 Installation and Docker
To install WSL 2 you need to follow these steps.
Requirements
- Windows 10 Version 2004 or higher installed on your system.
- Check your Version:
- Click the Windows Search button or Search box.
- Search for "about".
- Open the "About this PC" dialog.
- Version information is listed there.
- Open a command prompt window:
winver
- Get the latest version of Windows here: Windows 10 Update Installation
Manual Install Method
- Install WSL 2 using manual method. The steps are provided below, always check the Microsoft docs site for latest steps. Note: The quick method requires you to join the Windows Insider program thus the manual method is covered here.
- Enable the Windows Subsystem for Linux. Open PowerShell as Administrator and run:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
- Enable virtual machine feature:
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
- Note: You may have to enable Virtualization in your machine's BIOS as well. You get an error to that effect if your machine is not configured for Virtual Machine (VM) support.
- Restart Windows.
- Download the latest Linux kernel package: WSL2 Linux kernel update package for x64 machines
- Unzip file.
- Run the installer.
- In PowerShell or Command Prompt Admin window set WSL version to 2:
wsl --set-default-version 2
- Install Ubuntu Linux from Windows Store.
- Open the Windows Store.
- Search for "Ubuntu Linux"
- Select a version. For example "Ubuntu Linux 20.04"
- Select Install.
- Open Ubuntu Linux.
- Open the Ubuntu Linux. A terminal window opens.
- The terminal displays:
Installing, this may take a few minutes...
- Note: This can take a little time.
- Set a user name and password.
- To test
wsl
settings, open a command prompt window. Enter: wsl -l -v
.
- WSL should be set to version 2.
- Note: After your launch the Ubuntu app on your desktop, if you are missing anything, you are prompted on what to fix. Make any requested fixes and then the app should launch.
- Install Windows Terminal which provided a fully featured tabbed terminal for Windows.
- Open the Windows Store.
- Search for "Windows Terminal".
- Install Windows Terminal.
Upgrading from WSL 1 to WSL 2
Believe it or not, if you had WSL 1 installed, the system will not switch you to version 2 automatically! Use the following commands to check and set.
- From a Windows Admin Command prompt check default:
wslconfig /l /all
- From a Windows Admin Command prompt set default:
wsl --set-default-version 2
Note: If you don't know what Docker is, skip this section.
Once you have WSL 2 installed, Docker will detect WSL and then ask you if you would like to switch to WSL as your default VM. Choose Yes to switch.
After you select the switch, make the following changes to Docker.
- Select Settings -> Resources -> WSL Integration.
- Check Enable Integration...
- Enable your specific distro.
Once you have done that, you can run Docker commands from Windows or from a Linux terminal. Very slick.
Removing an old version of WSL
If you played around with the early beta versions of WSL, you may need to uninstall it. To do that run:
That should get rid of the old version. If that doesn't work, try:
lxrun /install
to set up WSL 1 again.
lxrun /uninstall /full
for the full delete.
WSL Tips
Access the Windows 10 Filesystem from Linux
- Access your C: drive:
ls /mnt/c
- List your home directory:
ls /mnt/c/Users/username
- Open explorer:
explorer.exe .
- Opens an Explorer Window on Windows desktop.
- Replace '.' with whatever directory you want to open.
Access Linux Filesystem from Windows
- From Windows Explorer using this address to view the Linux file system.
- From the Windows command prompt:
- Use the the
wsl
command.
wsl ls -l
wsl ls /home/yourname
wsl sudo apt-get update
Windows 10 WSL Mount External Drive
- (1) Create a mount location in WSL:
- (2) Mount the drive in WSL:
After you do that, D:
should automount every time after that.
Terminate your WSL 2 Instance
You can terminate your WSL 2 instance from the Windows command line.
- (1) List running instances:
wsl.exe --list --running
- (2) Terminate instance:
wsl -t <DistributionName>
- Alternatively use
--terminate
instead of -t
.
Reference: https://winaero.com/terminate-running-wsl-linux-distro-windows-10/
Useful WSL Commands
Run Windows Apps
Details can be found on SuperUser
The newest versions allow you to just execute what's in the path:
$ export PATH=$PATH:/mnt/c/Windows/System32
$ notepad.exe
$ ipconfig.exe | grep IPv4 | cut -d: -f2
$ ls -la | findstr.exe foo.txt
$ cmd.exe /c dir
WSL 2 Resources