DEV Community

loading...

Get Started with WSL 2

vishnumohanrk profile image Vishnumohan R K ・3 min read

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 or Chocolatey

These are package managers for Windows.

We need a package manager for installing LxRunOffline, a WSL management tool. It is useful for installing, uninstalling, moving our distros.

To install scoop, Open Powershell as Admin and execute

Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
Enter fullscreen mode Exit fullscreen mode

A quick side note, Windows is finally getting an official package manager called Winget. 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.

scoop bucket add extras
scoop install lxrunoffline
Enter fullscreen mode Exit fullscreen mode

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

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Enter fullscreen mode Exit fullscreen mode

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

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Enter fullscreen mode Exit fullscreen mode

After completion, Restart your system.

After boot, Set WSL 2 as default version

wsl --set-default-version 2
Enter fullscreen mode Exit fullscreen mode

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

Invoke-WebRequest -Uri https://aka.ms/wslubuntu2004 -OutFile Ubuntu.appx -UseBasicParsing
Enter fullscreen mode Exit fullscreen mode

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

move .\Ubuntu.appx .\Ubuntu.zip
Expand-Archive .\Ubuntu.zip
cd .\Ubuntu\
Enter fullscreen mode Exit fullscreen mode

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

lxrunoffline install -n Ubuntu-20.04 -d <install-dir> -f <distro img dir>\Ubuntu\install.tar.gz
Enter fullscreen mode Exit fullscreen mode

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

wsl --list --verbose
Enter fullscreen mode Exit fullscreen mode

To change WSL version

wsl --set-version Ubuntu-20.04 <ver-num>
Enter fullscreen mode Exit fullscreen mode

<ver-num> is 1 or 2.

WSL 2 is recommended.

To check distro location,

lxrunoffline get-dir -n Ubuntu-20.04
Enter fullscreen mode Exit fullscreen mode

replace Ubuntu-20.04 with name of the distro you installed

Open the distro's Terminal

Open powershell and execute

wsl
Enter fullscreen mode Exit fullscreen mode

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

wsl --set-default <distro-name>
Enter fullscreen mode Exit fullscreen mode

Now execute the first command to open the default distro.

The distro's terminal will open inside Powershell.

References and Links

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

Discussion

pic
Editor guide
Collapse
marciliocn profile image
marciliocn

Amazing text! Gratz 👏👏👏👏👏👏👏

Collapse
kazamario profile image
SuperMario

Thanks for putting this together Fam! 💯