In this tutorial, I will teach you how to deploy the Tensorflow environment or the Tensorflow-GPU environment, including installation of CUDA toolkit and cuDNN (Nvidia graphics cards that support CUDA compute capability 3.5 or higher).

Software requirements

Anaconda virtual environment

Why do we need a virtual environment?

It is much easier to reuse or regenerate different workspaces to meet your coding requirements and to install python packages very quickly. You can also backup and recover your favorite environment in another machine in just one command.

Install Anaconda

Visit the Download page and select your system platform and arch to start downloading the installer.

I highly recommend enabling the option called “Add Anaconda to my PATH environment variable” in the Advanced Options during installation because it gives you the power to access the Anaconda environment from the command line prompt.

You can leave everything by default during installation, except the option above and installation of visual studio code in the final step.

Create an Anaconda environment

After installation, you can choose to either launch the Anaconda navigator GUI application or access Anaconda from the command line prompt. In this example, I will introduce more about the command line prompt.

Create a new environment

# Python 3.7 is only available to tensorflow >= 1.13.1
conda create -n yourenvname python=3.6 # You need to specify which python you want to use

Enter the new environment

source activate yourenvname # For the Anaconda Prompt in Windows, use "activate yourenvname" instead

List installed packages

conda list

Install packages with Conda or Pip

conda install packagename
pip install packagename

Upgrade packages with Conda or Pip

conda update all # Upgrade all packages. Not recommended
conda update packagename
pip install --upgrade packagename

CUDA toolkit and cuDNN

We have to install the CUDA toolkit and the cuDNN if we want to enable the power of GPU

I will use CUDA 10.0, cuDNN 7.4 and Driver 410 as an example

Windows

Download the installer from the CUDA Archive and cuDNN Archive. The current version of tensorflow–gpu 1.13.1 supports the toolkit up to version 10.0, the cuDNN up to version 7.5.0 and the GPU driver 410.x or higher

1.Install CUDA toolkit

2.Unzip the cuda folder from the cuDNN package to C:\tools

C:\tools\cuda\...

3.Add paths to the Environment variables

# Right Click on "This PC" > Properties > Advanced System Settings > Advanced > Environment Variables > Select "Path" from System variables > Edit > New

# Add following paths to the Path
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\extras\CUPTI\libx64
C:\tools\cuda\bin

Ubuntu

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 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 update

# If you have no driver installed before, install 410.x or higher
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt install --no-install-recommends nvidia-driver-410 # Press Tab twice after ..-41, avaliable drivers starting with 41 will be listed
sudo reboot now # attention, this will reboot your system, so save your work before do it

# Install CUDA 10.0 and CUDNN 7.4
sudo apt install --no-install-recommends \
    cuda-10-0 \
    libcudnn7=7.4.1.5-1+cuda10.0  \
    libcudnn7-dev=7.4.1.5-1+cuda10.0

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

Ubuntu 16.04

# Add NVIDIA package repositories
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_10.0.130-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1604_10.0.130-1_amd64.deb
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo apt 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 update

# If you have no driver installed before, install 410.x or higher
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt install --no-install-recommends nvidia-driver-410 # Press Tab twice after ..-41, avaliable drivers starting with 41 will be listed
sudo reboot now # attention, this will reboot your system, so save your work before do it

# Install CUDA 10.0 and CUDNN 7.4
sudo apt install --no-install-recommends \
    cuda-10-0 \
    libcudnn7=7.4.1.5-1+cuda10.0  \
    libcudnn7-dev=7.4.1.5-1+cuda10.0

# Install TensorRT
sudo apt update && \
    sudo apt install nvinfer-runtime-trt-repo-ubuntu1604-5.0.2-ga-cuda10.0 && \
    sudo apt update && \
    sudo apt install -y --no-install-recommends libnvinfer-dev=5.0.2-1+cuda10.0

Tensorflow and Tensorflow-GPU

Tensorflow Official Website

After you entered/activated the environment you created

Install Tensorflow

pip install tensorflow # Non-GPU version
pip install tensorflow-gpu # GPU version
pip install tensorflow==2.0.0-alpha0 # Tensorflow 2.0 alpha version

Test Tensorflow

# If there is no warning, everything works fine
import tensorflow as tf
print(tf.__version__)

Enjoy it