joepferguson avatar

Exporting and Importing WSL Linux Distributions on Windows 10

joepferguson

Published: 13 Jul 2020 › Updated: 13 Jul 2020Exporting and Importing WSL Linux Distributions on Windows 10

Exporting and Importing WSL Linux Distributions on Windows 10

Launch Ubuntu 20.04 in Windows Subsystem for Linux

I've recently spent a lot of dedicated time exploring Windows 10 as a development environment. The best part of this process has been becoming familiar with Windows Subsystem for Linux, or WSL. With WSL 2.0 Microsoft is shipping a complete Linux kernel with Windows 10. Getting started with WSL is a few steps through the Windows Features menu to ensure you have enabled WSL services, then into the Microsoft store to download and install a distribution such as Ubuntu 20.04. If you're familiar with web development on macOS you're likely familiar with installing Homebrew to install command-line tools. Think of WSL as that Homebrew layer but instead of installing macOS binaries you're installing and running Linux
binaries alongside Windows.

Once you have started configuring your WSL environment you can use the Powershell CLI to list the distributions you currently have via wsl -l (also wsl.exe -l):

PS C:\Users\halo\Code> wsl -l
Windows Subsystem for Linux Distributions:
docker-desktop-data (Default)
docker-desktop
Ubuntu-20.04
PS C:\Users\halo\Code>

You can see Ubuntu-20.04 alongside two other distributions that come from Docker Desktop on Windows. Since this existing distro is already customized I want to export it so I can reimport it later. Think of this export as a snapshot of the system or an image of a running virtual machine. To export our distro we'll use the --export flag and provide a distribution name and a filename to use:

wsl --export Ubuntu-20.04 Ubuntu-20.04-07-12-20.tar
winrar a -afzip Ubuntu-20.04-07-12-20.tar.zip Ubuntu-20.04-07-12-20.tar
PS C:\Users\halo\Code>

Compressed WSL Export File size

Note: You may be able to run Compress-Archive -Path DISTRO.tar -DestinationPath DISTRO.tar.zip, however if your WSL export is over 2GB uncompressed (like mine is): this Stack Overflow article explains the issue. This is why I'm using winrar CLI to compress the file to a zip file.

We can check on our compression via dir:

PS C:\Users\halo\Code> dir

    Directory: C:\Users\halo\Code

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         7/12/2020     11:50     3522938880 Ubuntu-20.04-07-12-20.tar
-a----         7/12/2020     12:28     1565740517 Ubuntu-20.04-07-12-20.tar.zip

Normally you won't need to compress this archive unless you want to keep it around for long term storage and backups. You can safely skip this step if you'd like.

Now we're ready to remove our Ubuntu-20.04 distribution. If you have compressed your distro's tar file go ahead and uncompress is os we can import it via the WSL CLI.

wsl --unregister Ubuntu-20.04
Unregistering...
PS C:\Users\halo\Code> wsl -l
Windows Subsystem for Linux Distributions:
docker-desktop-data (Default)
docker-desktop
PS C:\Users\halo\Code>

Now our Ubuntu-20.04 distribution has been removed. If you want to install a clean copy of the Ubuntu 20.04 you can install and launch it from the Microsoft Store

To reimport our previously configured distribution we won't need the Microsoft store. We'll reimport the distro via the WSL command in PowerShell:

wsl.exe --import Ubuntu-20.04 C:\Users\halo\wsl\Ubuntu-20.04 Ubuntu-20.04-07-12-20.tar
PS C:\Users\halo> ubuntu2004.exe config --default-user halo
PS C:\Users\halo> wsl.exe  --list
Windows Subsystem for Linux Distributions:
docker-desktop-data (Default)
docker-desktop
Ubuntu-20.04
PS C:\Users\halo>

The install location parameter is where the virtual hard drive or ext4.vhdx is stored. I'm keeping mine in C:\Users\halo\wsl\<DISTRO>.

PS C:\Users\halo\Code> wsl -l
Windows Subsystem for Linux Distributions:
docker-desktop-data (Default)
docker-desktop
Ubuntu-20.04

You'll note that we also ran the command ubuntu2004.exe config --default-user halo to set our default user to halo, if you skip this step you'll be logged into root when opening the WSL distro or running WSL commands.

The last thing we should do is set our renamed distro to the default WSL:

PS C:\Users\halo> wsl --set-default Ubuntu-20.04
PS C:\Users\halo>

Now when we run wsl bash ... commands Windows will use the Ubuntu-20.04 distro by default.

Happy Linuxing on Windows 10!

Leave Exporting and Importing WSL Linux Distributions on Windows 10 to:

Written by

Joe Ferguson is a PHP developer and community organizer. He is involved with many different technology related initiatives in Memphis including the Memphis PHP User group. He’s been married to his extremely supportive and amazing wife for a really long time and she turned him into a crazy cat man. They live in the Memphis suburbs with their two cats.

Read more #linux posts


Best Posts From joepferguson

We have not curated any of joepferguson's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From joepferguson