Skip to content

Software Engineer ·

Any distro with WSL

In my recent job, I was forced to use Windows (Yeah, I know). So, I needed to find a way to use a Unix-based distro. Some distros are available in the Windows store, but I wanted to use Arch, which is not officially maintained. So, I started digging into how to do that myself.

In this article, I will walk you through step how to use any Docker (or container) image as WSL (Windows Subsystem for Linux), not only Arch but other distros as well. Let’s focus on container images available in the Docker Hub.

First things first, we need Docker to pull our base image. I’m going to install Ubuntu and install Docker in WSL.

I find this method cleaner than installing Docker Desktop. We can delete this WSL later.

Let’s assume you have WSL installed and ensure it’s the latest version by running wsl --update.

Terminal running wsl --update, which says the latest WSL is installed, then wsl --version listing WSL version 2.1.5.0.

After that, install Ubuntu with the WSL command wsl --install -d ubuntu or another Ubuntu variant by listing available distros using the command wsl --list --online.

Output of wsl --list --online, listing installable distros such as Ubuntu, Debian, kali-linux, Ubuntu-24.04, Oracle Linux and openSUSE.

wsl --install -d Ubuntu-24.04 finishing, with the prompt to create a UNIX user named demo and the Ubuntu 24.04 welcome banner.

After finishing installing Ubuntu, you will be prompted to register a username and password.

Next, we will install Docker with the script from Docker Install GitHub Repository. This can take a couple of minutes.

curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh

The Docker install script starting in Ubuntu: a notice that WSL was detected, then a sudo password prompt after curl and sh get-docker.sh.

After Docker is installed, run the post-install script so we can run Docker without typing sudo.

Docker Post-Install Documentation

Post-install steps (usermod -aG docker, newgrp docker), then docker run hello-world pulls the image and prints Hello from Docker!

Let’s test by running the hello-world image. It should work like this picture:

Now, we can pull Arch Linux and then export it to a tar file so we can use it with WSL.

docker run -it --name arch archlinux sh -c "exit"
docker export arch > /tmp/arch.tar

docker run pulls the archlinux image, then docker export arch > /tmp/arch.tar saves the container as a tar file.

Navigate to \\wsl.localhost\Ubuntu-24.04\tmp to see the arch.tar file we exported.

File Explorer open on the tmp folder of Ubuntu-24.04 under wsl.localhost, showing the exported arch.tar file of about 435 MB.

Create a directory where you want to store WSL. Mine is C:\Users\LoneExile\WSL.

PowerShell running mkdir arch-demo inside the WSL folder of the Windows user profile, which creates the new arch-demo directory.

Now we can import it.

wsl --import arch-demo C:\Users\<YOUR_USER>\WSL\arch-demo\ \\wsl.localhost\Ubuntu-24.04\tmp\arch.tar

wsl --import arch-demo with an install folder and the path to arch.tar, ending with the message that the operation completed successfully.

wsl --list

You can see it in the list now 🎉

wsl --list now showing Ubuntu (Default), Ubuntu-24.04, archlinux and the newly imported arch-demo distro.

Before we start using it, we need to initialize the pacman keyring.

wsl -d arch-demo

The command pacman-key --init in Arch Linux is used to initialize the pacman keyring. The keyring is a collection of cryptographic keys used by pacman, the package manager in Arch Linux, to verify the integrity and authenticity of packages.

pacman-key --init

wsl -d arch-demo opens a root shell, where pacman-key --init generates the pacman master key and updates the trust database.

Next, add the username and password that you want to use. Mine is le.

useradd -m -G wheel le
passwd le
pacman -Syyu # update package
pacman -Syu sudo vim git curl # install packages
EDITOR=vim visudo

Creating the user le in the wheel group and setting its password, then pacman -Syyu starts a full upgrade of 83 packages.

Then uncomment this line %wheel ALL=(ALL:ALL) ALL.

This line allows all users in the wheel group to execute any command as any user or group using sudo.

The sudoers file open in vim, with the cursor on the commented line # %wheel ALL=(ALL:ALL) ALL that needs its comment mark removed.

Now we can exit and then log in to the user we created.

Logging in as user le with wsl.exe -u le -d arch-demo, then sudo vim /etc/wsl.conf asking for the sudo password.

Now edit this file /etc/wsl.conf.

  • systemd=true: This setting enables the use of systemd as the init system for your WSL distribution.
  • generateResolvConf = false: This setting prevents WSL from automatically generating the resolv.conf file.
  • default=<YOUR_USER>: This setting specifies the default user that will be logged in when you start a WSL session.
sudo vim /etc/wsl.conf
[boot]
systemd=true

[network]
generateResolvConf = false

[user]
default=<YOUR_USER>

The /etc/wsl.conf file in vim with systemd=true, generateResolvConf = false and a user section with default=username.

Then shut down your WSL.

Edit the .wslconfig file at C:\Users\<YOUR_USER>\.wslconfig. (Reference)

# Settings apply across all Linux distros running on WSL 2
[wsl2]

# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=32GB

# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB

wsl --shutdown, then Notepad opening the .wslconfig file, which sets memory=32GB and swap=8GB under a wsl2 section.

Log back into the Arch Linux WSL with the command wsl ~ -d archlinux -u le.

Installing archlinux-keyring is important to ensure that your system can verify and trust the packages and updates you download from the Arch Linux repositories.

Test install with the neofetch package:

sudo pacman -S archlinux-keyring
sudo pacman -S neofetch

Output of neofetch on Arch Linux running on Windows 10, with the Arch logo, the WSL2 kernel and 129 pacman packages.

What if we want a GUI application? Let’s try!

pacman -Syu gimp

Running gimp in the Arch shell after installing it fails with Cannot open display, plus warnings about missing libraries.

If it doesn’t work, here is the fix:

https://superuser.com/questions/1617298/wsl-2-running-ubuntu-x-server-cant-open-display/1834709#1834709

rmdir /tmp/.X11-unix && ln -s /mnt/wslg/.X11-unix /tmp/.X11-unix

Then run it again.

After the rmdir and ln -s fix for /tmp/.X11-unix, running gimp opens its window on the Windows desktop.

But you need to run that command every time WSL restarts. We can automate it with systemd.

sudo vim /etc/systemd/system/wslg.service
[Unit]
Description=symlink /tmp/.X11-unix
After=systemd-tmpfiles-setup.service

[Service]
Type=oneshot
ExecStart=rmdir /tmp/.X11-unix
ExecStart=ln -s /mnt/wslg/.X11-unix /tmp/

[Install]
WantedBy=sysinit.target

The wslg.service systemd unit in vim, with Unit, Service and Install sections that recreate the /tmp/.X11-unix symlink on boot.

systemctl enable wslg

We can link Windows commands to launch applications in Arch Linux as well. Here, I link explorer.exe to Arch Linux as open.

sudo ln -s "/mnt/c/Windows/explorer.exe" "/usr/local/bin/open"

Linking explorer.exe to /usr/local/bin/open with ln -s, then open . launches Windows File Explorer in the Arch home folder of user le.

Finally, I can use my Neovim and start working now 🚀

Neovim start screen with an ASCII art face and a menu: Find file, New file, Recently used files, Configuration, Quit Neovim.

If you have any questions, please leave a comment below.

Comments

Get new posts by email

An email when I publish something new.

Unsubscribe any time. Read the privacy policy.