Skip to content

Instantly share code, notes, and snippets.

@SomeBottle
Created January 26, 2025 17:43
Show Gist options
  • Select an option

  • Save SomeBottle/4fd86e0000ec552b1dd2bf0c6823af2a to your computer and use it in GitHub Desktop.

Select an option

Save SomeBottle/4fd86e0000ec552b1dd2bf0c6823af2a to your computer and use it in GitHub Desktop.
Alternative to Docker in Google Colab: Apptainer

There have been many discussions under this gist about installing docker in Google Colab: https://gist.github.com/mwufi/6718b30761cd109f9aff04c5144eb885

As a possible alternative, Apptainer works on Google Colab.

You can follow the instruction in the document to install it: https://apptainer.org/docs/admin/latest/installation.html#install-ubuntu-packages

It is possible to convert a docker image to .sif file for Apptainer:

!apptainer build hello.sif docker://hello-world

However, it is worth noting that Google Colab has some restrictions on root user's capabilities, so you must gain full capabilities in a new user namespace created by unshare -r before running Apptainer as root:

!unshare -r apptainer run hello.sif

or just run as a regular user:

# The user in the container will be somebottle
!sudo -u somebottle apptainer run hello.sif

# The user in the container will be root
!sudo -u somebottle apptainer run --fakeroot hello.sif

Additionally, if you want to run a CUDA application inside a container, you need to use flag --nv in the command line, according to https://apptainer.org/docs/user/1.3/gpu.html#requirements

# execute 'nvidia-smi' inside a newly created container
!unshare -r apptainer exec --nv pytorch-gpu.sif nvidia-smi

You may run into a problem like this:

problem_emergence-2025-01-21

That's because Apptainer depends on ldconfig -p to find shared libraries, but NVIDIA libraries haven't been added to it on Google Colab.

Follow these steps to fix it:

  1. Check the LD_LIBRARY_PATH to find nvidia library path:

    !unshare -r env | grep LD_
    # >> LD_LIBRARY_PATH=/usr/lib64-nvidia
  2. Write the nvidia library path into /etc/ld.so.conf.d/:

    !echo "/usr/lib64-nvidia" >> /etc/ld.so.conf.d/nvidia.conf
  3. Refresh library cache:

    !ldconfig

Now you'll be able to find nvidia library files in ldconfig -p, and can execute nvidia-smi command normally:

nvidia-smi_success-2025-01-22


I hope this helps!

Original post: https://github.com/cat-note/bottleofcat/blob/main/Containerization/GPUApptainerOnGoogleColab.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment