Skip to content

Instantly share code, notes, and snippets.

@Brainiarc7
Last active September 17, 2022 04:51
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save Brainiarc7/a8ab5f89494d053003454efc3be2d2ef to your computer and use it in GitHub Desktop.
Save Brainiarc7/a8ab5f89494d053003454efc3be2d2ef to your computer and use it in GitHub Desktop.
How to correctly install nvidia-docker2 on Ubuntu 16.04LTS

How to install NVIDIA Docker 2 package on Ubuntu and Debian:

If you came to this result (from Google or elsewhere) after realizing that Nvidia-docker's entry on this subject does not result in a working installation, here are the basic steps needed to install this package correctly:

For starters, ensure that you've installed the latest Docker Community edition by following the steps below:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce
sudo service docker restart

Then, proceed to adding the required repositories and installing the needed software:

Start with nvidia-docker-runtime:

For Ubuntu distributions (Xenial x86_64):

First, if you have older nvidia-docker installations, purge the installation and all associated GPU containers:

docker volume ls -q -f driver=nvidia-docker | xargs -r -I{} -n1 docker ps -q -a -f volume={} | xargs -r docker rm -f
sudo apt-get purge -y nvidia-docker

Then proceed:

curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | \
  sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-container-runtime/ubuntu16.04/amd64/nvidia-container-runtime.list | \
  sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
sudo apt-get update

For Debian distributions (Stretch x86_64):

curl -s -L https://nvidia.github.io/nvidia-container-runtime/gpgkey | \
  sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-container-runtime/debian9/amd64/nvidia-container-runtime.list | \
  sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
sudo apt-get update

Install the nvidia-container-runtime package:

sudo apt-get install nvidia-container-runtime

Followed by nvidia-docker2:

sudo apt-get install nvidia-docker2

Docker Engine setup:

To register the nvidia runtime, use either method below on Ubuntu 16.04LTS+. It is recommended that you use the systemd drop-in file method to prevent docker upgrades from directly overwriting the docker daemon configuration file.

(a).Systemd drop-in file (Recommended approach, but see notes below):

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/override.conf <<EOF
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --host=fd:// --add-runtime=nvidia=/usr/bin/nvidia-container-runtime
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

(b). Daemon configuration file (Use if you're on a distribution such as Devuan, without a systemd dependency):

sudo tee /etc/docker/daemon.json <<EOF
{
    "runtimes": {
        "nvidia": {
            "path": "/usr/bin/nvidia-container-runtime",
            "runtimeArgs": []
        }
    }
}
EOF
sudo pkill -SIGHUP dockerd

Basic usage:

nvidia-docker registers a new container runtime to the Docker daemon. You must select the nvidia runtime when using docker run, as illustrated below (with nvidia-smi):

sudo docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi

Notes:

If the systemd unit method fails, skip straight to the configuration file method. The result is the same.

If you do not want to run Docker as a root user, the smart approach is as follows:

Add the docker group if it doesn't already exist:

sudo groupadd docker

Add the connected user "$USER" to the docker group. Change the user name to match your preferred user if you do not want to use your current user:

sudo gpasswd -a $USER docker

Either do a newgrp docker or log out/in to activate the changes to groups.

You can use:

docker run hello-world

To check if you can run docker without sudo.

For the snippet above, you can now run it as:

docker run -u $(USER) --runtime=nvidia --rm nvidia/cuda nvidia-smi

Or directly IF you're logged in as a user that's a member of the docker group:

docker run --runtime=nvidia --rm nvidia/cuda nvidia-smi
@vanpelt
Copy link

vanpelt commented Dec 16, 2018

I had to change /usr/bin/dockerd --host=fd:// --add-runtime=nvidia=/usr/bin/nvidia-container-runtime to /usr/bin/dockerd --host=unix:// --add-runtime=nvidia=/usr/bin/nvidia-container-runtime for it to work for me.

@Brainiarc7
Copy link
Author

Thanks for the update, @vanpelt.

@HTLife
Copy link

HTLife commented Mar 1, 2019

nvidia-docker2 seems to have dependency issue with docker.
to solve this:
sudo apt-get install docker-ce=5:18.09.2~3-0~ubuntu-xenial docker-ce-cli=5:18.09.2~3-0~ubuntu-xenial containerd.io

@Micrified
Copy link

Micrified commented Apr 21, 2020

Following these instructions leaves me with:

E: Unable to locate package nvidia-docker2

After sudo apt-get install nvidia-docker2. I am unable to proceed further (Ubuntu 16.04 Xenial)

@abaybektursun
Copy link

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