2

Just Installed the WSL in Windows 10 for Ubunutu Linux.

I was wondering how we can store the data and all the software that we are downloading and using in the WSL to another directory like D: drive because it hogs up the C: drive on windows very quickly

Should I move the entire folder or Do I have to do any other configurations in path

Currently, my WSL data is getting stored at AppData folder under
C:\Users\User\

gronostaj
  • 58,482

2 Answers2

4

No, you won't be able to just move the installation directory for WSL. WSL uses registry keys to enumerate the list of installed distributions/instances.

While in theory you could move the directory and then update the registry key, this is still not recommended for several reasons:

  • Mucking with the registry is never "recommended" (but sometimes necessary).

  • The distributions that you install from the Store have their own predefined names and locations. Changing that will confuse the "launcher" (e.g. ubuntu.exe). Re-running ubuntu.exe (etc.) may attempt to install a second copy when it detects that it has been moved.

And while there's no way to "move" an existing distribution, there are some "multi-step" workarounds that essentially boil down to:

  • Copy the existing distribution to a new location and name.
  • Delete (unregister) the old distribution.

Okay, that's the definition of a "move", yes, but just a little more complicated.

For any of the options below:

  • First, create the location where you want the new distribution to live. I'm going to call it D:\WSL\instances\Ubuntu_20.04_WSL2 here, but replace that directory as needed in the commands below.

  • We'll also name the new distribution/instance as Ubuntu_20.04_WSL2, but again, you can modify that as you wish. I choose that name because it doesn't "look like" one of the predefined names that the Store installer will create. That way, we avoid confusion between the Store version (that we'll ultimately remove) and the "moved" one that you want to use.

  • I'm also assuming that your existing distribution is named Ubuntu.

Option 1: WSL Preview Method

The easiest way to do this currently is if you have:

  • Windows 11
  • The "Windows Subsystem for Linux Preview" installed from the Microsoft Store.
  • A WSL2 instance (which you have tagged in the question). For others, note that this method won't work with WSL1 instances.

The WSL Preview provides a new feature that allows you to wsl --import an existing VHD file (the virtual disk for WSL2 distributions) directly to a new instance/location using the --vhd option.

  1. Exit any running WSL distribution

  2. From PowerShell:

    wsl -l -v
    # Confirm name of existing instance
    

    You seem to already know this part, but for others

    Find the location of the the virtual disk:

    Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss\ | ForEach-Object { (Get-ItemProperty $_.PSPATH) | Select-Object DistributionName,BasePath }

    wsl --shutdown wsl --import Ubuntu_20.04_WSL2 D:\WSL\instances\Ubuntu_20.04_WSL2 <package_directory_from_above>\ext4.vhdx --vhd

    wsl ~ -d Ubuntu_20.04_WSL2

  3. You'll now be in your new "cloned" instance that lives on the D drive. However, you'll notice that you are the root user rather than your "default" user. Follow the instructions in this answer to create a /etc/wsl.conf file to set the default user.

  4. Once you confirm that the new distribution is working as expected, you can remove the original with:

    wsl.exe --unregister Ubuntu
    # or whatever the original distribution was named
    

Option 2: Earlier versions of WSL

I just realized I've already provided most of this (and the above) method in another answer on Ask Ubuntu recently. The steps are similar to the above, but to summarize:

  • wsl --export the existing distribution
  • wsl --import the new one
  • Set the user name (as above)
  • wsl --unregister the old one when ready (as above)
  • Delete the --exported tar file if desired.

See the Ask Ubuntu answer for full details.

NotTheDr01ds
  • 28,025
4

This is now much easier (using WSL-Version: 2.4.10.0) with:

wsl --manage <distro> --move <target>

Where <distro> is the name from wsl --list and <target> is the folder to which the large ext4.vhdx shall be moved.

Fabian
  • 141