31

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.

6 Answers 6

45

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

# Make downloads faster by not displaying progress bar:
$ProgressPreference = 'SilentlyContinue'
# Using the URL you found above, download the appx package:
Invoke-WebRequest -Uri <appx_package_url> -OutFile Linux.appx -UseBasicParsing

# 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

You can now delete everything except ext4.vhdx (for WSL2, rootfs for WSL1), and ubuntu.exe file which starts that distro and can change the default username.

12
  • 4
    Oh man! It just saved my ssd space! A little curiosity: the process Set-Location, New-Item these thing could have been done in normal commands like cd D:\, mkdir WSL ... i mean, why did you suggested these types of commands instead?
    – DG_
    Jul 30, 2020 at 6:44
  • 3
    Because I am familiar with using full commands instead of aliases. Welcome
    – Wasif
    Jul 30, 2020 at 6:47
  • 3
    by the way, for future users, who may face problem like this, what I did stupid is I edited .bashrc / .zshrc directly with my text editor program which is installed on windows machine, DO NOT EVER DO THAT
    – DG_
    Jul 31, 2020 at 5:49
  • 3
    FYI if you obtain Debian from aka.ms/wsl-debian-gnulinux there is a second layer of .appx packages that contains the files for various versions and their associated .exe
    – rovyko
    Jun 18, 2021 at 3:37
  • 3
    I just installed Ubuntu 22.04 using this method, with help from @rovyko 's comment. There is no .exe file in the extracted directory, but there are 6 .appx files. Four of these are very small (under 60k), one has a x64 in the name and the other has ARM64 in the name, and as my system is x64 I knew that was the archive I was looking for. So I did "cd Ubuntu" then "ren Ubuntu_2204.0.10.0_x64.appx Ubuntu_2204.0.10.0_x64.appx.zip" and then "Expand-Archive Ubuntu_2204.0.10.0_x64.appx.zip". After that, there was a .exe file that I could run. I also removed the .zip files to free up space.
    – jezzaaaa
    Jul 25, 2022 at 1:18
11

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.

7
  • 8
    Have you encountered the error " The publisher of this app doesn't allow it to be moved to a different location. The error code is 0x80073d21, in case you need it." Jan 29, 2022 at 17:05
  • I couldn't successfully download the app with Invoke-WebRequest, so installing via the store and then moving it was the only way that worked for me. Mar 5, 2022 at 9:43
  • Brilliant! This not only moves the VHDX but the whole WSL/ubuntu supporting files. For me, running this off of a PCIe4 NVMe drive, vs an old HDD, moving the whole kit and kaboodle is, I'd suggest, is the beast mode way of doing it.
    – kabammi
    Apr 6, 2022 at 3:50
  • 2
    For me on Win10, wsl version 1.0.3.0, the move was suceecesful. But when I tried starting from start menu I get error as Error: 0x80071772 The specified file is encrypted and the user does not have the ability to decrypt it. screenshot Feb 19, 2023 at 7:19
  • 2
    same on Windows 11 Installing, this may take a few minutes... WslRegisterDistribution failed with error: 0x80071772 Error: 0x80071772 The specified file is encrypted and the user does not have the ability to decrypt it. Press any key to continue... May 6, 2023 at 11:02
3

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>

3

WSL requires the C drive, but you can move distros to other drives;

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

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

2
  • Welcome to Super User! The problem with the --export/--import method you mention is that the distribution's package files are still installed to C:\Program Files\WindowsApps\<package>. This can take around 500MB, and the original question mentioned a lack of space on the C: drive. You can see this from an Administrative (elevated) PowerShell by running Get-ChildItem -Recurse 'C:\Program Files\WindowsApps\' | Where-Object {$_.Name -eq 'install.tar.gz' }. @wasif's answer avoids this by skipping the use of C: for the distro entirely. Dec 2, 2022 at 5:13
  • See also superuser.com/questions/1550622/… for a more verbose explanation
    – rogerdpack
    Aug 17, 2023 at 4:29
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.

2

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.

You must log in to answer this question.

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