Setup Tensorflow GPU 1.15/2.2 on Ubuntu [2020]

Requirements

  • Supported GPU for deep learning training (Nvidia GPU)
  • CUDA toolkit
  • CuDNN
  • Conda (optional)

CUDA and CuDNN Setup

CUDA and CuDNN are required libraries for Tensorflow gpu to work. Here I will use the apt install method.

Each Tensorflow version may support specific CUDA and CuDNN versions. If you have installed CUDA for older version of TF before, you will want to remove it before installing the correct version for TF 1.15.0

sudo apt-get purge cuda*

Install on Ubuntu 18.04

# Instruction reference: https://www.tensorflow.org/install/gpu
# Add NVIDIA package repositories
cd ~/Downloads
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-repo-ubuntu1804_10.1.243-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo dpkg -i cuda-repo-ubuntu1804_10.1.243-1_amd64.deb
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-450
# 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-1 libcudnn7=7.6.5.32-1+cuda10.1 libcudnn7-dev=7.6.5.32-1+cuda10.1

Install on Ubuntu 16.04

# Instruction reference: https://www.tensorflow.org/install/gpu
# Add NVIDIA package repositories
# Add HTTPS support for apt-key
sudo apt-get install gnupg-curl
cd ~/Downloads
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_10.1.243-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo dpkg -i cuda-repo-ubuntu1604_10.1.243-1_amd64.deb
sudo apt-get update
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
sudo apt install ./nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb
sudo apt-get update
# Install NVIDIA driver
# Issue with driver install requires creating /usr/lib/nvidia
sudo mkdir /usr/lib/nvidia
sudo apt-get install --no-install-recommends nvidia-418
# 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-1 libcudnn7=7.6.4.38-1+cuda10.1 libcudnn7-dev=7.6.4.38-1+cuda10.1

Add CUDA path to ~/.bashrc

echo 'export PATH=/usr/local/cuda/bin${PATH:+:${PATH}}' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashrc
source ~/.bashrc

# Check CUDA version
nvcc --version

Install Tensorflow

If CUDA and CuDNN are installed without any problem from the previous step, now we’re ready to install Tensorflow.

I experimented on both setting up on native and virtual environment. For training, I recommend to use virtual environment to avoid outdated required packages. If you’re running on Ubuntu 16.04, you should really use virtual environment to avoid the packages update headache.

Conda (optional)

Download a miniconda: https://docs.conda.io/en/latest/miniconda.html#linux-installers
Conda installation instruction: https://conda.io/projects/conda/en/latest/user-guide/install/linux.html

Personally, I would install at least Python 3.6 because the latest Tensorflow Object Detection API supports Python 3.6. This is an sample of creating virtual environment:

# Create conda environment name Tensorflow-gpu with python3.6 default
conda --create Tensorflow-gpu python=3.6

# Check conda environment list
conda env list

# Activate Tensorflow-gpu
conda activate Tensorflow-gpu

Tensorflow

If you choose to work with virtual environment, activate your environment first. Install one of the tensorflow versions you want with the command below.

# Install tensorflow gpu 1.15.0
pip install tensorflow-gpu==1.15

# Install tensorflow gpu 2.2
pip install tensorflow

For native environment: If system does not have pip default to python3, use pip3 install instead. Otherwise, the installation may go to Python 2.

After installation, check tensorflow version.

# Go to python from terminal

import tensorflow as tf
print(tf.__version__)
__

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s