Using Linux With WSL on Windows

Using WSL

Essential WSL Commands

You can manage the WSL distros effectively using these commands.

These are some essential WSL commands that you should be aware of.

πŸ’‘
The command line utility, wsl, used throughout this article, is also available from inside a WSL distribution. In order to access inside a Linux distribution, you need to use wsl.exe instead of wsl.

List available WSL distributions

There are several distributions, that you can install directly from either Microsoft store or through terminal commands. When it comes to listing these distributions, it is easier to use the terminal commands.

Open a terminal in Windows and run:

wsl --list --online
List all the available distributions is showed in terminal, that can be installed using either terminal or from Microsoft store.
List available distributions

Install Ubuntu, the default distribution

As specified in the above listing command, there are various distributions available, like Ubuntu and several versions of it, Debian, openSUSE, etc. But in WSL, Ubuntu is set as the default distribution, if you didn't tweak that explicitly.

So, if you want to install Ubuntu, you can open a terminal in Admin mode and run the command:

wsl --install
Install the default distribution, that is Ubuntu using the install option
Install default distribution

Install Debian or some other distribution

Not a fan of Ubuntu? Don't worry! You can install the other distributions, that are available by using the command:

wsl --install --distribution <distribution_name>

OR

wsl --install -d <distribution_name>
Install Debian in WSL. Since Debian is not the default distribution, we need to specify it explicitly.
Install Debian in WSL

You should make sure that, the distribution_name should be same as the name shown in the output of the wsl --list --online command.

List installed distributions

If you have multiple distributions installed, it is important to list those for future management. In order to do that, use the command:

wsl --list --verbose

OR

wsl -l -v
List the installed distributions in WSL. The verbose option will give you the details of each installed distribution as well.
List installed distributions

As you can see, this will give the proper name of the installed distributions, including its current status (running or stopped) and the WSL version it is using.

Run a distribution from Windows terminal

If you have installed the distribution from the store, you can launch it from the start menu. But that's not always the case. Sometimes, you may need to rely on the terminal to run a distribution.

In that case, you can run a distribution by using:

wsl -d <distribution_name>
πŸ“‹
Remember: The distribution_name is the one that appears on the output of wsl -l -v.

Terminate a running distribution

I have covered this as a separate article as, how to shut down a WSL system. In short, if you want to terminate a running WSL distribution, run:

wsl --terminate <running_distribution_name>

Or, if you intend to shut down the entire WSL system, use:

wsl --shutdown

Get the WSL version installed on your system

There are two major versions for WSL; WSL 1 and WSL 2. WSL version 2 has several advanced features and is better compared to WSL 1.

But how would you check the version of WSL on your system? To do that, you can execute the command:

wsl --version
Get the WSL version details using the --version option
Get WSL Version

In the screenshot above, this command will print the version of WSL, the kernel version it is using, etc. You can see that I am running WSL 2 version.

Check WSL Status

You can check the status of WSL, its configuration, etc. using the --status option.

wsl --status
Check the status of WSL on your system.
Check WSL status

Here, you can see that, I use WSL 2 as the default version. It also mentions that, WSL 1 is not configured properly, and what I should do to make it work.

Change the WSL version of a distribution

Let's say you are running a distribution using WSL 1, and you want to change the WSL version to 2 so that you can install Linux GUI apps with that system. For this, you can open a terminal in Admin mode and use the command below:

wsl --set-version <distribution name> <versionNumber>
Changing WSL version between 1 and 2 for an installled Ubuntu distribution.
Changing WSL version

So, in my case, I will use:

wsl --set-version Ubuntu 2
🚧
You should be cautious when changing the WSL version of a distribution. Since WSL 1 and WSL 2 use different architectures, failures resulting in data losses are possible.

Export a distribution

Backing up data is always important, if you are dealing with critical information. In Linux distributions, we have tools like Timeshift, to create comprehensive system backups.

What about WSL? Yes, WSL provides a built-in method to take a backup of your installed distribution along with all the data.

πŸ’‘
It is better to create a two separate folders called WSL_Exports and WSL_Imports to keep the exported files and imported distributions. This way, you can avoid cluttering and easy data retrieval.

To export a distribution, use the command inside the WSL_Exports folder (for convenience):

wsl --export <distribution_name> <file_name.tar>

You are exporting the distribution along with the data to a tar file. The distribution_name should be the proper name, as in the output of the command ws -l -v.

Export a WSL distribution to tar file.
Export a distribution

Import a distribution

You have exported a distribution. Now, how will you restore it so that you can work with the data it carries? For this, on an elevated terminal, run the command:

wsl --import <Distribution Name> <Import location> <exported file>

Here, for future convenience, I am using a dedicated installation folder for the imports. So, my command will look like:

Import a distribution from tar file
Import a distribution

This will import the distribution from the file.

The imported distribution won't have a start menu entry so that you can open easily. This is the case, even if the original distribution had a start menu entry.

🚧
While importing a distribution, make sure that there is no naming conflict. That means, if there is a distro with the name Ubuntu already in place, use some other name.

Unregister a distribution

To remove a distribution from the system, you can use the --unregister option of WSL. First, list the distribution using wsl -l -v. Note the name of the distribution and then perform the command:

wsl --unregister <distribution_name>
Unregister a wsl distribution
Unregister a WSL distribution

If you have installed the distribution from the list of available distributions (wsl --list --online), you may also need to uninstall the app from the system too.

Update WSL

To update WSL from the command line, you can use the command below on an elevated terminal:

wsl --update
Update a WSL distribution from command line.
Update WSL

Get help

There are many options and sub commands for WSL, that you can utilize to get the most out of WSL. So, if you need any assistance regarding some options, always use the built-in help mechanism.

wsl --help
Get help for various WSL commands and options.
WSL help system

Wrapping Up

I have gone through several important commands, that you can use to manage various distributions.

If you are curious, you can download the cheat sheet given below, that lists some more commands, and their purposes.