How to Install Singularity on Linux

Singularity

Are you searching for a way to run your app quickly and reliably when shared from one computing environment to another? In this blog post, we will discuss what is known as Singularity, specifically, how to Install Singularity on Linux.

You might be interested in How to enable Amazon S3 default bucket encryption using S3 Console and Top Reasons to Choose AWS as Your Cloud Platform

What do you need to know to get started?

Before jumping to “Singularity” containers let’s start first with what a container is.

A container is defined as a software unit that contains the code of an app and all its dependencies. The goal is to run your app quickly and reliably when shared from one computing environment to another. You can see it as a lightweight alternative to full-machine virtualization.

You can create your own container from an image, i.e., your app’s blueprint (code, dependencies, how to run it).

What is Singularity Container?

Singularity is a container runtime designed specifically for High-Performance Computing HPC, but not only it can also run on NON-supercomputer 😉 … and it is compatible with all other standard container formats (e.g. Docker, OCI).

Singularity has some benefits over other container runtimes. e.g. A Singularity container favors integration with the host by default rather than isolation, while still preserving security restrictions on the container, and providing reproducible images. And it guarantees the contained software and data using crypt-graphically verifiable signatures and encryption when used in cloud environments for data protection.

Another benefit is that Singularity has unprivileged runtime, i.e. no “sudo” required compared to Docker, which is quite important when you want to run your container in HPC.

Read also: How to Create a Password Reset Disk in Windows 10 and How to Install and Use FreeOffice 2021 on Ubuntu

One drawback of Singularity is that “sudo” is still required to build an image, so the best practice is to build the image locally and then ship your container to the HPC to run it.

Installation on Linux

Singularity can be installed on any modern Linux distribution, on bare metal, or inside a Virtual Machine. Nested installations inside containers are not recommended and require the outer container to be run with full privilege.

System Requirements

Singularity requires ~140MiB disk space once compiled and installed.

There are no specific CPU or memory requirements at runtime, though 2GB of RAM is recommended when building from the source.

How to install Singularity

The following commands will install the latest version of Singularity from the GitHub repo master branch to /usr/local. If you have an earlier version of Singularity installed, you should remove it before executing the installation commands below.

$ sudo apt-get update && \
sudo apt-get install \
python \
dh-autoreconf \
build-essential \
libarchive-dev

Step 1: Install system dependencies

You must first install development libraries on your host.

$ sudo apt-get update && sudo apt-get install -y \
build-essential \
libssl-dev \
uuid-dev \
libgpgme11-dev \
squashfs-tools \
libseccomp-dev \
wget \
pkg-config \
git \
cryptsetup

Step 2: Download Singularity from a release

Download Singularity version 3.7.0. After that run the following commands to proceed with the installation.

$ export VERSION=3.7.0 && # adjust this as necessary \
wget https://github.com/hpcng/singularity/releases/download/v${VERSION}/singularity-${VERSION}.tar.gz && \
tar -xzf singularity-${VERSION}.tar.gz && \
cd singularity

3. Compile the Singularity source code

Build Singularity using the following commands:

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

How do you use a singularity container

  1. Create an image file
$ sudo singularity create [image]

2. Bootstrap image

$ sudo singularity bootstrap [image] [definition.def]

3. Run image

$ singularity shell [image]
$ singularity exec [image] [path to executable]
$ singularity run [image]
$ ./image

4. Pull an image

$ singularity pull shub://vsoch/hello-world

Singularity Commands

  • build: Build a container on your user endpoint or build environment.
  • exec: Execute command to your container.
$ singularity exec <image> <cmd>
$ singularity exec
  • inspect: See labels, run and test scripts, and environment variables.
  • pull: pull an image from Docker or Singularity Hub.
$ singularity pull<image>
  • run: Run your image as an executable.
  • shell: Shell into your image.
$ singularity shell <image>

Run Hello World in Singularity container

How to run the hello_world script py in the singularity container

$ sudo singularity build -*-sandbox ubuntu_s docker://ubuntu

A new folder will be created

$ singularity exec ubuntu_s python hello_world.py

You will get an error mentioning that python is not found, so we have to install it first

$ >> python not found

To install python enter as root and use the writable flag to keep all the installed packages in the container otherwise they will be removed when you exit the shell

$ sudo singularity shell -writable ubuntu_s

then run the following commands

$ apt-get -y update
$ apt-get -y install python

Now the container is ready to run the hello world python script.

Another way to build a singularity container is to use the “Recipe” file, which is the image or blueprint of the container.

So open an editor e.g. “vim Singularity.recipe” and past into it the following:

Bootstrap: docker
From: ubuntu:18.04% post section
apt-get -y update
apt-get -y install python% files
hello_world.py /%runscript
python /hello_world.py

Then use the recipe file (image of your container) to build the container:

$ sudo singularity build ubuntu.sim Singularity.recipe

Run the container

$./ubuntu.simg

That’s all about the basics of Singularity. Explore this blog for other topics on Singularity. Kindly drop comments below for any questions.

Leave a Reply

0 Comments
Inline Feedbacks
View all comments