21

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
  • 17,574
  • 4
  • 44
  • 81
DG_
  • 353
  • 2
  • 3
  • 6

5 Answers5

32

Yes. First, find 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:
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

# Find the installer:
Get-Childitem -Filter *.exe

You should 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, look for an .appx file that was just unpacked, that is the flavor you want, and unzip that.

Set-Location Linux
# 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 it exists, run it
.\ubuntu.exe
rogerdpack
  • 2,146
  • 7
  • 32
  • 49
Wasif
  • 7,984
  • 2
  • 19
  • 32
  • 2
    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
  • 2
    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
  • 3
    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](https://devblogs.microsoft.com/commandline/extended-eol-in-notepad/). 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, though it created the equivalent of `d:\wsl\Ubuntu2204_x64\ext4.vhdx` FWIW... – rogerdpack Aug 13 '23 at 05:15
9

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.

rink.attendant.6
  • 768
  • 12
  • 27
Takigama
  • 91
  • 1
  • 1
  • 7
    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
  • 1
    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](https://drive.google.com/file/d/15Y4heWtJ_BG4MAzWUctTnucREtc8nJR1/view?usp=share_link) – kiranpradeep Feb 19 '23 at 07:19
  • 1
    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
2

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
  • 17,574
  • 4
  • 44
  • 81
2

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
  • 21
  • 1
  • 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\`. 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](https://superuser.com/a/1572837/1210833) 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
1

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
  • 11
  • 2