Using Host libraries: GPU drivers and OpenMPI BTLs


Much of the GPU portion of this tutorial is deprecated by the --nv option that automatically binds host system driver libraries into your container at runtime. See the exec command for an example.

Singularity does a fantastic job of isolating you from the host so you don’t have to muck about with LD_LIBRARY_PATH, you just get exactly the library versions you want. However, in some situations you need to use library versions that match host exactly. Two common ones are NVIDIA gpu driver user-space libraries, and OpenMPI transport drivers for high performance networking. There are many ways to solve these problems. Some people build a container and copy the version of the libs (installed on the host) into the container.

What We will learn today

This document describes how to use a bind mount, symlinks and ldconfig so that when the host libraries are updated the container does not need to be rebuilt.

Note this tutorial is tested with Singularity commit 945c6ee343a1e6101e22396a90dfdb5944f442b6, which is part of the (current) development branch, and thus it should work with version 2.3 when that is released. The version of OpenMPI used is 2.1.0 (versions above 2.1 should work).


In our environment we run CentOS 7 hosts with:

  1. slurm located on /opt/slurm-<version> and the slurm user slurm

  2. Mellanox network cards with drivers installed to /opt/mellanox ( Specifically we run a RoCEv1 network for Lustre and MPI communications)

  3. NVIDIA GPUs with drivers installed to /lib64

  4. OpenMPI (by default) for MPI processes

Creating your image

Since we are building an ubuntu image, it may be easier to create an ubuntu VM to create the image. Alternatively you can follow the recipe here.

Use the following def file to create the image.

Bootstrap: debootstrap


OSVersion: xenial

Include: apt


apt install -y software-properties-common

apt-add-repository -y universe

apt update

apt install -y wget

mkdir /usr/local/openmpi || echo "Directory exists"

mkdir /opt/mellanox || echo "Directory exists"

mkdir /all_hostlibs || echo "Directory exists"

mkdir /desired_hostlibs || echo "Directory exists"

mkdir /etc/libibverbs.d || echo "Directory exists"

echo "driver mlx4" > /etc/libibverbs.d/mlx4.driver

echo "driver mlx5" > /etc/libibverbs.d/mlx5.driver

adduser slurm || echo "User exists"

wget -O /tmp/desired_hostlibs.txt

cat /tmp/desired_hostlibs.txt | xargs -I{} ln -s /all_hostlibs/{} /desired_hostlibs/{}

rm /tmp/desired_hostlibs.txt

The mysterious wget line gets a list of all the libraries that the CentOS host has in /lib64 that we think its safe to use in the container. Specifically these are things like nvidia drivers.
















Also note:

  1. in hostlibs.def we create a slurm user. Obviously if your SlurmUser is different you should change this name.

  2. We make directories for /opt and /usr/local/openmpi. We’re going to bindmount these from the host so we get all the bits of OpenMPI and Mellanox and Slurm that we need.

Executing your image

On our system we do:




srun  singularity exec -B /usr/local/openmpi:/usr/local/openmpi -B /opt:/opt -B /lib64:/all_hostlibs hostlibs.img <path to binary>

Building an Ubuntu image on a RHEL host

This recipe describes how to build an Ubuntu image using Singularity on a RHEL compatible host.


This tutorial is intended for Singularity release 2.1.2, and reflects standards for that version.

In order to do this, you will need to first install the ‘debootstrap’ package onto your host. Then, you will create a definition file that will describe how to build your Ubuntu image. Finally, you will build the image using the Singularity commands ‘create’ and bootstrap.


This recipe assumes that you have already installed Singularity on your computer. If you have not, follow the instructions here to install. After Singularity is installed on your computer, you will need to install the ‘debootstrap’ package. The ‘debootstrap’ package is a tool that will allow you to create Debian-based distributions such as Ubuntu. In order to install ‘debootstrap’, you will also need to install ‘epel-release’. You will need to download the appropriate RPM from the EPEL website. Make sure you download the correct version of the RPM for your release.

# First, wget the appropriate RPM from the EPEL website (

# In this example we used RHEL 7, so we downloaded epel-release-latest-7.noarch.rpm

$ wget

# Then, install your epel-release RPM

$ sudo yum install epel-release-latest-7.noarch.rpm

# Finally, install debootstrap

$ sudo yum install debootstrap

Creating the Definition File

You will need to create a definition file to describe how to build your Ubuntu image. Definition files are plain text files that contain Singularity keywords. By using certain Singularity keywords, you can specify how you want your image to be built. The extension ‘.def’ is recommended for user clarity. Below is a definition file for a minimal Ubuntu image:

DistType "debian"

MirrorURL ""

OSVersion "trusty"




The following keywords were used in this definition file:
  • DistType: DistType specifies the distribution type of your intended operating system. Because we are trying to build an Ubuntu image, the type “debian” was chosen.

  • MirrorURL: The MirrorURL specifies the download link for your intended operating system. The Ubuntu archive website is a great mirror link to use if you are building an Ubuntu image.

  • OSVersion: The OSVersion is used to specify which release of a Debian-based distribution you are using. In this example we chose “trusty” to specify that we wanted to build an Ubuntu 14.04 (Trusty Tahr) image.

  • Setup: Setup creates some of the base files and components for an OS and is highly recommended to be included in your definition file.

  • Bootstrap: Bootstrap will call apt-get to install the appropriate package to build your OS.

  • Cleanup: Cleanup will remove temporary files from the installation.

