33

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
  • 21,923
DG_
  • 473

6 Answers6

46

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.

rogerdpack
  • 2,226
Wasif
  • 8,474
  • 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 '20 at 06:44
  • 3
    Because I am familiar with using full commands instead of aliases. Welcome – Wasif Jul 30 '20 at 06:47
  • now I have got an issue with permission, anything I try to open with vim/ touch a file, even inside ubuntus file, it shows Permission denied – DG_ Jul 30 '20 at 09:29
  • do chmod +x before execution – Wasif Jul 30 '20 at 09:30
  • 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 '20 at 05:49
  • 4
    FYI if you obtain Debian from https://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 '21 at 03:37
  • @programmer365 this only works for wsl2 not wsl1 and their ntfs extended attributes right? – user2284570 May 08 '22 at 09:45
  • 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 '22 at 01:18
  • @DG_ there's nothing wrong with that. Any decent editor would recognize CRLF/LF correctly, even modern built-in Notepad in Windows. The line ending won't change unless you explicitly tell it to do – phuclv Aug 03 '22 at 02:10
  • Takes forever due to output of byte count. Set ENV var first $ProgressPreference = 'SilentlyContinue' – Paul Randleman Mar 04 '23 at 22:31
  • had a similar problem with @jezzaaaa so I did Copy-Item .\Ubuntu_2204.1.7.0_x64.appx .\Ubuntu2204_x64.zip then Expand-Archive .\Ubuntu2204_x64.zip then cd into that directory and saw ubuntu.exe and did .\ubuntu.exe but get Failed to attach disk 'D:\wsl\ext4.vhdx' to WSL2: The system cannot find the file specified. Error code: Wsl/Service/CreateInstance/MountVhd/ERROR_FILE_NOT_FOUND Failed to attach disk 'D:\wsl\ext4.vhdx' to WSL2: The system cannot find the file specified. – mLstudent33 May 06 '23 at 12:00
  • Worked for me, created the equivalent of d:\wsl\Ubuntu2204_x64\ext4.vhdx (or rootfs dir for WSL1) FWIW... – rogerdpack Jan 07 '24 at 06:04
  • If Invoke-WebRequest does not work for you (proxy, etc.) just download the file using your browser, and proceed with that file. – TFuto Feb 27 '24 at 07:23
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.

Takigama
  • 111
  • 9
    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." – Graham Chapman Jan 29 '22 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. – Kit Johnson Mar 05 '22 at 09: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 06 '22 at 03:50
  • @GrahamChapman Yes, just tried to Move Ubuntu 18 on Windows 10 and got "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." – AdamE Oct 25 '22 at 23:43
  • 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 – kiranpradeep Feb 19 '23 at 07: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...

    – mLstudent33 May 06 '23 at 11:02
  • @mLstudent33 I was able to make it moveable by giving my user permissions on the WindowsApps directory, worked on Windows 10. – Incomputable Nov 05 '23 at 19:41
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>

NotTheDr01ds
  • 21,923
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.

Tia
  • 31
  • 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. – NotTheDr01ds Dec 02 '22 at 05:13
  • See also https://superuser.com/questions/1550622/move-wsl2-file-system-to-another-drive for a more verbose explanation – rogerdpack Aug 17 '23 at 04: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.

QooBee
  • 21
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.