Recently I setup WSL under Windows 10 Professional to try out the capabilities and compare the functionality of Windows, Native Linux and Mac for development. I was unable to find a simple guide that outlined all the steps to get this up and running end to end when wanting to use multiple copies of the same linux instance type ie Ubuntu 20, this guide will hopefully solve that for you and provide some additional tips to setup your environment.
For the purpose of this guide we will use Ubuntu 20.04, however this should work with any of your favourite distributions.
There are quite a few steps, but once you have setup your environment it will be mostly care free.
Open a Powershell window as Administrator and run the following comments. This will setup WSL and set the default version to 2. Unless there is a specific reason to use version 1 I would highly recommend version 2.
PS C:\> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Path :
Online : True
RestartNeeded : False
PS C:\> Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform
Path :
Online : True
RestartNeeded : False
PS C:\> wsl --set-default-version 2
For information on key differences with WSL 2 please visit https://aka.ms/wsl2
To being setup we need to download the source file for the Linux distribution from Microsoft and extract it.
TIP: Make a Folder called WSL under you C:\ to keep the source files and keep your multiple distributions in a convenient location
Browse here and download Ubuntu 20.04: <https://docs.microsoft.com/en-us/windows/wsl/install-manual>
If/when this screen appears, just close it. We don’t need to install or Launch the image. This screen appears because I have already Installed the App from the store for an original Ubuntu 20.04 instance.
Go to download folder and extract the downloaded appx file (Use 7Zip or similar), you should end up with a folder something like the below.
Open a Powershell console as Administrator and run the below command.
NOTE: For each unique instance you will want a different Distro Name and Install Location, the filename is the source tar file we downloaded earlier. wsl --import <Distro> <InstallLocation> <FileName>
PS C:\> wsl --import Ubuntu-20.04-CUSTOM1 C:\WSL\Ubuntu-20.04-CUSTOM1 C:\wsl\source\Ubuntu_2004.2020.424.0_x64\install.tar.gz
Once the import process has completed, Launch the new Instance, replacing the Distro Name with what you specified above. You should then be placed inside your new Linux distro instance.
PS c:\wsl> wsl --distribution Ubuntu-20.04-CUSTOM1
root@HOSTNAME:/mnt/c/wsl#
At the linux CLI run the following, replacing ‘myusername’ with the username of your preference. Typically this will be the same username as the host, but can be what ever you wish.
root@HOSTNAME:/mnt/c/wsl# useradd myusername
root@HOSTNAME:/mnt/c/wsl# passwd myusername
root@HOSTNAME:/mnt/c/wsl# usermod -aG sudo myusername
At this point you are ready to use your new installation by running the following command from a powershell windows.
PS C:\> wsl --distribution Ubuntu-20.04-CUSTOM1 –u myusername
Here are a few extra items that will help your experience with WSL in generaly and especially when using multiple instances and importing of images.
When importing an image like we have here the normal wizard doesn’t run, so the instance will always run as root, this is undesirable. To change that you will need to use regedit. And add or set the ‘DefaultUid’ DWORD Value for each instance to the Linux UID of the user you would like to start the instance with. Typically the first user has an ID of 1000 decimal (0x03e8 hex), update this Value as you require. To change this back to root set it back to 0.
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss
Follow these instructions to install from the App Store. This provides a single console window with a simple dropdown list of all your registered instances, and you can tab between them.
https://docs.microsoft.com/en-us/windows/terminal/get-started
When using multiple instances of Linux windows terminal will show likely the same name on each tab (unless you are using a different user for each one). To set a static Tab Name for each instance go into settings in windows Terminal (in the dropdown instance list).
For each instance add the ‘tabTitle’ and ‘suppressApplicationTitle’ entries to each guid, and set the tabTitle to whatever you want.
{
"guid": "{12345678-1234-5678-1234-123456789ABC}",
"hidden": false,
"name": "Ubuntu-20.04-CUSTOM1",
"source": "Windows.Terminal.Wsl",
"tabTitle": "UBUNTU CUSTOM1",
"suppressApplicationTitle": true
}
You may want to export and import an image so you can install base packages and re-use image multiple times, or just be able to back it up or move to a new PC. You can use the ‘wsl –export’ command for this.
An example to export the image we created about would be the following (be sure to create the exports folder first)
PS C:\> wsl –-export Ubuntu-20.04-CUSTOM1 c:\wsl\exports\Ubuntu-20.04-CUSTOM1.tar
If you haven't already downloaded and install Docker desktop do this now and enable the Docker WSL 2 engine, you can natively use docker from within your WSL instances with direct integration with Docker on the host.
https://docs.docker.com/docker-for-windows/wsl/
NOTE: Don't forget to enable new distrubtions after you import them, docker WSL integration doesn't apply automatically
Windows Terminal Getting Started: https://docs.microsoft.com/en-us/windows/terminal/get-started
Windows terminal settings: https://docs.microsoft.com/en-us/windows/terminal/customize-settings/profile-settings
Windows Subsystem Manual downloads: https://docs.microsoft.com/en-us/windows/wsl/install-manual