Installing Arch Linux on WSL2

July 2024

Preface

A quick search will result in plenty of solutions on how to install Arch Linux onto WSL2 and most of these solutions work just fine. However, puts on tinfoil hat, you can never trust what someone else’s application/script is doing or they’re plain out-dated. Realistically, I’m an advocate of doing things by hand there is always something to learn in the process, more tools for the toolbox.

WSL has a limited selection of distributions to install. Running the command:

wsl --list --online

will list the officially supported distributions available to install through the WSL binary.

The following is a list of valid distributions that can be installed.
Install using 'wsl.exe --install <Distro>'.

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
Ubuntu-24.04                           Ubuntu 24.04 LTS
OracleLinux_7_9                        Oracle Linux 7.9
OracleLinux_8_7                        Oracle Linux 8.7
OracleLinux_9_1                        Oracle Linux 9.1
openSUSE-Leap-15.5                     openSUSE Leap 15.5
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
SUSE-Linux-Enterprise-15-SP5           SUSE Linux Enterprise 15 SP5
openSUSE-Tumbleweed                    openSUSE Tumbleweed

As of the date of this blog post, Arch Linux is no where to be found on the list. Unofficial distributions must be installed manually or with tools; life can never be simple.

Installing WSL2

Follow the official installation guides by Microsoft:

How to install Linux on Windows with WSL is for Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11. For other earlier versions of Windows use Manual installation steps for older versions of WSL.

Installing Arch onto WSL2

Downloading the Arch Root File System (RootFS)

Once WSL2 is setup on Windows we’ll need to download and prepare the root file system (rootfs). The rootfs contains files and directories critical for system operation.

Arch strives to remain bleeding edge, offering the latest stable versions of most software, and is installed as a minimal base system to be configured by the user to build their ideal environment. GUI configuration utilities are not officially provided, and most system configuration is performed from the shell by editing simple text files.

The Arch Linux team provides daily snapshots of the minimal rootfs on their Gitlab Arch Linux Docker repository (archlinux/archlinux-docker). This saves us from preparing one ourselves and unlike third-party programs that setup Arch Linux onto WSL for you it’ll be the latest image.

I reccomend getting base-YYYYMMDD.X.XXXXXX.tar.zst (minimal package set to define a basic Arch Linux installation) as you can always install what the other releases offer.

Importing the RootFS

WSL makes it simple to import the rootfs we just acquired. The command is below:

wsl --import [DISTRO_NAME] [INSTALL_LOCATION] [ROOTFS_FILE_LOCATION]
DISTRO_NAMEName of the distro being created.
INSTALL_LOCATIONLocation to save the .vhdx (Virtual Hard Disk) file.
ROOTFS_FILE_LOCATIONLocation of the rootfs .tar file.

Setting up Arch Linux

Preliminary

wsl -l -v

Will list all the installed distributions. We should see our newly installed distro on there as well.

wsl --set-default [DISTRO_NAME]

This command is optional (but reccomended). It’ll make the specified distro the default distro. The benefit of this is when ever the WSL command is called it’ll log into that specified distro.

Login as root

We’ll need root level access to setup our Arch install.

wsl -d arch -u root

Configure pacman

Before we can use pacman (Arch Linux’s default package manager) to update and retrieve additional packages we need to initialize and populate the keyring.

pacman-key --init
pacman-key --populate

Install a text editor

Now that pacman is primed we can install packages from the Arch repository. Hold on though we’ve still got some configuration left. We’ll get to installing some useful packages later, but for now we only need a text editor. I prefer vim, but nano is much more forgiving to beginners.

pacman -Syu vim

Create a user

It’s bad juju to use root as the default user, as root has unfettered access the system Create a new user with the following command:

useradd -m -G wheel -s /bin/bash [username]

Set the username to the same as the one we put in /etc/wsl.conf, that way when we launch into the distro from WSL it will auto log into that user.

Command break down

  • -m will create a directory in the /home/ directory for your user.
  • -G will add the the user to the wheel group (will allow the user to use sudo to run root level commands).
  • -s will set the default shell for the user to /bin/bash

Set user password

The user will also need a password and we can change that via:

passwd [USERNAME]

It’ll prompt you to set the password for the specified user.

Access to sudo

Modify the /etc/sudoers file to uncomment the following line:

## Uncomment to allow members of group wheel to execute any command
%wheel ALL=(ALL:ALL) ALL

Create wsl.conf

The wsl.conf file configures settings on a per-distro basis. It’s got more options you can learn about here. In the meantime use your favorite text editor to create the /etc/wsl.conf file. The contents of that file will be:

[boot]
systemd = true

[user]
default = <USERNAME>
USERNAMEThe username you’d like to use. Linux usernames must be lowercase and contain no spaces.

Note: enabling systemd has a noticable delay on initial startup time of the distro.

Configure locale

Edit the /etc/locale.gen file to uncomment your locale. The /etc/locale.conf file may exist and have the line LANG=C.UTF-8 in it, but make sure. If not, create it, make sure to set the contents to your locale:

LANG=en_US.UTF-8

To finalize this step we execute:

locale-gen

Finally we’re done! You can exit the root user, close the window, but WSL will still be running in the background. Simply open your Windows terminal and type:

wsl --shutdown

Installing packages

If you configured wsl.conf properly you’ll be able to launch directly into your distro.

# launches default distro
wsl

# launches into specific distro
wsl -d [DISTRO_NAME]

If you configured /etc/sudoers correctly your user will have access to sudo. This will give you access to installing packages via pacman.

Running the command:

sudo pacman -Syu [PACKAGE_NAME_1] [PACKAGE_NAME_2] [...]

will update the package list, upgrade all packages, and then install [PACKAGE_NAME_1], [PACKAGE_NAME_2], etc. if it wasn’t already installed.

Caveats

man command not working

This rootfs image of Arch disables pacman from extracing man pages when installing a package. The solution is simple and can be found in this blog post I wrote.