'How to run multiple WSL2 instances on Windows?
I have installed WSL2 and deployed two machines on it:
- CentOS
- Ubuntu-20.04
It is possible to run multiple instances of the Ubuntu-20.04 instance on my win10 machine?
Solution 1:[1]
Yes, entirely possible. Here are my recommended steps. In PowerShell (you could use cmd also):
Create a directory somewhere convenient where you want to work with your WSL images. Of course, I name mine
WSL. This does not have to be on your C:\ drive.Create two subdirectories,
imagesandinstances. Again, the names are really up to you.wsl -l -vjust to confirm your current distribution names.Back up your existing 20.04 distro using something like
wsl --export Ubuntu images\2021-02-05_Ubuntu20.04_snapshot.tar(assuming your distribution is named "Ubuntu", of course).Create a new instance using that snapshot using something like
wsl --import Ubuntu20.04_clone instances\Ubuntu20.04_clone images\2021-02-05_Ubuntu20.04_snapshot.tar --version 2. You should see anext4.vhdxfile in yourinstances\Ubuntu20.04_clonedirectory when it is done. The--versionis optional, but shows that you can create WSL1 and WSL2 instances side-by-side if needed. (For instance, WSL1 is much faster if you need to access the NTFS filesystem.)Launch that instance using
wsl ~ -d Ubuntu20.04_clone. Or restart Windows Terminal (assuming you are using it) and the new instance should be detected automatically.The new instance will launch as root by default. You will need to set your username by creating a
/etc/wsl.confwith the following:[user] default=me... of course, substituting your username.
Exit, terminate (via
wsl --terminate Ubuntu20.04_clone), and restart that distribution, and confirm that your environment is operating as you'd expect.Now that you have confirmed your new instance operates correctly, the following steps are optional, but recommended. (For an alternative method, see u/Zoredache's comments (plural) in this Reddit thread).
"Reset" the old image to a clean state by
wsl --unregister <distroname>of the original distribution.Re-run the "App" (technically it's an "App Execution Alias") from the Windows Start Menu with Start->Run and type Ubuntu. This will re-run the configuration steps and create your default user/password/etc. (Thanks @JackoBongo for the suggestion in the comments for this).
For convenience, I recommend going ahead and adding the
/etc/wsl.confas described above to this instance. That will keep you from having to do it each time you add or replace an instance. Note: Don't do this if you plan to distribute this image to other users; just if you are doing this for yourself.Back up this "clean copy" using the
wsl --exportcommand as above, to something like2021-02-05_Ubuntu20.04_default.tar. This "clean" version can now be your starting point for awsl --importif you want to spin up a new Ubuntu 20.04 to test something out without worrying that your apps/customizations from your normal working environment get in the way.Since you are already running multiple distributions (CentOS and Ubuntu), then I'm guessing you are using something like Windows Terminal already. But if you launch Ubuntu through the start menu with the "Ubuntu" entry, then you will probably want to copy your cloned
vhdxfile back over the newly installed clean version. You'll find the "default" version in%userprofile%\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc(or something similar).
Solution 2:[2]
There is also a GUI on GitHub (my project) for managing multiple WSL2 instances. Might save you a little time.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pang |
| Solution 2 |
