Skip to content

Instantly share code, notes, and snippets.

@fdb
Last active December 3, 2019 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdb/f6267c7a152d9272a5c8914546e6d98e to your computer and use it in GitHub Desktop.
Save fdb/f6267c7a152d9272a5c8914546e6d98e to your computer and use it in GitHub Desktop.
Setup Tensorflow GPU using Docker on Ubuntu 18.04
#!/bin/bash
# This script is used to setup a working Tensorflow GPU installation using Docker.
# It's only tested on Ubuntu 18.04.
# When all is done, run the following command to get a Bash shell with Tensorflow:
# docker run --gpus all -u $(id -u):$(id -g) -v $PWD:/root -it --rm tensorflow/tensorflow:latest-gpu-py3 bash
# Setup Docker
# Instructions from https://docs.docker.com/install/linux/docker-ce/ubuntu/
# Update apt
sudo apt-get update && sudo apt-get upgrade
# Install packages to allow apt to use a repository over HTTPS
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
# Add Docker’s official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# Add Docker's stable repository
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
# Update apt again
sudo apt-get update
# Install Docker Engine
sudo apt-get install docker-ce docker-ce-cli containerd.io
# Test it out
sudo docker run hello-world
# Enable current user to run Docker
sudo usermod -aG docker $USER
newgrp docker
# NVIDIA Docker Container Runtime
# Instructions from https://github.com/NVIDIA/nvidia-docker
# Add NVIDIA repository
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
# Install container runtime
sudo apt-get update && sudo apt-get install -y nvidia-container-toolkit
sudo systemctl restart docker
# Test it out
docker run --gpus all nvidia/cuda:9.0-base nvidia-smi
# Tensorflow
# Instructions from https://www.tensorflow.org/install/docker
docker run --gpus all -it --rm tensorflow/tensorflow:latest-gpu-py3 python -c \
"import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment