3
OS: Windows 11

I installed WSL on my Windows 11 Pro and loaded Ubuntu 20.04, as follows (from a Command Prompt run as administrator):

wsl --install Ubuntu-20.04

The install completed without any errors. I then exited the Ubuntu session, and landed on:

c:\windows\system32

I then typed:

wsl

And ended up in an Ubuntu terminal session:

john@john-x5700: /mnt/c/Windows/system32

I have two disks: C and D. appearing in the Ubuntu terminal as:

/mnt/c 

and

/mnt/d

It appears as if Ubuntu was installed on /mnt/c. What I would like to do, is have Ubuntu install in /mnt/d. Is this possible?

3
  • Your question would be sound.. but I don't understand what you are actually trying to do. Sure you can install your distros "elsewhere" but the phrasing of your question makes me inclined to think that you don't understand the file system. From WSLs point of view, it is installed to "/" (root) and not one of the drives you mention (from a logical point of view). Those are mount points to your windows drives. Perhaps I am wrong. Please explain what you are trying to accomplish. :) Commented Jan 25, 2022 at 5:46
  • The question is: I want Ubuntu to install in /mnt/d and not /mnt/c. Commented Jan 25, 2022 at 8:24
  • 1
    To install to "D: drive" see superuser.com/questions/1572834/…, to move to D: drive see superuser.com/questions/1550622/…
    – rogerdpack
    Commented Aug 17, 2023 at 4:32

1 Answer 1

9

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.

3
  • 1
    Great answer, thank you. You should publish it as your own blog entry. Commented Jan 26, 2022 at 0:42
  • Amazing answer sir! Not only was I too lazy to answer, your answer is better than mine would have even been! :) Commented Jan 26, 2022 at 2:53
  • Thanks @SeñorCMasMas (and @EastsideDev) -- I have the advantage of doing this workflow very often myself. :-) Commented Jan 26, 2022 at 2:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .