12

I am trying to install the Windows Subsystem for Linux in my PC (Windows 10). I opened a "Command Prompt" window by running it as administrator and ran wsl --install without changing the initial default folder C:\Windows\system32. Thus, WSL is installed in this location by default.

Later, I uninstalled WSL by going to Settings -> Apps & Features -> Windows Subsystem for Linux Update -> Uninstall.

Then, I downloaded and installed WSL Update in C:\users\myaccount. However, when I run WSL from the start menu, it still goes to C:\Windows\system32.

Is there a way to move this default location of WSL installation to C:\users\myaccount. Besides, I have noticed that WSL is still in C:\Windows\system32 folder after I uninstall WSL from Settings -> Apps & Features.

3
  • 1
    I am not sure that you understand how WSL works. There are also two versions of WSL (1 & 2). WSL is nothing (both in size and functionality) without a distro installed. I see no mention of a distro being installed here. You are going to come back and ask the same question (only about a distro, not WSL itself) once you figure this out. See this link for some help. Also, what do you care where the files are stored if they are on the same volume? Apr 3, 2022 at 18:41
  • You can only move the WSL2 to another location you cannot literally move the feature to another drive. What exactly are you trying to achieve?
    – Ramhound
    Apr 3, 2022 at 20:57
  • WSL (ubuntu) is installed in where I want it to be. However, the feature has to be in C:\Windows\system32. I don't know why, but you have already answered my question.
    – Ling
    Apr 3, 2022 at 23:36

3 Answers 3

19

It's important to understand that there are multiple components involved in a WSL installation, and whether you can "move" or change their installation location differs for each of those components:

  1. The wsl.exe command, which is what you mainly appear to be asking about, is built-in to Windows in recent releases. You didn't (and can't) actually install or uninstall the actual wsl.exe through any of the Windows Features or Apps & Features settings. It is present when you install Windows.

  2. The base Windows Subsystem for Linux ("lxss", to some degree), which is also built into Windows as a "Windows Feature". As a Windows Feature, it can be enabled or disabled, but not, to my knowledge, uninstalled. As you can see in my post here, this takes up very little disk space.

  3. The WSL2 Virtual Machine Platform, which is also a Windows feature.

Like all other Windows features, no, you can't change the location where the files live. Most will be under System32. The fact that you were in that directory when you enabled the feature has nothing to do with it.

I can't recall which of the files in the C:\Windows\System32\lxss directory are there when Windows is installed.

The other components, which are not built-in to Windows, are:

  1. The WSL2 Linux kernel. This is actually what was removed when you uninstalled the "Windows Subsystem for Linux Update". I agree that it is confusingly named.

    This official kernel is also installed in a subdirectory with the other WSL binaries, in C:\Windows\System32\lxss\tools. While you can't change the folder where this kernel is installed, you can move it to another directory if you'd like and create a file in your Windows (not Linux) user profile directory named .wslconfig with the following contents:

    [wsl32]
    kernel="C:\path\to\kernel"
    

    You can also compile additional kernels and place them whereever you like, pointing to the one you want to launch in the .wslconfig.

  2. One or more WSL distributions.

    When you install a distribution using wsl --install -d <distro> or from the Microsoft Store, it is installed by default in %USERPROFILE%\AppData\Local\Packages\<PackageName>. These can be "moved" by exporting them and re-importing them.

    From PowerShell:

    mkdir D:\WSL\images # For example
    mkdir D:\WSL\instances\<newDistroName>
    cd D:\WSL
    
    wsl -l -v # Verify distro name to export
    wsl --export <distroname> .\images\<distroname>.tar
    wsl --import <newDistroName> .\instances\<newDistroName> .\images\<distroname>.tar
    wsl --set-default <newDistroName>
    

    You'll also need to set the default username in that copied instance via the /etc/wsl.conf file as discussed in this answer.

    It might also be possible to forcibly move the distribution files from your AppData folder to another location and then update the registry corresponding registry location (HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss). However, I have not tested this, nor seen anyone else try it. If you want to give it a shot, make sure you have a backup via wsl --export above.

6
  • I've just edited the path in HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss to a new location and moved the ext4 image over and it seems to work. Jan 12, 2023 at 9:56
  • @MiroKropacek It does (usually) and it's fairly safe; I just try to avoid suggesting registry changes when there's another way to do it. It's also recommended that you change the name if the distribution was installed via one of the standard distribution installers, since the installer (and perhaps some older uninstallers) look for it in its package directory. This wouldn't be a problem unless you install more than one distribution of the same "version" (e.g. Ubuntu-22.04), which most people won't, but I commonly do. Jan 12, 2023 at 12:18
  • I tried this method but get The system cannot find the file specified.[process exited with code 4294967295 (0xffffffff)] error when starting terminal. I can browse the copied image in windows though. Any ideas?
    – warmwhisky
    Jan 5 at 16:27
  • @warmwhisky Which method exactly? The export/import in #5, or something else? Jan 5 at 18:22
  • @NotTheDr01ds Sorry, yes, exactly The export/import in #5
    – warmwhisky
    Jan 5 at 18:37
9

If changing the location of a specific distribution is the aim, this is probably the fastest and most secure way to do it :

  1. wsl --terminate distro_name
  2. move the ext4.vhdx file to new_location
  3. wsl --unregister distro_name
  4. wsl --import-in-place distro_name ext4.vhdx_file_in_new_location

all 4 operations are immediate.

5
  • 1
    The author was looking to change the location of the Windows Subsystem for Linux not the installation of a specific WSL instance.
    – Ramhound
    Jul 20, 2022 at 1:42
  • Yes, sorry, I should have mentioned that I was elaborating on point 5. of NotTheDr01ds ' comment
    – cfeard
    Jul 20, 2022 at 7:37
  • 1
    I have not been able to get the --import-in-place method to work in cmd or powershell in Windows 11. Has anyone else?
    – abalter
    Dec 23, 2022 at 19:13
  • 1
    @abalter You do need to be on Windows 11 22H2 or Windows 10 22H2 with KB5020030 or later. Then you'll need to install the latest WSL release (currently 1.0.30). See here for more details. Jan 12, 2023 at 12:12
  • It might be useful to mention where the ext4.vhdx file may be found. Dec 9, 2023 at 13:06
4

In case you want to move linux distributions installed under WSL, follow these steps :

Find out your Linux distribution through :

wsl -l

this will list all the distributions installed

Suppose Ubuntu is installed.

  1. open windows settings
  2. then open apps tab
  3. and then installed apps
  4. your installed distribution (Ubuntu in our case) will be listed here
  5. click on three-dot icon on the right side
  6. you'll get an option in the submenu "move", Click on it
  7. move distribution to your desired location

PS: as others have already explained, you can't move a certain windows feature to another drive, In case you really want to move due to storage space concerns, you can change the location of windows installation itself, i.e by reinstalling windows

2
  • Dude, I really like to use cmd steps flow, but this flow is too simple. thanks for share it.
    – Supremo
    Sep 8, 2023 at 13:44
  • happy to help :)
    – Chemist
    Sep 9, 2023 at 16:16

You must log in to answer this question.

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