Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository "deb [arch=amd64] $(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 | \
  sudo apt-key add -
curl -s -L | \
  sudo tee /etc/apt/sources.list.d/nvidia-container-runtime.list
sudo apt-get update

For Debian distributions (Stretch x86_64):

curl -s -L | \
  sudo apt-key add -
curl -s -L | \
  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
ExecStart=/usr/bin/dockerd --host=fd:// --add-runtime=nvidia=/usr/bin/nvidia-container-runtime
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": []
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


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

This comment has been minimized.

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.


This comment has been minimized.

Copy link
Owner Author

Brainiarc7 commented Jan 12, 2019

Thanks for the update, @vanpelt.


This comment has been minimized.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.