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.

30 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. hi, when i command nvidia-smi it says command not found
    the kernel of my ubuntu is 4.13.0-38-generic
    i used to install cuda-9.1 and cudnn 7.0.5 on this version but now it fails.
    what should I do???
    (the version of my ubuntu is 16.04)

    • Have you used the network deb installer ? This is caused by not loading nvidia driver. Go to advanced boot option while booting and select the kernel 4.10 or 4.4 and try again.

      • I installed kernel 4.10 and CUdA 9.1 and cudnn 7.0.5 and tensorflow-gpu (version 1.7)successfully installed, But when I call import tensorflow I see the error below:

        ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

        what I do?
        thank you

        • I think you have installed official wheel which only support cuda 9.0. To suuport cuda 9.1 you have to build it from source as illustrated in the tutorial. Install tf in new virtual environment or upgrade.

  2. Thanks! This was the first comprehensive installation tutorial which worked for this version of Ubuntu. Well done :))

  3. Hi, thanks a lot for the article. After running the ./configure command and setting the locations I get this error:

    ERROR: /home/mark/.cache/bazel/_bazel_mark/750cb90d9f07da92bb7e9828b438c326/external/io_bazel_rules_closure/closure/private/defs.bzl:27:16: The function ‘set’ has been removed in favor of ‘depset’, please use the latter. You can temporarily refer to the old ‘set’ constructor from unexecuted code by using –incompatible_disallow_uncalled_set_constructor=false
    ERROR: error loading package ”: Extension file ‘closure/private/defs.bzl’ has errors

    Any ideas?

    • According to the error use this argument in bazel build command may fix the error. Use `–incompatible_disallow_uncalled_set_constructor=false`

      • I’ve tried adding it “./configure –incompatible_disallow_uncalled_set_constructor=false” but I get the same error.

        • Add that flag in bazel rather then configure. I can’t test this now but i am planning to update all the tutorials in next week.

          • Sorry I’m still getting the error. I tried running “sudo bazel canonicalize-flags –incompatible_disallow_uncalled_set_constructor=false

            with no errors until ./configure at which point I got the same errors while trying to build. Then I put it in ./configure like so:
            “Please specify optimization flags to use during compilation when bazel option “–config=opt” is specified [Default is -march=native]: –incompatible_disallow_uncalled_set_constructor=false”
            and I also tried putting it after ./configure but all lead to the same error. I’m still kind of a noob to ubuntu so any help is appreciated.

            • Ok. Please specify the os, bazel version, tensorflow and cuda version, gcc version so that i can give you the out of box working solution.

            • I’m on Ubuntu 16.04 , I’ve tried bazel versions 0.1.10(newest one) 0.5.4 and 0.9.0, tensor flow is 1.7.0, CUDA is 9.1 and Cudnn is 7.1.1, and my gcc is 5.4.0

            • Hi Markf, i have tested the build for tensorflow 1.7.0-rc1 with bazel 0.11.1, cuda 9.1, cudnn 7.1.2, gcc 5.4 without any problem. May be there is problem with bazel cache files. so use bazel clean --expung then bazel shutdown and start again from ./configure if not worked then remove bazel from cache using following command rm -rf ~/.cache/bazel then try again. Also use bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package to build.

            • Still no luck after clearing the cache “~/tensorflow$ ./configure
              Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3
              Please specify optimization flags to use during compilation when bazel option “–config=opt” is specified [Default is -march=native]:
              Do you wish to use jemalloc as the malloc implementation? [Y/n] y
              jemalloc enabled
              Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] y
              Google Cloud Platform support will be enabled for TensorFlow
              Do you wish to build TensorFlow with Hadoop File System support? [y/N] y
              Hadoop File System support will be enabled for TensorFlow
              Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] n
              No XLA JIT support will be enabled for TensorFlow
              Found possible Python library paths:
              /usr/local/lib/python3.5/dist-packages
              /usr/lib/python3/dist-packages
              Please input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages]

              Using python library path: /usr/local/lib/python3.5/dist-packages
              Do you wish to build TensorFlow with OpenCL support? [y/N] n
              No OpenCL support will be enabled for TensorFlow
              Do you wish to build TensorFlow with CUDA support? [y/N] y
              CUDA support will be enabled for TensorFlow
              Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
              Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 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]:
              Please specify the Cudnn version you want to use. [Leave empty to use system default]: 7.1.1
              Please specify the location where cuDNN 7.1.1 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:
              Please specify a list of comma-separated Cuda compute capabilities you want to build with.
              You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
              Please note that each additional compute capability significantly increases your build time and binary size.
              [Default is: “3.5,5.2”]: 5.0
              ……….
              INFO: Starting clean (this may take a while). Consider using –async if the clean takes more than several minutes.
              ……….
              ERROR: /home/mark/.cache/bazel/_bazel_mark/750cb90d9f07da92bb7e9828b438c326/external/io_bazel_rules_closure/closure/private/defs.bzl:27:16: The function ‘set’ has been removed in favor of ‘depset’, please use the latter. You can temporarily refer to the old ‘set’ constructor from unexecuted code by using –incompatible_disallow_uncalled_set_constructor=false
              ERROR: error loading package ”: Extension file ‘closure/private/defs.bzl’ has errors
              Building: no action

            • have you deleted the /.cache/bazel dir? using rm -rf ~/.cache/bazel also verify bazel version using ‘bazel version‘. It should be heigher version then 0.10.

            • Upgrade your bazel because tensorflow 1.7 build system is only compatible with bazel higher than 0.10 . Use the latest bazel 0.11.1. See the official documentation to install it.

            • I completely removed with “sudo apt-get remove –purge bazel” and installed by following directions on bazel’s website but still get the same error.

            • Thanks! Can just install this on a virtual env given that I have the drivers installed? Using pip install .whl correct?

  4. Hello thanks a lot for your article
    I can’t make the last step :pip3 install tensorflow*
    I have : tensorflow-1.4.1-cp36-cp36m-linux_x86_64.whl is not a supported wheel on this platform.

    how can I manage it ?
    thank you

    • Check pip platform pip3 --version the whl is for python 3.6 64 bit python. Use pip associated with python 3.6 64 bit. Thanks.

  5. Hello! Great article, I was able to install TensorFlow 1.6 by simply downloading the most recent version and managed to get everything else done following this tutorial.

    However when testing the installation here’s what comes up:

    import tensorflow as tf
    /home/jdevezas/anaconda/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
    from ._conv import register_converters as _register_converters

    hello = tf.constant(‘Hello, TensorFlow!’)

    sess = tf.Session()

    print(sess.run(hello))
    b’Hello, TensorFlow!’

    There’s that message at the beginning and also that ‘b’ before it prints out “Hello, TensorFlow”. Is there anything that can be done? Will it be okay to use TF?

    • You can ignore the warning later it will be resolved. ‘b’ represents bytes type so always include b with string for other use. You can use search engine for clear information. Your installation is good and can continue to use.

  6. hi and thx – I got this running before – but was not able to get tensorRT running. It worked only w 9.0/7.0.5
    Did you try this and succeed?

  7. Getting this error at STEP-9.previously, I tried to install CUDA 8.0. That was unsuccessful. Currently running Nvidia GForce820M series on my laptop.Please help me if you can…

    cudnnGetVersion() : 7005 , CUDNN_VERSION from cudnn.h : 7005 (7.0.5)
    Host compiler version : GCC 5.4.1
    There are 1 CUDA capable devices on your machine :
    device 0 : sms 2 Capabilities 2.1, SmClock 1250.0 Mhz, MemSize (Mb) 1985, MemClock 900.0 Mhz, Ecc=0, boardGroupID=0
    Using device 0

    Testing single precision
    CUDNN failure
    Error: CUDNN_STATUS_ARCH_MISMATCH
    mnistCUDNN.cpp:394
    Aborting…

    • I think there is miss match between cuda and cudnn, install the appropriate cudnn for cuda you want to choose then configure accordingly. Use network deb installer. If there is error about minimum compute capability then use minimum compute capability it is 3.0 or 3.5. Please confirm that from error.

1 Trackbacks & Pingbacks

  1. Nvidia-powered Tensorflow on a Linux home server in 400,000 easy steps • Cryptocurrency decentral market headlines

Leave a Reply

Your email address will not be published.




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