Skip to main content Matthias blog

Use Windows Subsystem for Linux for Test Environments

Why WSL
Installing the base system
Import base system to a new distribution

Why WSL

I want to have a separated environment to test a self-hosted Ghost instance. Easiest for me was to use WSL2 on my Windows laptop.

I used WSL2 before to run a Debian environment. And while I would not recommend it generally it was good enough for my use-case.

Installing the base system

To install the base system I use the basic WSL2 command as describe on the Debian wiki

I already enabled the needed Windows features before.

code snippet start

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

code snippet end

So I just updated wsl and set the default to WSL2.

code snippet start

wsl.exe --update
wsl --set-default-version 2

code snippet end

For a fresh start I removed my former Debian instance.

code snippet start

wsl --unregister Debian

code snippet end

To directly install it a new version.

code snippet start

wsl.exe --install -d Debian

code snippet end

Afterwards I only had one Debian WSL2 instance.

code snippet start

wsl --list
Windows Subsystem für Linux-Distributionen:
Debian (Standard)

code snippet end

I further configured the system as described in the Debian wiki and also installed Docker. With that the preparation was finished and the WSL2 instance was ready to be copied.

Import base system to a new distribution

Instances of distributions can be exported to a tar file, which I did.

code snippet start

wsl.exe --export Debian ghost-blog-wsl.tar

code snippet end

To create a new Distribution one can use the import functionality

code snippet start

    --import <Distro> <InstallLocation> <FileName> [Options]
        Imports the specified tar file as a new distribution.
        The filename can be - for standard input.

        Options:
            --version <Version>
                Specifies the version to use for the new distribution.

code snippet end

So I imported the base system export to the new distribution named Ghostblog in the directoy GhostBlog and set the version identifier for this destribution to 1.0.

code snippet start

wsl.exe --import GhostBlog .\GhostBlog .\ghost-blog-wsl.tar --version 1.0

code snippet end

Reading the online help instead of the command line help could have saved me a lot of time and debugging. The version parameter does not define the version of the distribution, but the WSL version used by the distribution. That means that I accidentally overwrote the default WSL version 2 to version 1 again. That broke systemd for example.
So I unregistered and imported the GhostBlog distribution again.

In the end I had two instances of Debian running as WSL2 distributions.

code snippet start

wsl --list
Windows Subsystem für Linux-Distributionen:
Debian (Standard)
GhostBlog

code snippet end

After playing around with Ghost I decided to check out Hugo too. So again I imported the base system export to a new WSL2 distribution.

code snippet start

wsl.exe --import HugoBlog .\HugoBlog .\ghost-blog-wsl.tar

code snippet end

The let to me having the different Debian instances running via WSL2 on my Windows laptop.

code snippet start

wsl --list
Windows Subsystem für Linux-Distributionen:
Debian (Standard)
HugoBlog
GhostBlog

code snippet end