Get Started with WSL 2
Published: 2020-07-05 07:01:36


# How to setup WSL 2

Before we begin, update Windows to the latest version.

- **Version**: 2004
- **OS Build**: 19041 or later

You can check this by going to Settings => Update and Security => OS build Info

*In this post, we will setup Ubuntu 20.04, but the procedure is same, only name and download links differ, at the end of the post, I will leave links for other distros*

## Install [Scoop](https://scoop.sh/) or [Chocolatey](https://chocolatey.org/)

These are package managers for Windows.

We need a package manager for installing [LxRunOffline](https://github.com/DDoSolitary/LxRunOffline),  a WSL management tool. It is useful for installing, uninstalling, moving our distros.

To install scoop, Open Powershell as Admin and execute

```PS
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
```

A quick side note, Windows is finally getting an official package manager called [Winget](https://devblogs.microsoft.com/commandline/windows-package-manager-preview/). But now we will use either of the above.

After scoop is installed successfully, install LxRunOffline by executing the following commands one by one in powershell.

```PS
scoop bucket add extras
scoop install lxrunoffline
```

## Enable WSL 2

WSL is a optional feature, so you have to manually enable it to use it.
To do so, in Powershell (as admin), execute the following

```PS
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
```

To upgrade to WSL 2, you must enable another optional feature, "Virtual Machine Platform"
This can be done by executing the following in Powershell.

```PS
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
```

After completion, **Restart** your system.

After boot, Set WSL 2 as default version

```PS
wsl --set-default-version 2
```

## Install distro

There are quite a few distros available to install from. You can directly install any one or more from Microsoft store or manually from powershell. The problem with installing from store is that by default, all apps from store, if you have not changed the settings, will be saved in the system drive - "C" drive.

So, if you don't want to save all your project files (that you will be working with in WSL) in C drive. Consider not to save the distro in C drive, rather in another partition.

So, installing distro manually through Powershell is the best option.
**Navigate to the directory, where you want to save the distro image**, in Powershell and execute

```PS
Invoke-WebRequest -Uri https://aka.ms/wslubuntu2004 -OutFile Ubuntu.appx -UseBasicParsing
```

This will download Ubuntu 20.04 image to the current working directory.

*If you want to install other distro, check the end of the post for links, then replace the link in the above command with that link.*

After successful download, execute the following commands one by one in powershell

```PS
move .\Ubuntu.appx .\Ubuntu.zip
Expand-Archive .\Ubuntu.zip
cd .\Ubuntu\
```

Now, inside this directory you will have a tarball named `install.tar.gz`, we will use it with the WSL management tool that we installed in the beginning.

Now , in Powershell, execute

```PS
lxrunoffline install -n Ubuntu-20.04 -d <install-dir> -f <distro img dir>\install.tar.gz
```

Where `<install-dir>` is where you have want your distro, with its entire file system to live, and `<distro img dir>` is where you have installed your distro image (this will probably be your current working directory, if you have been following along).

Now this will install the distro, in the `<install-dir>`.

## Verify installation

[All commands in Powershell]

To check installed distros, with status and WSL version

```PS
wsl --list --verbose
```

To change WSL version

```PS
wsl --set-version Ubuntu-20.04 <ver-num>
```

`<ver-num>` is 1 or 2.

*WSL 2 is recommended.*

To check distro location,

```PS
lxrunoffline get-dir -n Ubuntu-20.04
```

*replace Ubuntu-20.04 with name of the distro you installed*

## Open the distro's Terminal

Open powershell and execute

```PS
wsl
 ```

If you have only one distro installed, then it will be set as default, and it will open.
If you have installed multiple distros, to set one as default, execute

```PS
wsl --set-default <distro-name>
```

Now execute the first command to open the default distro.

The distro's terminal will open inside Powershell.




### References and Links

- WSL docs => [https://docs.microsoft.com/en-us/windows/wsl/](https://docs.microsoft.com/en-us/windows/wsl/)
- Available Distro Links => [https://docs.microsoft.com/en-us/windows/wsl/install-manual#downloading-distros](https://docs.microsoft.com/en-us/windows/wsl/install-manual#downloading-distros)
- Why to use Linux File System for WSL projects and not Windows' =>  [https://docs.microsoft.com/en-us/windows/wsl/compare-versions#use-the-linux-file-system-for-faster-performance](https://docs.microsoft.com/en-us/windows/wsl/compare-versions#use-the-linux-file-system-for-faster-performance)

PS: This is my first blog post, feedback appreciated.