Skip to content

How to Install Multiple Ubuntu Distros in WSL2

Tools/

I recently wanted to experiment on a fresh installation of Ubuntu in WSL2. I couldn’t install another instance of the distro from Microsoft Store because I already had one. Luckily, there is a way to bypass this limitation. Using wsl command you can install the same version of Ubuntu more than once.

Backing up Current Distro

Creating a backup of your current Ubuntu distribution is needed to create duplicate installations.

Launch Windows PowerShell and follow the steps to save a copy of your current environment.

  1. List all available distributions by running wsl command with --list and --verbose flags.

    wsl -l -v
      NAME      STATE           VERSION
    * Ubuntu    Running         2
    
  2. Shutdown all distributions

    wsl --shutdown
    
  3. Run the wsl --export command and pass it your distro’s name and your desired name for the backup file. It will create a tar archive.

    wsl --export Ubuntu ubuntu.tar
    

Exporting can take a while depending on the amount of data in your distro. Be patient and you should see ubuntu.tar file in your current working directory eventually.

Installing Multiple Instances

You can install as many instances of your distro as you want from the backup you created.

Run the wsl --import command and provide a unique name for the distribution in the first option. In the second option provide a path where to install it. I have created a dedicated folder wsl-instances for this. Lastly, specify the backup tar archive.

The example below installs two instances of your backed up distro.

wsl --import UbuntuCopy .\wsl-instances\UbuntuCopy ubuntu.tar
wsl --import UbuntuAnotherCopy .\wsl-instances\UbuntuAnotherCopy ubuntu.tar

Run wsl -l -v to see the what you have just installed.

After installation you will have to change the default user back to what it was in your original installation.

  1. Access the CLI of the installed distro

  2. Create a config file

    nano /etc/wsl.conf
    
  3. Insert the following setting

    [user]
    default=your_username
    
  4. Go back to Windows PowerShell and shutdown the distro to restart it

    wsl --shutdown
    

You should see your user being used the next time you access the CLI of your distro.

Installing Clean Version

To install a clean version of your distro you have to delete the Ubuntu instance.

Make sure you have the backup you created previously.

  1. Destroy the current installation

    wsl --unregister Ubuntu
    
  2. Re-install Ubuntu running the following command

    ubuntu install
    

You should now have a clean installation of Ubuntu alongside the copy of your old distro that you created in the previous section.

You can repeat the entire process described previously to create an archive of the clean installation. You can then import a fresh distro whenever you one.

Summary

You learned to back up your current distribution and duplicate it into new instances. You also learned how to install a clean version of your distro without losing your original one.

References