How to install Tensorflow GPU with CUDA Toolkit 9.1 and cuDNN 7.1.2 for Python 3 on Ubuntu 16.04-64bit

STEP BY STEP INSTALLATION OF CUDA TOOLKIT 9.1, CUDNN 7.0.5 AND TENSORFLOW 1.4.1 GPU VERSION ON UBUNTU 16.04

This is going to be a tutorial on how to install tensorflow 1.7.0 GPU version. We will also be installing CUDA Toolkit 9.1 and cuDNN 7.1.2 along with the GPU version of tensorflow 1.7.0. At the time of writing this blog post, the latest version of tensorflow is 1.7.0.This tutorial is for building tensorflow from source. If you want to use the official pre-built pip package instead, I recommend another post, How to install Tensorflow 1.7.0 using official pip package.

Tensorflow is an open source software library developed and used by Google that is fairly common among students, researchers, and developers for deep learning applications such as neural networks. It has both the CPU as well as GPU version available and although the CPU version works quite well, realistically, if you are going for deep learning, you will need GPU. In order to use the GPU version of TensorFlow, you will need an NVIDIA GPU with a compute capability > 3.0. While it is technically possible to install tensorflow GPU version in a virtual machine, you cannot access the full power of your GPU via a virtual machine. So, I recommend doing a fresh install of Ubuntu if you don’t have Ubuntu before starting with the tutorial.

There must be 64-bit python installed tensorflow does not work on 32-bit python installation.

Step 1: Update and Upgrade your system:

sudo apt-get update 
sudo apt-get upgrade

Step 2: Verify You Have a CUDA-Capable GPU:

lspci | grep -i nvidia

Note GPU model. eg. GeForce 840M

If you do not see any settings, update the PCI hardware database that Linux maintains by entering update-pciids (generally found in /sbin) at the command line and rerun the previous lspci command.

If your graphics card is from NVIDIA then goto http://developer.nvidia.com/cuda-gpus and verify if listed in CUDA enabled gpu list.

Note down its Compute Capability. eg. GeForce 840M 5.0

Step 3: Verify You Have a Supported Version of Linux:

To determine which distribution and release number you’re running, type the following at the command line:

