47

Due to limited SSD space, I can not afford for WSL (Windows Subsystem for Linux) to be installed by default on my main C: drive by the Windows Store.

Can I install WSL on a different drive? I have searched throughout Google, but there is no mention of this problem.

NotTheDr01ds
  • 28,025
DG_
  • 613

6 Answers6

64

Yes. First, note the URL of the distribution you want to install in this list, ex: https://aka.ms/wslubuntu2204

Now open PowerShell:

# Substitute the drive on which you 
# want WSL to be installed if not D:
Set-Location D:

Create a directory for our installation and change to it, we'll call it WSL:

New-Item WSL -Type Directory Set-Location .\WSL

Using the URL you found above, download the appx package:

curl.exe -L -o Linux.appx <distribution_package_url>

Make a backup and unpack:

Copy-Item .\Linux.appx .\Linux.zip Expand-Archive .\Linux.zip

Search for the installer:

Get-Childitem -Filter *.exe

You might find a file named <distribution>.exe. Run that file, and the WSL distribution should be installed in the unpack folder of the other drive. If you don't see an executable, let's look for an .appx file that was just unpacked, that is the flavor you want, and unzip that, as follows:

Set-Location Linux
# look for correct appx file:
Get-Childitem -Filter *.appx
# rename to .zip so that Expand-Archive will work
ren .\Ubuntu_2204.1.7.0_x64.appx .\Ubuntu_2204.zip
Expand-Archive .\Ubuntu_2204.zip
Set-Location .\Ubuntu_2204
# Now exe should exist:
Get-Childitem -Filter *.exe
# run it
.\ubuntu.exe

After completion, you can now delete everything just downloaded/created except ext4.vhdx (for WSL2) or rootfs (for WSL1), and ubuntu.exe file, which starts that distro and can change the default username.

rogerdpack
  • 2,394
wasif
  • 9,176
18

There seems to be a much easier answer to this, at least on windows 11 if your not really wanting to run around with powershell commands in your head.

Basically, install a distro. For example:

wsl --install -d Ubuntu

Unregister it:

wsl --unregister Ubuntu

This deletes your "root" drive as such, but doesn't remove the ubuntu image.

Go to Settings > Apps > Apps & Features, search for ubuntu, then click on the three dots button next to the result, should look something like this:

Ubuntu install thingo

Click on move, then just choose the drive.... go to the start menu (or hit the windows key or whatever works for you), search for ubuntu and run it which kicks off the installer.

Everything it does will now be on the drive you chose including the running disk image.

Takigama
  • 189
  • 1
  • 2
10

I really like @Wasif's answer, but I'll add some additional details that didn't seem appropriate for an edit:

  • First, and most importantly, as noted in the comments, the <distribution>.exe typically is no longer found directly in the Appx package. The Appx that you download is a now a MSIX package that can (and does, in this case) include packages for multiple architectures. For most distributions, after the Expand-Archive, you'll find several additional Appx files, but no .exe. For example, here are the contents of the Ubuntu 20.04 package:

    Directory: D:\WSL\instances\Ubuntu\installer\Linux
    

    Name

    AppxMetadata
    AppxBlockMap.xml
    AppxSignature.p7x
    Ubuntu_2004.2021.825.0_ARM64.appx
    Ubuntu_2004.2021.825.0_scale-100.appx
    Ubuntu_2004.2021.825.0_scale-125.appx
    Ubuntu_2004.2021.825.0_scale-150.appx
    Ubuntu_2004.2021.825.0_scale-400.appx
    Ubuntu_2004.2021.825.0_x64.appx
    [Content_Types].xml

    The important ones are the _ARM64 and _x64 packages. Choose the one for your architecture (typically _x64) and Expand-Archive that package.

    Note that, if you are using PowerShell Core, you can Expand-Archive the appx file directly. Otherwise you'll need to rename it to a .zip as @Wasif's answer recommends.

    After expanding that archive, you should find the <distribution>.exe file you need to run.

  • If you are concerned about disk space, realize that all that you need from the extracted archive are two files:

    <distribution>.exe
    install.tar.gz
    

    You can move these over to the top level directory that you created (e.g. D:\WSL) and delete everything else. I do recommend copying the files before running the .exe as the location it is in will end up being the installation location.

  • After installing, you can also remove the install.tar.gz. You could even remove the <distro>.exe, but you might want to keep it around. It can be used to either run the distribution or to change the default username if needed.

  • Note that installing this way will not create a Windows Start menu entry for the distribution. It's still possible to set one up manually to point to either wsl.exe (for the default distribution) or wsl ~ -d <distroname>

NotTheDr01ds
  • 28,025
9

The WSL2 remains on drive C, but it is possible to move the distribution (Ubuntu) to another drive (D) and save C space.

Credits for the original answer: https://superuser.com/a/1618643/1771934


Move Ubuntu Installation to Drive D:

  1. Export Ubuntu:

    • Create a backup folder on drive D:

      mkdir D:\backup
      
    • Export the Ubuntu distribution to a tar file in the backup folder:

      wsl --export Ubuntu D:\backup\ubuntu.tar
      
  2. Unregister the Existing Distribution:

    • Remove the existing Ubuntu distribution from drive C:
      wsl --unregister Ubuntu
      
  3. Import Ubuntu to Drive D:

    • Create a new folder on drive D for the WSL installation:

      mkdir D:\wsl
      
    • Import the Ubuntu distribution from the backup tar file to the new folder on drive D:

      wsl --import Ubuntu D:\wsl\ D:\backup\ubuntu.tar
      
  4. Set Default User:

    • By default, Ubuntu may utilize the root user. To revert to the previous user, follow these steps:
      • Open the Ubuntu App Folder. You can do this by running the following command:

        cd %userprofile%\AppData\Local\Microsoft\WindowsApps
        
      • Set the default user by running the following command and replacing <username> with your desired username:

        ubuntu config --default-user <username>
        

Now, your Ubuntu installation is moved to drive D, and the default user is set to the specified username.

Note: Ensure that you replace <username> with your actual username.

Leonardo
  • 191
3

WSL requires the C drive, but you can move distros to other drives, as such:

wsl --export <distro> "<distro>.bak"
wsl --unregister <distro>
wsl --import <distro> <D:\Example> <distro>.bak

^This can lead to linker errors, but hopefully those are easier to resolve than this obscure feature of import was to find.

Tia
  • 31
2

I found a trick way to get appx package via Microsoft Store which includes a <distribution>.exe directly.

  1. [optional] We can put a .wslconfig which change the swapFile position explicitly based on WSL DOC (I didn't test whether swapFile changed location automatically when we change install location).

  2. Just click Ubuntu 22.04 install button in Microsoft Store App(which only download appx package, installation will be triggered when we click Ubuntu in StartMenu first time).

  3. Then search install.tar.gz via Everything app, suprise are as follows: Expand-appx from Macrosoft Store

  4. Copy all the files to D:WSL\appx or where you like, click <distribution>.exe to install, then a ext4.vhdx file will be created like follows:after install

  5. Finally trigger uninstall from StartMenu which delete the packages in dirve-C.

QooBee
  • 21