how to move ubuntu in WSL

When I started developing in WSL I don't know why, but I installed Ubuntu-20.04 app from Microsoft store instead of just Ubuntu. I know that only difference is that Ubuntu-20.04 will not get automatically updated when new Ubuntu version will be released. All I want to do is to make Ubuntu just like Ubuntu-20.04 without reconfiguring it. I thought of moving all files from Ubuntu-20.04 to Ubuntu, but I don't know if doing that is safe. Does anybody know how to do what I'm trying to do? Also should I do it?

edit: both versions of ubuntu are running on WSL-2 I have checked it using wsl -l -v as @NotTheDr01ds said.

edit 2: I want all of my configuration from Ubuntu-20.04 to Ubuntu including all files and installed packages


Solution 1:

Why you may not need to do this at all

Ok, first things first. This is probably pretty unnecessary.

The fact that the "Ubuntu" (without a version number) in the Store automatically updates is ... true, but a bit misleading in reality.

This is a bit difficult to explain, but when you install "Ubuntu" (no version) from the Store, you actually end up with two things:

  • A rootfs package (install.tar.gz) in C:\Program Files\WindowsApps\CanonicalGroup.... You can see this by starting an Administrative PowerShell and running Get-ChildItem -Recurse 'C:\Program Files\WindowsApps\Canonical*' | Where-Object {$_.Name -eq 'install.tar.gz' } | % { $_.DirectoryName }.

  • When run for the first time (via ubuntu.exe), the installer creates your actual WSL instance in your %UserProfile%\AppData\Local\Packages\CanonicalGroup....

When there's a new release on the Store, the only thing that gets updated is the rootfs package. It does not change your installed instance. (Credit and thanks to u/zoredache on Reddit who keeps reminding me of this.)

This is pretty useless for most users unless you:

  1. Unregister the instance (which deletes all configuration) and re-configure it by re-running the ubuntu.exe command.
  2. Want to use that install.tar.gz to create a second WSL/Ubuntu instance (using wsl --import). The newly created instance would then use the Store-updated rootfs.

Neither of these scenarios is very common. Even when the unversioned Store Ubuntu gets updated to (presumably) 22.04, it won't change your installed instance of 20.04, just the rootfs package.

You'll still need to run a sudo do-release-upgrade -d when you eventually want to upgrade to 22.04 (or an interim release).

So, given that ... Since you already have your Ubuntu-20.04 configured the way that you want it, there's no great advantage to you to change it, other than getting a shorter, cleaner Ubuntu named for the WSL instance.

How to do it anyway

That said, the way that WSL2 stores your data, it's not all that difficult to move configurations around. The entire filesystem for a WSL2 instance is stored in a virtual HDD named ext4.vhdx.

Since you have two Ubuntu installations (Ubuntu and Ubuntu-20.04), you'll find two of these ext4.vhdx files under %userprofile%\AppData\Local\Packages\. To get the exact locations, run the following from PowerShell:

Get-ChildItem "$env:USERPROFILE\AppData\Local\Packages\CanonicalGroupLimited*\LocalState\*"

For my installation, that's:

...\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\ext4.vhdx
...\CanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgsc\LocalState\ext4.vhdx

The solution may be fairly obvious at this point, but use caution, of course:

  • Exit all of your running WSL instances.

  • Then wsl --shutdown to make sure nothing is currently running.

  • A backup of your existing config wouldn't be a bad idea with wsl --export Ubuntu-20.04 2021-09-26_Ubuntu-20.04_backup.tar (or whatever you want to call the backup file). This basically creates a tarball of the current rootfs, including all files, ownership, and permissions.

  • At that point, simply copy your desired (Ubuntu-20.04) ext4.vhdx over the newly installed (Ubuntu) one. The real trick is simply making sure that you copy the right one. You may want to run your Ubuntu-20.04 distribution and make a quick change so that you can easily see the latter timestamp on the "right" copy.

The only real requirement is that the default user for both instances should be the same.


Another possibility -- Since you backed up your desired config with wsl --export, you could always just wsl --import that into a new instance named Ubuntu.

  • First, you'd need to uninstall the Store Ubuntu.
  • Then pick a directory for your new instance. It can be anywhere and no longer has to live under your AppData directory.
  • wsl --import Ubuntu <directory> path\to\2021-09-26_Ubuntu-20.04_backup.tar --version 2

I keep mine in %userprofile%\Documents\WSL\instances and my tar backups in %userprofile%\Documents\WSL\images. That allows me to quickly spin up new instances via wsl --import. It's nice to be able to test something out without impacting my daily environment.


Note for any WSL1 users reading this. The rootfs for WSL1 is not stored in a virtual HDD file, but as actual files and directories under ...\LocalState\rootfs\. Please do not access these files directly as it can easily cause WSL filesystem corruption (see Microsoft devblog).

If you need to simply access the files from Windows, use the \\wsl$\<distro> path in Windows as noted in that Microsoft blog. But if you need to copy the entire thing over, like in this question, the best bet would be to convert the instance to WSL2 first, via wsl --set-version <distro> 2 (do a wsl --export backup first). After the conversion, the filesystem will be in the WSL2 ext4.vhdx format.