How to Install WSL/WSL2 Distro on an External Drive

AuthorGormery

By: Gormery

Published: 11/1/2023

Updated: 1/6/2024

Scroll to read expand_more
How to Install WSL/WSL2 Distro on an External Drive

It is possible to edit this blog post. Please login to edit.

I had this problem today. I had a WSL Ubuntu distro with important stuff on it. It was on my computer SSD (C:\) drive, but I needed to reset my computer. It would be a hassle to move them manually, especially because many of them were Linux programs. So, I needed to move the whole distro. After some research, I theorized two ways to do it.

The first way was to install another distro on my own specified path and then move the files there using some Linux tool like SSH. However, I wasn't able to find a way to define the path.

The second way, which I successfully did, was to export the whole distro to an external file and then import it while defining a path to install it. This way, I didn't need to move the data manually because the data is moved with the Linux.

Here is how I did it:

  • Create a `vm` folder beforehand on the external disk.
  • Export the WSL distribution to a tar file:
    0.
  1. When exporting the code, do it as a tar file. It might just be `distroname.tar`.
  2. 1.
  3. `wsl --export <distribution_name> <tar_file_path>`
  4. 2.
  5. Replace `<distribution_name>` with the name of the distribution you want to move and `<tar_file_path>` with the path and filename of the tar file you want to create. For example:
  6. 3.
  7. `wsl --export Ubuntu D:\vm\ubuntu.tar`
  8. 4.
  9. This will create a tar file named `ubuntu.tar` in the `D:\vm` directory.
  • Copy the tar file to the external hard drive.
  • Import the WSL distribution from the tar file on the external hard drive:
    0.
  1. `wsl --import <new_distribution_name> <install_directory> <tar_file_path>`
  2. 1.
  3. Replace `<new_distribution_name>` with the new name you want to give the distribution, `<install_directory>` with the directory on the external hard drive where you want to install the distribution, and `<tar_file_path>` with the path and filename of the tar file you copied in step 2. For example:
  4. 2.
  5. `wsl --import Ubuntu-external D:\vm\Ubuntu-external D:\vm\ubuntu.tar`
  6. 3.
  7. This will create a new WSL distribution named "Ubuntu-external" in the `D:\vm\Ubuntu-external` directory.
  • Check if the distro is listed by WSL using: `wsl -d -v`. The distro name should be listed there, e.g., `Ubuntu-external`.
  • Launch it with: `wsl -d <new_distribution_name>`.
  • Go to the root and you'll see it's somewhere else:
    0.
  1. `root@DESKTOP-3EGT6DL:~#`

This is good for the most part as you could say you are free. Before, you could not go into the root folder and were using a user. Now, you are logged in as root. To find the former root folder, go to `cd ../home/valiantlynx/` and change `valiantlynx` to your own username.

Join the Discussion