Thursday, April 22

What is Windows Subsystem For Linux (WSL), and How Do You Use It?

Share
  •  
  •  
  •  
  •  
  •  

Windows Subsystem For Linux (WSL) is a tool provided by Microsoft to run Linux natively on Windows. It’s designed to be a seamless experience, essentially providing a full Linux shell that can interact with your Windows filesystem.

Not Just a Virtual Machine

Lets start with what WSL isn’t—it’s not a full Linux desktop experience that you might get by running Ubuntu in VirtualBox, for example. It does technically use virtualization for performance reasons, but it’s very different from running a full VM. It’s all managed for you, and just provides a simple shell and environment for you to do your work and use familiar bash commands like gitssh, and all the other tools that come with the chosen distribution.

Basically, WSL allows you to install a full Linux distribution as an app from the app store. You can simply download Ubuntu (or the distro of your choice), install it, and have it available as a app you can launch, or a profile in your terminal to run alongside CMD or Powershell.

While the simplest explanation is that it “lets you run bash on Windows,” it’s more than just a shell; you can install programs using apt and customize it like a regular Linux instance.

WSL is intended to give developers and bash veterans the Linux shell experience despite having to use Windows as the primary OS. It offers the best of both worlds by allowing you to run Windows apps, like Visual Studio, alongside a Linux shell for easier command line access.

Microsoft offers two version of WSL: version 1, and version 2. WSL 2 uses a virtual machine, and uses a full Linux kernel built and shipped with Windows.  WSL 1 is older, and generally a lot slower, but does have better performance when working across filesystems, e.g. accessing Windows files from Linux, and vice versa. We recommend WSL 2 for most operations, as it’s faster and works much better with tools like Docker.

Setting Up WSL

WSL is an optional Windows feature, so you’ll need to turn it on. Open up Powershell as Administrator via the start menu:

Then, turn it on:

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

You will need to restart your machine to apply the changes. After that, you can open up the Windows Store and search for “WSL,” which will display the available distros:

The first time you run it, you’ll be prompted to set up your username and password for the instance. These are just for Linux, and don’t need to match your windows password at all.

The first one you install should be set as your default, but if you install multiple, you will need to set the default manually. You can manage WSL via the wsl command in Powershell. For example, listing all currently installed versions:

wsl --list

Then, you can set your default WSL distro with the following command, which can also be used to swap between WSL 1 and 2″

wsl --set-version Ubuntu-20.04 2

You can also set a preferred WSL version (1 vs. 2) for all future installs:

wsl --set-default-version 2

Using WSL

You can open WSL in a few different ways. The easiest is to just press Windows and type “wsl,” which will automatically run wsl.exe and open your default distro.

This is just a basic terminal window though; if you’re using Microsoft’s new Terminal App, which we highly recommend, your WSL versions can be accessible via a dropdown menu (alongside any SSH profiles you may have set up):

In case you want to set up a new profile, the config for this is the following:

{
    "guid": "{c6eaf9f4-32a7-5fdc-b5cf-066e8a4b1e40}",
    "hidden": false,
    "name": "Ubuntu",
    "source": "Windows.Terminal.Wsl",
    "fontFace": "RobotoMono Nerd Font",
    "fontSize" : 10,
    "cursorColor" : "#FFFFFF",
    "cursorShape" : "bar",
    "fontFace" : "RobotoMono Nerd Font",
    "acrylicOpacity" : 0.75,
    "closeOnExit" : true,
    "colorScheme" : "Campbell"
},

A Note On Filesystems

Your Linux filesystem is mounted at /, as per usual. Windows is located at /mnt/c/, or whichever drive letter is your default (additional drives are mounted as well).

Since this is all virtual, your Linux files are of course stored on the drive itself. For WSL 1, these are stored with a simple folder. WSL 2 changes things though, and stores files as a virtual hard disk image, or VHDX.

You can find these images at the following path, and while they can be mounted while the distro is unregistered, we don’t recommend messing with them from the Windows side.

%USERPROFILE%\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState