Before getting into how to move/install a WSL distribution on another drive, it's important to understand a few things.
First, when you run just wsl
(without any options) from PowerShell or CMD (or anywhere, really) that tells WSL to start in whatever directory the parent process passes in to it. If you are in C:\Windows\System32
in CMD, then that's where it will start.
So your WSL starting directory has nothing to do with where your WSL instance is installed.
You can override the starting directory in one of several ways:
wsl ~
: Will start in the home directory of the default user in the filesystem in WSL. For WSL2, that directory is in a virtual disk.
wsl --cd /
: Will start in the root directory of that virtual filesystem.
wsl --cd C:\
: Will start in the root of the Windows C:
drive, which is mounted at /mnt/c
by default, as you've already found. This is a mount point in the virtual filesystem to a real (likely) physical drive that is actually exposed through an internal WSL network.
All of these commands will work the same regardless of where your actual Ubuntu distribution is installed.
So really, you don't want to move it to /mnt/d
; you want to move it to somewhere on D:
.
So where is Ubuntu installed?
That virtual disk that I mentioned is really the bulk of the Ubuntu installation in WSL. It contains the entire Linux (technically, "ext4") filesystem where all your files inside WSL are stored.
The virtual disk is automatically installed into:
%userprofile%\AppData\Local\Packages\Canonical<...>\LocalState\ext4.vhdx
That should be something like:
C:\Users\<your_windows_user>\AppData\Local\Packages\Canonical<...>\LocalState\ext4.vhdx
Warning -- Do not attempt to move this manually.
So how can I move it or change where it is installed?
Technically, you can't "move it" or "change where it is installed", but as a perfectly acceptable workaround, you can:
- Make a backup of your existing Ubuntu installation
- Restore it to whatever directory you want (on the
D:\
drive in this case)
- Remove the original
To do that:
First, in PowerShell, create the following directory structure somewhere on your D:
drive:
mkdir D:\WSL\instances\Ubuntu2004
mkdir D:\WSL\images
cd D:\WSL\images
Then, while still in the images
directory:
wsl --export Ubuntu-20.04 ubuntu.tar
wsl --import Ubuntu2004 D:\WSL\instances\Ubuntu2004 ubuntu.tar --version 2
The first line creates the backup, and the second creates a new WSL instance named Ubuntu2004
(instead of the default Ubuntu-20.04
) on your D:
drive.
Start your new instance via:
wsl ~ -d Ubuntu2004
WSL doesn't "remember" the default user for an --import
'd instance, so you should be root
at the moment. You'll need to set your default username for this instance per this answer. Namely:
sudo -e /etc/wsl.conf
And add the following:
[user]
default=<your_username>
Save, exit, and exit Ubuntu.
wsl --terminate Ubuntu2004
wsl ~ -d Ubuntu2004
If everything is working as expected, then set that instance as your default. Again, back in PowerShell:
wsl --set-default Ubuntu2004
Now, when you type wsl ~
you will start in that instance, as you can confirm by running wsl -l -v
(again, from PowerShell) and/or echo $WSL_DISTRO_NAME
(from inside Ubuntu).
Once you have confirmed that everything is working as intended, you can remove the original with:
wsl --unregister Ubuntu-20.04
Remember, this is a destructive operation and will remove all files in that virtual filesystem.
Of course, even though this is now installed under the D:\WSL
directory, unless you specify the directory when starting (via something like wsl ~
or one of the other methods mentioned), you are still going to start in whatever directory the parent process passes in (e.g. C:\Windows\System32
).
One great advantage of this technique is that, once you learn it, you can install multiple distributions. You can easily create a copy of your existing Ubuntu installation, try something out in it, and then get rid of the "temporary" copy.