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.



18 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. I get the error:
    From Compiling tensorflow/core/kernels/reduction_ops_gpu_double.cu.cc:
    Segmentation fault (core dumped)

    ERROR: /home/usr/tensorflow-1.7.0/tensorflow/core/kernels/BUILD:2820:1: output ‘tensorflow/core/kernels/_objs/reduction_ops_gpu/tensorflow/core/kernels/reduction_ops_gpu_double.cu.pic.o’ was not created
    ERROR: /home/usr/tensorflow-1.7.0/tensorflow/core/kernels/BUILD:2820:1: not all outputs were created or valid
    Target //tensorflow/tools/pip_package:build_pip_package failed to build steps.

    Can someone help me?

  2. Thanks Arun! This tutorial saved me time, I was struggling to get my NVidia GTX 1080 to work on Ubuntu. I installed into a fresh Ubuntu 16.04. I then installed with the .whl in Anaconda Python and added Keras, which also worked. However, I have a problem using Spyder.

    I usually launch Spyder remotely using PuTTY and it appears on my Win 10 via XMing server. This works fine except in the environment created using your Tensorflow .whl, which instead gives an error “Could not initialize GLX” in the PuTTY session. Do you have any idea why?

    In fact, in this environment Spyder does not even launch on its own Ubuntu machine. Instead there are a lot of QOpenGLShader errors. I solved this problem by adding

    import ctypes
    ctypes.CDLL(“libGL.so.1”, mode=ctypes.RTLD_GLOBAL)

    into

    site-packages/spyder/app/start.py

    which is a solution I found at this site:

    https://github.com/spyder-ide/spyder/issues/3226

    Do you have any idea what is causing this GLX error? Any help appreciated.

  3. Thanks Arun! – I was struggling to get Tensorflow with GPU support working after the normal “pip install” operations failed (for reasons including CUDA 9.0 hardcoded (!) and support problems with AMD processors). This post was just what I was looking for – ran through the steps and compiled from source and I have it working!

  4. Linux kernel version: 4.13.0-37-generic

    I’m getting following error in step 8:
    “bash: nvidia-smi: command not found”

    • Use kernel 4.10 or 4.4 and try again. You can do it from advanced menu on boot screen. If it worked then you can make the changes pemanent by either configuring this option as default or by uninstalling the 4.13.

    • Seems like tensorflow installed in other python location. Please verify the target of python keyword. May be python3 will work for you. Also check it by using pip list or pip3 list if TF installed then check the associated python and its path by using pip -V or pip3 -V .

      • Thank you very much. Its working now. The mistake i was doing is as i was using conda for environment management but during ./configure i gave python from /usr/bin/python, so although everything was installed nicely but to different location. So when i activated conda environment, the python in it cud not find TF.

        I have one simple question, is TF 1.7 backword compatible with earlier ones like 1.5,1.6 etc. Thanks once again.

  5. Hello. Thanks for very helpful post. The end result of all the steps on my ubuntu 16.0.4 is
    Successfully installed absl-py-0.1.13 astor-0.6.2 bleach-1.5.0 gast-0.2.0 grpcio-1.10.1 html5lib-0.9999999 markdown-2.6.11 numpy-1.14.2 protobuf-3.5.2.post1 setuptools-39.0.1 six-1.11.0 tensorboard-1.7.0 tensorflow-1.7.0 termcolor-1.1.0 werkzeug-0.14.1 wheel-0.31.0

    but when i test in python, it says

    >>> import tensorflow as tf
    Traceback (most recent call last):
    File “”, line 1, in
    ModuleNotFoundError: No module named ‘tensorflow’

    Please guide.

  6. Hi, I tried out your tutorial. I got nvidia-smi command not found.
    My linux kernel version is 4.13.0-38-generic

1 Trackbacks & Pingbacks

  1. ubuntu 16.04 cuda 9.1源代码安装tensorflow-1.7 – LiangXu Wang

Leave a Reply

Your email address will not be published.




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