This howto is a TL;DR version of how to do create multiple instances of the same Linux distribution in WSL.
Some reasons why you may want to do this:
- You are experimenting and want to keep your "production" version of WSL working while you make breaking changes.
- You want to have different git profiles. (I found VS Code with WSL didn't quite work well with multiple users in WSL. I was too lazy to look into this.)
- For the lulz?
Inspired by this out of date post by Hossein, this post combines instructions from two Microsoft documents:
- Import any Linux distribution to use with WSL
- Manually download Windows Subsystem for Linux (WSL) Distros
This howto assumes you are downloading Ubuntu 20. But it doesn't matter as the instructions are the same. You can use any Linux distribution so long as you can get the necessary files in a .tar format. The first document provides specific details on what is needed. If you want to use distros that are already available in the Microsoft store, follow along.
- Download the distro you want to install from the Microsoft WSL website.
- Extract the downloaded file. If you don't have one, you can use OpenSource and free to use 7-zip.
- Use Windows File Explorer to browse to the extracted directory that holds
install.tar.gz. While holding down the shift key, right-mouse-click an empty area of the folder and click "Open Powershell Window Here". - You'll need to decide where you want to host your Linux Distro's virtual drive. Unlike the default installs, you can dictate where this file is located. Run the following command:
wsl --import Ubuntu-20.04-myDevEnvironment C:\someStorageLocation\Ubuntu-20.04-myDeveEnvironment .\install.tar.gz
The important parts are:
wsl: The WSL command...--import: The import sub-command. Full instructions hereUbuntu-20.04-myDevEnvironment: What you want to call the distro. This name shows up if you use Terminal as well as when you run thewsl --listcommand. Keep in mind that this name cannot contain spaces and must be unique across all distros.C:\someStorageLocation...: Where you want the virtual drive file to be located. Fun-fact: it creates a Hyper-V Hard Disk Image File in the directory specified. While you don't have to name it the same as your distro, it's just going to confuse you if you don't..\install.tar.gz: The file in your current directory
- After install is completed, the distro is technically ready. It will run as root but it's usable. To start the distro, run
wsl -d Ubuntu-20.04-myDevEnvironment. - If this is good enough for you, you can stop here. If you want to add a user, run the following command:
adduser --ingroup sudo sohmc
The important parts are:
adduser: The adduser command. Don't useuseraddbecause it's dumb and won't create your home directory.--ingroup sudo: Add the user to the sudo group so that the user can sudo.sohmc: The username you want to add. Obviously use your own username. Or mine. Doesn't matter to me.
- At this point, if you wish to always have root access and want the option to run as a user, you should stop here. When you want to run as the user, you'll run
wsl -d Ubuntu-20.04-myDevEnvironment -u sohmcPowershell. If you wish to make it the default user, run the following command:echo -e "[user]\ndefault=sohmc" >> /etc/wsl.confinside your WSL distro. This command will insert the following into/etc/wsl.conf:
[user]
default=sohmc
- This change doesn't take place immediately. You must restart the WSL distro. In Powershell, run the following TWO commands:
wsl --terminate Ubuntu-20.04-myDevEnvironment
wsl -d Ubuntu-20.04-myDevEnvironment
That's it. If you use Windows Terminal, the entry will get automatically added with no additional work on your part.