How to add second WSL2 Ubuntu distro (fresh install)

Solution 1:

Important: The following procedure works only in Windows 10 Build 18305 or above. Please make sure wsl.exe has --import option in commandline. For older Windows 10 version, try this alternative method.

Procedure:

  • First we need the rootfs tarball from Ubuntu. Open https://cloud-images.ubuntu.com/releases/ in a web browser. Go to the folder with the required Ubuntu version.

  • Download the ubuntu-x.x-server-cloudimg-amd64-wsl.rootfs.tar.gz file (x.x used as version number). Make sure the file name has amd64-wsl and the file type is .tar.gz (GZIP tarball). As example, the direct link will be like this:

https://cloud-images.ubuntu.com/releases/eoan/release/ubuntu-19.10-server-cloudimg-amd64-wsl.rootfs.tar.gz
  • Now we are going to install it using wsl.exe in commandline. Open Command Prompt. The actual command format will be like this:
wsl.exe --import <Distribution Name> <Install Folder> <.TAR.GZ File Path>
  • Run the command twice to install Ubuntu with different distribution name. The folder name and distribution name must be different otherwise there will be error shown in output. Here are the two example:
wsl.exe --import DistroA FolderA ubuntu-x.x-server-cloudimg-amd64-wsl.rootfs.tar.gz
wsl.exe --import DistroB FolderB ubuntu-x.x-server-cloudimg-amd64-wsl.rootfs.tar.gz
  • The installed distributions can be executed like this wsl.exe -d DistroA.

Notes:

  • The procedure can be used with any GNU/Linux distribution user space.

  • This does not use Windows Store. So, Windows Store neither show the name nor update the installation.

  • In Windows 10 ARM64 (AArch64), use the arm64-wsl cloud image for Ubuntu.

  • In this procedure the installation folder can be accessed by other users in that same machine. If you are using a shared machine, use proper permissions.

  • If you are willing to share the installation with other users of same machine then just export this registry HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Lxss. Then import it in other users of same machine.

Further Readings:

  • Command Reference for Windows Subsystem for Linux

Solution 2:

Answer by Biswapriyo works great, but additional steps are required to change the default user. Note that this requires build 18980 and above.

Add the non-root user via adduser command:

PS C:\Users\Username> wsl -d DistroA
root@DESKTOP:/mnt/c/Users/Username# NEW_USER=username
root@DESKTOP:/mnt/c/Users/Username# adduser "${NEW_USER}"
Adding user `username' ...
Adding new group `username' (1000) ...
Adding new user `username' (1000) with group `username' ...
Creating home directory `/home/username' ...
Copying files from `/etc/skel' ...
New password: ****
Retype new password: ****
passwd: password updated successfully
Changing the user information for username
Enter the new value, or press ENTER for the default
        Full Name []: User Name
        Room Number []:
        Work Phone []:
        Home Phone []:
        Other []:
Is the information correct? [Y/n]

Enable sudoer privileges for ${NEW_USER}:

adduser ${NEW_USER} sudo

Add set the default user in /etc/wsl.conf:

tee /etc/wsl.conf <<_EOF
[user]
default=${NEW_USER}
_EOF

Exit WSL via logout, and then issue the WSL shutdown command for the changes to take effect:

wsl --shutdown DistroA

The next time wsl -d DistroA is invoked, the ${NEW_USER} user will be active.

Solution 3:

Maybe a bit late, but found your post and tried a different way.

I wanted 2 x a Ubuntu 20.04 LTS image. First one was easy, just the standard one from the Windows store. For the second one I installed an Ubuntu 18.04 LTS image for the Windows Store and did an "do-release-upgrade" to upgrade it to Ubuntu 20.04 LTS.

Solution 4:

I recently had the same issue, as I am working with different companies at the same time, and needed a script that could easily create a new WSL2 instance, and just as easily delete it.

Deletion by itself is not hard, as the official command wsl --unregister <distro_name> works just fine, and gets rid of the WSL2 instances perfectly, without leaving any trace.

Creation however, can be troublesome or even tedious. My script does just this: https://github.com/IAL32/WSL2-Create-Distro

The following example creates a WSL2 instance using Ubuntu20.04 (Focal Fossa) using a previously downloaded tarball (the example uses the following one: https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-wsl.rootfs.tar.gz), creates a user with username myuser and adds it to the group sudo. It also sets the user password to the one given in the pipeline, and the password for the root user, also given in the pipeline.

.\CreateLinuxDistro.ps1 -INPUT_FILENAME .\focal-server-cloudimg-amd64-wsl.rootfs.tar.gz -OUTPUT_DIRNAME "$env:LOCALAPPDATA\Packages\ubuntu2004-test-1" -OUTPUT_DISTRONAME ubuntu2004-test-1 -CREATE_USER 1 -CREATE_USER_USERNAME myuser -ADD_USER_TO_GROUP 1 -ADD_USER_TO_GROUP_NAME sudo -SET_USER_AS_DEFAULT myuser

Under the hood, if a new user is created it will also change its default shell from /bin/sh to /bin/bash.