How to run multiple WSL2 instances on Windows?
Asked Answered
P

2

16

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?

Prevost answered 5/2, 2021 at 14:52 Comment(0)
P
33

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, images and instances. Again, the names are really up to you.

  • wsl -l -v just 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 an ext4.vhdx file in your instances\Ubuntu20.04_clone directory when it is done. The --version is 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.conf with 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.conf as 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 --export command as above, to something like 2021-02-05_Ubuntu20.04_default.tar. This "clean" version can now be your starting point for a wsl --import if 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 vhdx file back over the newly installed clean version. You'll find the "default" version in %userprofile%\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc (or something similar).

Plate answered 5/2, 2021 at 17:16 Comment(7)
Thanks! By the way, are those instances running in parallel, or they somehow time-share a singleton kernel?Incretion
Note that there are no empty characters before and after the = in /etc/wsl.confIphagenia
If you want to restart from a fresh state it doesn't seem to be mandatory to uninstall the Ubuntu and download it again. Just unregister the current Ubuntu distribution (wsl --unregister Ubuntu) and launch it again from the start menu. A console window appears and the initial install process starts.Economical
Thanks @Economical - That's a great point, and does simplify things. I finally got around to editing the answer to include your suggestion. I believe this will work with any WSL distro in the Store as well (not just Ubuntu). I tested with Alpine, but the process should be valid for others as well.Plate
I was super excited by this but alas it doesn't appear to restore any files, directories or packages that were installed in the backup. Am i missing something?Berstine
@Berstine Hmm. The tar file generated by wsl --export should have everything in that instance. Note that my instructions had you exporting to tgz (gzipped tar), but it's actually just a plain .tar that wsl --export creates. That shouldn't make a difference though, but it will for troubleshooting. Rename it to tar and check the contents of the tarball to see what's in there. Happy to help you further on this, but probably need to create a new question (on Super User, where it's more on-topic) so we can get the details. Just too much for comments.Plate
@古今 And so sorry I never replied to your question. Hopefully you have figured out by now that all WSL2 instances share the same kernel. To the best of my understanding, what is happening behind the scenes is that there's a "hidden" VM running with the kernel. Essentially, this VM creates new containers/namespaces for each WSL2 instance. At the time you asked, I really didn't understand what was happening as well as I do now.Plate
O
7

There is also a GUI on GitHub (my project) for managing multiple WSL2 instances. Might save you a little time.

Otha answered 18/9, 2021 at 0:27 Comment(2)
Looks pretty cool. Have been thinking of doing something similar, but I went the route of writing PowerShell autocompletions for the WSL arguments. I'll have to check yours out soon. My next step was probably going to be dialog based to run it in the terminal, but I like the Flutter concept. Also just a reminder that Stack requires you to disclose that you are linking to your own project when you do so.Plate
@Plate Oups, I forgot that. Added it right away. Your autocompletion project looks nice indeed. That would save a lot of time when you don't want to deal with a GUI. Unfortunately Windows' PS/cmd autocompletion is not nearly as thought through as the one from Linux.Otha

© 2022 - 2024 — McMap. All rights reserved.