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

Step 8: Go to terminal and type:

nano ~/.bashrc

In the end of the file, add:

export PATH=/usr/local/cuda-9.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-9.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

ctrl+x then y to save and exit

source ~/.bashrc
sudo ldconfig
nvidia-smi

Check driver version probably Driver Version: 390.30

(not likely) If you got nvidia-smi is not found then you have unsupported linux kernel installed. Comment your linux kernel version noted in step 5.

Step 9: Install cuDNN 7.1.2:

Goto https://developer.nvidia.com/cudnn and download Membership required

After login

Download the following:

cuDNN v7.1.2 Runtime Library for Ubuntu16.04 (Deb)

cuDNN v7.1.2 Developer Library for Ubuntu16.04 (Deb)

cuDNN v7.1.2 Code Samples and User Guide for Ubuntu16.04 (Deb)

Goto downloaded folder and in terminal perform following:

sudo dpkg -i libcudnn7_7.1.2.21-1+cuda9.1_amd64.deb
sudo dpkg -i libcudnn7-dev_7.1.2.21-1+cuda9.1_amd64.deb
sudo dpkg -i libcudnn7-doc_7.1.2.21-1+cuda9.1_amd64.deb

Verifying cuDNN installation:

cp -r /usr/src/cudnn_samples_v7/ $HOME
cd  $HOME/cudnn_samples_v7/mnistCUDNN
make clean && make
./mnistCUDNN

If cuDNN is properly installed and running on your Linux system, you will see a message similar to the following:

Test passed!

Step 10: Install Dependencies

libcupti (required)

sudo apt-get install libcupti-dev
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc

Bazel (required)

sudo apt-get install openjdk-8-jdk
wget https://github.com/bazelbuild/bazel/releases/download/0.11.1/bazel_0.11.1-linux-x86_64.deb
sudo dpkg -i bazel_0.11.1-linux-x86_64.deb

To install these packages for Python 2.7, issue the following command:

sudo apt-get install python-numpy python-dev python-pip python-wheel

To install these packages for Python 3.n, issue the following command:

sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel

Step 11: Configure Tensorflow from source:

Reload environment variables

source ~/.bashrc
sudo ldconfig

Start the process of building TensorFlow by downloading latest tensorflow 1.7.0 .

wget https://github.com/tensorflow/tensorflow/archive/v1.7.0.zip
unzip v1.7.0.zip
cd tensorflow-1.7.0
./configure

Give python path in

Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3

Press enter two times

Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: Y
Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: Y
Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: Y
Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: Y
Do you wish to build TensorFlow with Apache Kafka Platform support? [y/N]: N
Do you wish to build TensorFlow with XLA JIT support? [y/N]: N
Do you wish to build TensorFlow with GDR support? [y/N]: N
Do you wish to build TensorFlow with VERBS support? [y/N]: N
Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: N
Do you wish to build TensorFlow with CUDA support? [y/N]: Y
Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 9.0]: 9.1
Please specify the location where CUDA 9.1 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.1.2
Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/lib/x86_64-linux-gnu
Do you wish to build TensorFlow with TensorRT support? [y/N]: N

Now we need compute capability which we have noted at step 1 eg. 5.0

Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 5.0] 5.0
Do you want to use clang as CUDA compiler? [y/N]: N
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: /usr/bin/gcc
Do you wish to build TensorFlow with MPI support? [y/N]: N
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: -march=native
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:N

Configuration finished

Step 12: Build Tensorflow using bazel

Do following to create symbolic link to cuda/include/math_functions.hpp from cuda/include/crt/math_functions.hpp to fix math_functions.hpp is not found error.

sudo ln -s /usr/local/cuda/include/crt/math_functions.hpp /usr/local/cuda/include/math_functions.hpp

If it says file already exits then ignore it.

The next step in the process to install tensorflow GPU version will be to build tensorflow using bazel. This process takes a fairly long time.

To build a pip package for TensorFlow with CPU-only support, you would typically invoke the following command:

bazel build --config=opt --incompatible_load_argument_is_label=false //tensorflow/tools/pip_package:build_pip_package

Note:-

 add "--config=mkl" if you want Intel MKL support for newer intel cpu for faster training on cpu

We are building with GPU support so do following

To build a pip package for TensorFlow with GPU support, invoke the following command:

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

This process will take a lot of time. It may take 1 – 2 hours or maybe even more.

The bazel build command builds a script named build_pip_package. Running this script as follows will build a .whl file within the tensorflow_pkg directory:

To build whl file issue following command:

bazel-bin/tensorflow/tools/pip_package/build_pip_package tensorflow_pkg

Activate your virtual environment here if you use.

To install tensorflow with pip:

cd tensorflow_pkg

for python 2:

pip2 install tensorflow*.whl

for python 3:

pip3 install tensorflow*.whl

Step 13: Verify Tensorflow installation

Run in terminal

python

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

If the system outputs the following, then you are ready to begin writing TensorFlow programs:

Hello, TensorFlow!

Success! You have now successfully installed tensorflow 1.7.0 on your machine. If you are on Windows OS, you might want to check out our other post here, How to install Tensorflow 1.7.0 GPU with CUDA Toolkit 9.1 and cuDNN 7.1.2 for Python 3 on Windows OS. Cheers!!

For prebuilt wheel with optimization for AVX2 and cuda 9.1, cudnn 7.1.2, compute capability 5.0 go to this link .

Update:

Now cuda 9.2 is available, follow this link for the updated tutorial How to install Tensorflow GPU with CUDA 9.2 for python on Ubuntu .

 

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.