Skip to content

Instantly share code, notes, and snippets.

@ikbelkirasan
Created April 21, 2019 22:37
Show Gist options
  • Save ikbelkirasan/bb8e9470b60e820c8c156a4c1b49f434 to your computer and use it in GitHub Desktop.
Save ikbelkirasan/bb8e9470b60e820c8c156a4c1b49f434 to your computer and use it in GitHub Desktop.
Install Tensorflow with GPU support

Install Tensorflow with GPU support

To install tensorflow, you can either install it with pip (or pip3) or with Docker.

Install using pip

# Install using pip (for python3)
pip3 install --user tensorflow-gpu

Install using Docker

# Tensorflow image with GPU support (python3)
docker pull tensorflow/tensorflow:latest-gpu-py3

Install CUDA 10 on Ubuntu 18.04

# Add NVIDIA package repositories
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804_10.0.130-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo apt-get update
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
sudo apt install ./nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
sudo apt-get update

# Install NVIDIA driver
sudo apt-get install --no-install-recommends nvidia-driver-410
# Reboot. Check that GPUs are visible using the command: nvidia-smi

# Install development and runtime libraries (~4GB)
sudo apt-get install --no-install-recommends \
    cuda-10-0 \
    libcudnn7=7.4.1.5-1+cuda10.0  \
    libcudnn7-dev=7.4.1.5-1+cuda10.0


# Note: Make sure that you're running nvidia-driver-410 in 'Additional Drivers on Ubuntu'

# Install TensorRT. Requires that libcudnn7 is installed above.
sudo apt-get update && \
        sudo apt-get install nvinfer-runtime-trt-repo-ubuntu1804-5.0.2-ga-cuda10.0 \
        && sudo apt-get update \
        && sudo apt-get install -y --no-install-recommends libnvinfer-dev=5.0.2-1+cuda10.0

Install nvidia-docker

# If you have nvidia-docker 1.0 installed: we need to remove it and all existing 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

# Add the package repositories
curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | \
  sudo apt-key add -
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | \
  sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update

# Install nvidia-docker2 and reload the Docker daemon configuration
sudo apt-get install -y nvidia-docker2
sudo pkill -SIGHUP dockerd

# Test nvidia-smi with the latest official CUDA image
docker run --runtime=nvidia --rm nvidia/cuda:9.0-base nvidia-smi

Test your code

# Runs `your_script.py` from the current working directory in a Docker container

docker run \
		-it \
		--rm \
		-u 1000:1000 \
		-v $(PWD):/app \
		tensorflow/tensorflow:latest-gpu-py3 \
	python3 /app/your_script.py