Using Singularity on Windows with WSL2

Previously on this blog, my colleagues Carlos and Eoin have extolled the many virtues of Singularity, which I will not repeat here. Instead, I’d like to talk about a rather interesting subject that was unexpectedly thrust upon me when my faithful Linux laptop started to show the early warning signs of critical existence failure: is there a good way to run a Singularity container on a pure Windows machine? It turns out that, with version 2 of the Windows Subsystem for Linux (WSL), there is.

Installing WSL2 and a Linux distribution

First, we’ll need to set up WSL2. If you’ve already done this, you can jump to the fun part. If you’re on the Windows Insiders Program and have installed the latest preview build, you can simply use the new wsl install command, which will install WSL2 and a default Ubuntu distribution. To do this, in a terminal with administrator privileges, simply run:

wsl --install

You can install different distributions using:

wsl --install -d <Distribution Name>

You can see the names of available distributions by running:

wsl --list --online

If you’re not using a preview build (perhaps because you like your system to just work, thank you very much) you’ll have to enable and install WSL2 the old fashioned way. First, we need to enable some system settings that are required for WSL2. We can do this the GUI way or the terminal way – here I’ll show you both.

To enable WSL2 features through the GUI, open ‘Turn windows features on or off’ from the search bar. This will open a list of options for you to enable or disable. Ensure that the following options are checked:

  • Virtual Machine Platform
  • Windows Subsystem for Linux
Linus forgive me.
Enabling WSL2 features. You don’t need the Windows Hypervisor Platform, but it’s an option if you want to let third-party tools like VirtualBox use Windows’ HyperV platform.

You can also enable both of these options by running the following PowerShell commands as Administrator. To enable Virtual Machine Platform:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

To enable the Windows Subsystem for Linux:

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

Now we have what we need to install WSL2. Before going any further, reboot your system.

The actual installation process is easy. Simply download the latest Linux kernel update from Microsoft and follow the installer (you’ll be prompted to run with Administrator privileges). Once it’s finished, WSL2 is installed and ready to go!

It’s worth pointing out that WSL2 does not replace WSL; in fact, the two can run side by side, and you can choose which version is associated to each Linux distribution you install. To set WSL2 as the default version, you can run the following in PowerShell as Administrator:

wsl --set-default-version 2

Finally, we need to install our first Linux distribution. Here we’ll use the default Ubuntu 20.04 from the Microsoft store, but there are plenty of others to experiment with. Simply open the Microsoft store, search for ‘Ubuntu’, and select the version you’d like to install.

Here we go again.
Here’s one I prepared earlier.

With Ubuntu installed, you’re ready to go. You can start Ubuntu from the start menu, or by running wsl from a terminal. To see the names of all your installed Linux distributions, you can run:

wsl --list

To set a distribution as the default for WSL, run:

wsl --setdefault <DistributionName>

And to run a specific distribution using WSL, you can use:

wsl --distribution <DistributionName>

The first time you run a distribution you’ll be prompted to create a new user and password. These are not in any way related to your Windows user(s), and will be needed to run ‘sudo’ within the WSL.

This is where the fun begins!

Installing Singularity in WSL2

Now we’re finally ready to install Singularity on our Windows computer. Fortunately, the process is the same as on any Linux machine, and mostly consists of installing dependencies, as is tradition. We’ll do this using Ubuntu, but the process is similar for other distributions.

First, system dependencies:

sudo apt-get update &amp;&amp; sudo apt-get install -y \
    build-essential \
    libssl-dev \
    uuid-dev \
    libgpgme11-dev \
    squashfs-tools \
    libseccomp-dev \
    pkg-config

Next, install Go. Change VERSION to whatever’s currently recommended by Singularity:

export VERSION=1.13 OS=linux ARCH=amd64 && \
wget https://dl.google.com/go/go$VERSION.$OS-$ARCH.tar.gz && \
sudo tar -C /usr/local -xzvf go$VERSION.$OS-$ARCH.tar.gz && \
rm go$VERSION.$OS-$ARCH.tar.gz 

Add Go to your PATH;

echo 'export PATH=/usr/local/go/bin:$PATH' >> ~/.bashrc && \
  source ~/.bashrc

Download the Singularity source. Again, change VERSION to whatever you want to install:

export VERSION=3.5.2 && \
    wget https://github.com/sylabs/singularity/releases/download/v${VERSION}/singularity-${VERSION}.tar.gz && \
    tar -xzf singularity-${VERSION}.tar.gz && \
    cd singularity

Finally, as root, install Singularity:

./mconfig && \
    make -C builddir && \
    sudo make -C builddir install

You should now be able to run singularity in your WSL terminal!

Look mum, no VirtualBox!

Finally, no tutorial would be complete without some sort of hello world. Try building a container from a premade Docker image:

singularity pull docker://godlovedc/lolcow

If everything is working properly, the cow should talk to you:

The most erudite part of this post.

Now you’re ready to work with Singularity containers on Windows. One feature to be aware of is the ability to control the resources available to WSL. Virtualisation is hungry work, and WSL will happily gobble up whatever resources are available to it. If WSL is using more than you would like, you can create the %UserProfile%\.wslconfig file in Windows and set your preferred options. For details about configuring WSL and to see available options, check the documentation. For example, to restrict WSL to 8GB memory and 4 virtual processors:

[wsl2]
memory=8GB
processors=4

Now you’re ready to work with Singularity containers on Windows!

Author