uname -m && cat /etc/*release

The x86_64 line indicates you are running on a 64-bit system which is supported by cuda 9.1

Step 4: Install Dependencies:

Required to compile from source:

sudo apt-get install build-essential 
sudo apt-get install cmake git unzip zip 
sudo apt-get install python2.7-dev python3.5-dev python3.6-dev pylint

Step 5: Install linux kernel header:

Goto terminal and type:

uname -r

You can get like “4.10.0-42-generic”. Note down linux kernel version.

To install linux header supported by your linux kernel do following:

sudo apt-get install linux-headers-$(uname -r)

Step 6:  Download the NVIDIA CUDA Toolkit:

Go to https://developer.nvidia.com/cuda-downloads and download Installer for Linux Ubuntu 16.04 x86_64 deb[network]. I highly recommend network installer to get updated gpu driver supported by your linux kernel.

For, direct download

wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.1.85-1_amd64.deb

Installation Instructions:

sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo dpkg -i cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda-9.1

Step 7: Reboot the system to load the NVIDIA drivers.

20 Comments on How to install Tensorflow GPU with CUDA Toolkit 9.1 and cuDNN 7.1.2 for Python 3 on Ubuntu 16.04-64bit

  1. Thanks. I’m having an issue installing Cuda Toolkit 9.1 on my clean install of ubuntu 16.04

    After installing, on reboot, I get stuck in an endless loop of: 1. login screen – 2. enter credentials – 3. crash – 1.login screen..

    Video: https://youtu.be/da1Xv7mNwrQ

    For anyone else having issues, I found another person with the same problem and we’ve been discussing on this thread:

    https://devtalk.nvidia.com/default/topic/1028802/problems-with-cuda-9-1-in-ubuntu-16-04/?offset=9#5233925

    • Thanks Patrick. If followed steps carefully then there should not be problem. Please check your linux kernel version and run deb network installer as written in tutorial.

  2. I’m sorry can you please elaborate on step 5? Why is this step needed? The output from “uname -r” for me was 4.10.0-28-generic. Does that mean I can skip this step bc it’s 4.10?

  3. I’ve installed correctly tensorflow with GPU’s, but i can’t run it with other users, i have the next error:

    ImportError: No module named ‘tensorflow’

    what should i do ?

    • Does it worked on current user? For another user pip install tensorflow*.whl should work. It will be good to use virtualenv.

  4. i did every step as mentioned but I am getting this error

    after using pip install tensorflow*

    tensorflow-1.4.1-cp27-cp27mu-linux_x86_64.whl is not a supported wheel on this platform.

    • Thank you for comment. You have built tensorflow with python 2.7 version. I have not tested on 2.7. Does pip belongs to python 27? You can check by pip –version. I think pip2 install tensorflo*.whl should work.

      • I have a similar problem

        tensorflow-1.4.1-cp35-cp35m-linux_x86_64.whl is not a supported wheel on this platform.

        • Please check the python version and associated pip command. The wheel you are trying to install is for python 3.5 64 bit.

  5. bazel build –config=opt –-config=cuda –incompatible_load_argument_is_label=false //tensorflow/tools/pip_package:build_pip_package

    It seems there is incorrect symbol, “–” instead of “-” before config=cuda. Not sure if this matters but at the moment I’ve got that tensorflow was built without CUDA support so I’m rebuilding it.

    • Thank you, Dmitry. It happened due to auto-correct option of editor. Now updated. There is two command CPU only and GPU support. I hope you have used GPU version command for bazel. Have you configured to build CUDA (Y) in while running ./configure ?

  6. I have successfully followed the above on Ubuntu 17.04 with the following changes:
    changed the all references to ubuntu1604 in step 6 to ubuntu 1704
    to build bazel for cuda 9.1 in step 12 I needed ‘sudo ln -s /usr/local/cuda/include/crt/math_functions.hpp /usr/local/cuda/include/math_functions.hpp’ as other have pointed out.
    I needed to use ‘pip3 install tensorflow*’ at the end of step 12 else I got ‘xxx.whl is not a supported wheel on this platform’.
    Thanks so much for this tutorial – it has enabled me to get tensorflow working on my GPU after a couple of days of trying to get various versions of ubuntu, cuda, tensorflow all playing happy in the same box – can finally get on with the Udacity Deep Learning course

  7. Hi.After building the tensorflow file and changing the directory to tensorflow_package, i am getting the following error-Double requirement given: tensorflow==1.4.1 from file:///home/dhruv/tensorflow-1.4.1/tensorflow_pkg/tensorflow-1.4.1-cp36-cp36m-linux_x86_64.whl (already in tensorflow, name=’tensorflow’)

    How do i resolve this error?

    • I think pip install –upgrade tensorflow*.whl should work. If not worked then this might work pip install –upgrade –no-deps –force-reinstall tensorflow*.whl
      Reply me if any issue.

    • Hello Benoit Courty. Thank you for your valuable comment. If CUDA Toolkit is installed correctly then all symbolic links should be generated in /usr/local/cuda.
      However, in cases where the symbolic links are not automatically created, you will get an error saying “math_functions.hpp: No such file or directory”. Creating a symlink from cuda/include/math_functions.hpp to cuda/include/crt/math_functions.hpp fixes the issue as you suggested.

  8. sudo dpkg -i libcudnn7_7.0.3.11-1+cuda9.0_amd64.deb
    sudo dpkg -i libcudnn7-dev_7.0.3.11-1+cuda9.0_amd64.deb
    sudo dpkg -i libcudnn7-doc_7.0.3.11-1+cuda9.0_amd64.deb

    this are cuda9.0 version
    i think they must be cuda9.1 version !!!

    • Hello naser,

      Thank you for the comment. It seems there has been a slight mistake on the post and now it’s updated.

      Cheers!

1 Trackbacks & Pingbacks

  1. Install GPU version of tensorflow | Python 3.6

Leave a Reply

Your email address will not be published.




This site uses Akismet to reduce spam. Learn how your comment data is processed.