Sometimes you need to move your WSL installation to another drive. Maybe you’re running out of space on your current drive, or you just want to move it to a faster drive. Whatever the reason, it’s simple as using several builtin commands from WSL. For this example, I’ll be moving my Ubuntu-22.04 installation from my C: drive to my D: drive.
Steps
List current installations in cmd or windows powershell
wsl --list -vOutput:
PS C:\Users\lim> wsl --list -v NAME STATE VERSION * Ubuntu-22.04 Running 2 docker-desktop Stopped 2 docker-desktop-data Stopped 2Terminate the WSL instance you would like to remove. In this case, I’ll be terminating
Ubuntu-22.04.wsl -t Ubuntu-22.04Export the WSL instance to a tar file using the export command. In this case, I’ll be exporting
Ubuntu-22.04toD:\wsl_export\ubuntu-ex.tar.Before that, create and
cdinto the directory you want to export the tar file to! I’ve tried using the aboslute path for the tar file, but it didn’t work.cd D:\wsl_exportThen run the following command as a regular (non-admin) user.
wsl --export Ubuntu-22.04 .\ubuntu-ex.tarOutput:
PS D:\wsl_export> wsl --export Ubuntu-22.04 .\ubuntu-ex.tar The operation completed successfully.Unregister the old Ubuntu instance.
wsl --unregister Ubuntu-22.04Import the instance to a new location using the import command. In this case, I’ll be importing
ubuntu-ex.tartoD:\wsl_import\ubuntu. (This time I’ll be using the absolute path because it works for me, trycdinto the directory you want to import the tar file to if it doesn’t work.)wsl --import Ubuntu-22.04 "D:\wsl_import\ubuntu" "D:\wsl_export\ubuntu-ex.tar"Output:
PS D:\wsl_import> wsl --import Ubuntu-22.04 "D:\wsl_import\ubuntu" "D:\wsl_export\ubuntu-ex.tar" Import in progress, this may take a few minutes. Windows Subsystem for Linux Distributions: docker-desktop (Default) Ubuntu-22.04 docker-desktop-dataSet the new import as default for WSL
wsl --setdefault Ubuntu-22.04Set default user for the new import
I used the method 1 from here and modified the config in
/etc/wsl.conffrom my Ubuntu 22.04 installation.Start the Ubuntu 22.04 instance and run the following commands.
vi /etc/wsl.confThen modify the user section to:
[user] default=<your-default-username>:wqto save and quitThen stop the instance from cmd or powershell.
wsl --terminate Ubuntu-22.04When you restart the instance, it should be using the default user you set.
* Feel free to try out the other methods if this doesn’t work for you.
Voila!
Now you have successfully moved your WSL installation to another drive. Happy coding!