Tutorial: How to Install Tensorflow-GPU 1.8 for Python 2.7 and Python 3.5 on Ubuntu 16.04

Tensorflow is a machine learning framework offered as an opensource project by Google to help the AI community build and realize their models in a variety of programming languages, it offers a cutting edge toolbox implemented in a professional and polished way, it keeps up with the curve and it is witnessing a lot of growth and progress, which makes it ideal for both testing and production.
The problem is, while it’s easy to install and configure this framework and make it utilize CPU power, its documentation seems lacking just a bit when it comes to installing the GPU enabled version.

The Goal:

In this tutorial, we will go step by step about how to get the prerequisites, configure Ubuntu 16.04 for the installation, then actually have tensorflow-gpu up and running.


  1. A CUDA enabled Nvidia Graphics Card with compute capability of 3.0 or higher, head to the official Nvidia page and check your card for compatibility:https://developer.nvidia.com/cuda-gpus.
  2. The required Nvidia drivers & library (at the moment version 390).
  3. The CUDA runtime library & development library (version 9.0).
  4. The cuDNN runtime library & development library (v7.0.5 for CUDA 9.0).
  5. Both python 3.5 and python 2.7, which are shipped with Ubuntu 16.04 (but you may need to upgrade their minor versions, that is 2.7.x and 3.5.x), as well as the corresponding python development libraries.
  6. Both versions (3.5 and 2.7) of the pip package manager which will help you install the rest of the required packages.

Note: Nvidia, CUDA and cuDNN packages are essential for allowing tensorflow-gpu to access the low-level gpu operations/primitives and actually utilize it.

Step 1:

Make sure that the apt package manager can access the Nvidia and CUDA repositories:

  1. In your dash search, type: Software & Updates.
  2. Open “Software & Updates” and go to the tab labeled: Ubuntu Software.
  3. Make sure that (main), (universe), (restricted) and (multiverse) are selected; if not, select them.

Step 2:

Add the Nvidia CUDA repository to the apt package manager sources:

sudo bash -c "echo 'deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64 /' > /etc/apt/sources.list.d/cuda.list"

sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub

sudo apt update

Step 3:

Upgrade your python minor versions:

sudo apt install python2.7 python3.5

Step 4:

Install the initially required packages from the apt package manager:

sudo apt install python-dev python-software-properties python-pip python-tk python3-dev python3-software-properties python3-pip python3-tk nvidia-390 nvidia-390-dev cuda-9-0 nvidia-cuda-dev nvidia-cuda-toolkit

Then Upgrade the pip package manager to the latest version:

sudo pip2 install --upgrade pip

sudo pip3 install --upgrade pip

Step 5:

Download the required cuDNN packages:

  1. Head to the cuDNN official site https://developer.nvidia.com/rdp/cudnn-download.
  2. Register for an account, complete the registration and log in.
  3. Agree to the Terms of the cuDNN Software License Agreement.
  4. Check the Archived cuDNN Releases.
  5. From the subsection Download cuDNN v7.0.5 (Dec 5, 2017), for CUDA 9.0:
  • Choose cuDNN v7.0.5 Runtime Library for Ubuntu16.04 (Deb) in order to download: libcudnn7_7.0.5.15–1+cuda9.0_amd64.deb.
  • Choose cuDNN v7.0.5 Developer Library for Ubuntu16.04 (Deb) in order to download: libcudnn7-dev_7.0.5.15–1+cuda9.0_amd64.deb.
  • Choose cuDNN v7.0.5 Code Samples and User Guide for Ubuntu16.04 (Deb) in order to download: libcudnn7-doc_7.0.5.15–1+cuda9.0_amd64.

Note that you need this version specifically (–1+cuda9.0), because it is the one most compatible with tensorflow-gpu at the moment.

Step 6:

Install the cuDNN packages, open a terminal inside the folder you downloaded the packages to, and issue the following commands:

sudo dpkg -i libcudnn7_7.0.5.15-1+cuda9.0_amd64.deb

sudo dpkg -i libcudnn7-dev_7.0.5.15-1+cuda9.0_amd64.deb

sudo dpkg -i libcudnn7-doc_7.0.5.15-1+cuda9.0_amd64.deb

Step 7:

Setup the appropriate environment variables so that both pip and tensorflow-gpu understand where your cuda paths are:

export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}

export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-9.0/extras/CUPTI/lib64

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/extras/CUPTI/lib64

export CUDA_HOME=/usr/local/cuda-9.0

Step 8:

Some python packages may be required for tensorflow operations and result visualization:

  1. The numpy package: since tensors are usually fed numpy arrays, and they are definitely evaulated to numpy arrays after they are executed in a tensorflow session.
  2. The matplotlib and seaborn packages: for data visualization of our training errors and results.

In the same terminal as in step 7, issue the following commands:

sudo pip2 install --no-cache-dir numpy matplotlib seaborn

sudo pip3 install --no-cache-dir numpy matplotlib seaborn

Note that the –no-cache-dir flag may be necessary since caching may break the installation process on some machines, mainly when using pip2.

Step 9:

Install tensorflow-gpu; in the same terminal as in step 7, issue the following commands:

sudo pip2 install --no-cache-dir tensorflow-gpu

sudo pip3 install --no-cache-dir tensorflow-gpu

Step 10:

Check if tensorflow-gpu is installed correctly:

sudo pip2 show tensorflow-gpu

sudo pip3 show tensorflow-gpu


Tensorflow is an amazing set of tools to have in your machine learning development box, but what is best is to have the power version of this tool with the GPU enabled tensorflow, which cuts training time significantly and provides semi-industrial levels of computation capability on a humble laptop with the minimum requirements.
With the steps above, you should have tensorflow-gpu up and running in little time, depending on your internet connection, good luck!

Legal Disclaimers:

  1. The Ubuntu word and associated logos are registered trademarks of Canonical Ltd.
  2. TensorFlow, the TensorFlow logo and any related marks are trademarks of Google Inc.
  3. The Python logo is a trademark of the Python Software Foundation.
  4. I am not associated nor endorsed by either: Canonical Ltd, Google Inc, the Python Software Foundation.

A Computer Scientist with a background in Computer Engineering, a tech enthusiast, and an open-source advocate. https://buymeacoff.ee/AlyShmahell

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store