While this definition file is enough to create a working Ubuntu image, you may want increased customization of your image. There are several Singularity keywords that allow the user to do things such as install packages or files. Some of these keywords are used in the example below:

DistType "debian"

MirrorURL ""

OSVersion "trusty"



InstallPkgs python

InstallPkgs wget

RunCmd wget

RunCmd python

RunCmd ln -s /usr/local/bin/pip /usr/bin/pip

RunCmd pip install --upgrade


Before going over exactly what image this definition file specifies, the remaining Singularity keywords should be introduced.

  • InstallPkgs: InstallPkgs allows you to install any packages that you want on your newly created image.

  • InstallFile: InstallFile allows you to install files from your computer to the image.

  • RunCmd: RunCmd allows you to run a command from within the new image during the installation.

  • RunScript: RunScript adds a new line to the runscript invoked by the Singularity subcommand ‘run’. See the run page for more information.

Now that you are familiar with all of the Singularity keywords, we can take a closer look at the example above. As with the previous example, an Ubuntu image is created with the specified DistType, MirrorURL, and OSVersion. However, after Setup and Bootstrap, we used the InstallPkgs keyword to install ‘python’ and ‘wget’. Then we used the RunCmd keyword to first download the pip installation wheel, and then to install ‘pip’. Subsequently, we also used RunCmd to pip install TensorFlow. Thus, we have created a definition file that will install ‘python’, ‘pip’, and ‘Tensorflow’ onto the new image.

Creating your image

Once you have created your definition file, you will be ready to actually create your image. You will do this by utilizing the Singularity ‘create’ and ‘bootstrap’ subcommands. The process for doing this can be seen below:


We have saved our definition file as “ubuntu.def”

# First we will create an empty image container called ubuntu.img

$ sudo singularity create ubuntu.img

Creating a sparse image with a maximum size of 1024MiB...

INFO   : Using given image size of 1024

Formatting image (/sbin/mkfs.ext3)

Done. Image can be found at: ubuntu.img

# Next we will bootstrap the image with the operating system specified in our definition file

$ sudo singularity bootstrap ubuntu.img ubuntu.def

W: Cannot check Release signature; keyring file not available /usr/share/keyrings/ubuntu-archive-keyring.gpg

I: Retrieving Release

I: Retrieving Packages

I: Validating Packages

I: Resolving dependencies of required packages...

I: Resolving dependencies of base packages...

I: Found additional base dependencies: gcc-4.8-base gnupg gpgv libapt-pkg4.12 libreadline6 libstdc++6 libusb-0.1-4 readline-common ubuntu-keyring

I: Checking component main on

I: Retrieving adduser 3.113+nmu3ubuntu3

I: Validating adduser 3.113+nmu3ubuntu3

I: Retrieving apt 1.0.1ubuntu2

I: Validating apt 1.0.1ubuntu2


Downloading pip-8.1.2-py2.py3-none-any.whl (1.2MB)

100% |################################| 1.2MB 1.1MB/s

Collecting setuptools

Downloading setuptools-24.0.2-py2.py3-none-any.whl (441kB)

100% |################################| 450kB 2.7MB/s

Collecting wheel

Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)

100% |################################| 71kB 9.9MB/s

Installing collected packages: pip, setuptools, wheel

Successfully installed pip-8.1.2 setuptools-24.0.2 wheel-0.29.0

At this point, you have successfully created an Ubuntu image with 'python', 'pip', and 'TensorFlow' on your RHEL computer.

Tips and Tricks

Here are some tips and tricks that you can use to create more efficient definition files:

Use here documents with RunCmd

Using here documents with conjunction with RunCmd can be a great way to decrease the number of RunCmd keywords that you need to include in your definition file. For example, we can substitute a here document into the previous example:

DistType "debian"

MirrorURL ""

OSVersion "trusty"



InstallPkgs python

InstallPkgs wget

RunCmd /bin/sh <<EOF



ln -s /usr/local/bin/pip /usr/bin/pip

pip install --upgrade



As you can see, using a here document allowed us to decrease the number of RunCmd keywords from 4 to 1. This can be useful when your definition file has a lot of RunCmd keywords and can also ease copying and pasting command line recipes from other sources.

Use InstallPkgs with multiple packages

The InstallPkgs keyword is able to install multiple packages with a single keyword. Thus, another way you can increase the efficiency of your code is to use a single InstallPkgs keyword to install multiple packages, as seen below:

DistType "debian"

MirrorURL ""

OSVersion "trusty"



InstallPkgs python wget

RunCmd /bin/sh <<EOF



ln -s /usr/local/bin/pip /usr/bin/pip

pip install --upgrade



Using a single InstallPkgs keyword to install both ‘python’ and ‘wget’ allowed to decrease the number of InstallPkgs keywords we had to use in our definition file. This slimmed down our definition file and helped reduce clutter.