How to install Tensorflow GPU with CUDA 9.2 for python on Ubuntu

This is going to be a tutorial on how to install tensorflow 1.8.0 GPU version. We will also be installing CUDA 9.2 and cuDNN 7.1.4 along with the GPU version of tensorflow 1.8.0. At the time of writing this blog post, the latest version of tensorflow is 1.8.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.

Update: We have a released a new article on How to install Tensorflow GPU with CUDA 10.0 for python on Ubuntu.

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.

Tensorflow 1.8 with CUDA 9.2 and cuDNN 7.1.4 performs up to 37% faster when compared to earlier versions of Tensorflow. View full results here.

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 add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
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: Install NVIDIA CUDA 9.2:

Remove previous cuda installation:

sudo apt-get purge nvidia*
sudo apt-get autoremove
sudo apt-get autoclean
sudo rm -rf /usr/local/cuda*

Install cuda :

For Ubuntu 16.04 :

sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64 /" | sudo tee /etc/apt/sources.list.d/cuda.list

For Ubuntu 17.10 and Ubuntu 18.04 :

sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64/7fa2af80.pub
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64 /" | sudo tee /etc/apt/sources.list.d/cuda.list

For Both Options:

sudo apt-get update 
sudo apt-get -o Dpkg::Options::="--force-overwrite" install cuda-9-2 cuda-drivers

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

Step 8: Go to terminal and type:

echo 'export PATH=/usr/local/cuda-9.2/bin${PATH:+:${PATH}}' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-9.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashrc
source ~/.bashrc
sudo ldconfig
nvidia-smi

Check driver version probably Driver Version: 396.26

(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.

20 Comments on How to install Tensorflow GPU with CUDA 9.2 for python on Ubuntu

  1. Awesome tutorial! Works flawlessly on my laptop. I’m running Ubuntu 16.04.4 and a GTX 850M.

    One thing though. In step 13, I think the command should be

    $ bazel build –config=opt –config=cuda //tensorflow/tools/pip_package:build_pip_package

    instead of

    bazel build –config=opt //tensorflow/tools/pip_package:build_pip_package

    beacause we are trying to install with GPU support.

    Thanks for the awesome tutorial! 🙂

  2. In step 13 I’m getting the following error :
    WARNING: /home/ayman/.cache/bazel/_bazel_ayman/34e68a100ed6057a683432517d6dc2b7/external/protobuf_archive/WORKSPACE:1: Workspace name in /home/ayman/.cache/bazel/_bazel_ayman/34e68a100ed6057a683432517d6dc2b7/external/protobuf_archive/WORKSPACE (@com_google_protobuf) does not match the name given in the repository’s definition (@protobuf_archive); this will cause a build error in future versions
    Unhandled exception thrown during build; message: Unrecoverable error while evaluating node ‘PACKAGE:tensorflow/core/kernels’ (requested by nodes ‘//tensorflow:libtensorflow_framework.so com.google.devtools.build.lib.skyframe.BuildConfigurationValue$Key@921c248b true (1650913861)’, ‘//tensorflow/core/grappler/costs:measuring_cost_estimator com.google.devtools.build.lib.skyframe.BuildConfigurationValue$Key@921c248b true (290604)’, ‘//tensorflow/core/grappler/clusters:single_machine com.google.devtools.build.lib.skyframe.BuildConfigurationValue$Key@921c248b true (96867453)’)

    Im running on Ubuntu 16.4, gtx1080ti

      • Manu kudos! It works though I see many warnings during compilation about comparing signed and unsigned integers, these guys don’t seem to know the difference. I managed to install the nccl-repo-ubuntu1604-2.2.12-ga-cuda9.2_1-1_amd64.deb package from the nVidia website for NCCL and installed the libnccl2 libnccl-dev libraries from their *.deb packages. The files are placed in /usr/lib/x86_64-linux-gnu, so no need to copy them over to /usr/local/cuda-9.2/targets/x86_64-linux/. Big thanks again, there should be more how to’s like this, e.g. caffe2 with cuda 9.2.

  3. Hey,
    I installed a fresh copy of Ubuntu 16.04 and I have an Nvidia 860M GPU. The current graphics card driver shows as – 396.26. My 860M has 640 cuda cores with Maxwell architecture.

    My kernel version is 4.13.0-43-generic
    I am getting the ‘nvidia-smi is not found’ error at Step – 8.
    Could you please help me out?

    • Restart your pc and select advanced option in boot menu. Then select kernel 4.10 then start from first step. If it worked then reply me.

      • Hey Arun.
        Thanks. This worked. I installed kernel 4.10 using the ukuu tool. And then started from the starting step. I was able to install the cudnn and build tensor flow and create the .whl file.

        I installed the whl with pip successfully.
        After this, while importing the tensorflow package in python interpreter, I am getting the following error –

        lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py”, line 24, in swig_import_helper
        _mod = imp.load_module(‘_pywrap_tensorflow_internal’, fp, pathname, description)
        ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

        Failed to load the native TensorFlow runtime.

        I checked that the nvcc version is 9.2.

        • May be you are running privious installation of tensorflow. Use virtual environment and install tf in it then try. Also make sure you have configured correctly as in tutorial.

  4. Thank you very much.. a very nice guide.

    For my specific case, 3 libraries were not found during tensorflow bazel command.
    I had to do: sudo ln -s /usr/local/cuda-9.2/lib64/libcurand.so.9.2 /usr/lib/libcurand.so.9.2

  5. Nice guide thank you.

    Using Ubuntu 16.04 I also needed to add a PPA for Python3.6-dev.

    sudo add-apt-repository ppa:deadsnakes/ppa

    sudo apt update

2 Trackbacks & Pingbacks

  1. How to Install Tensorflow GPU with CUDA 9.2 for Python on Ubuntu
  2. 今週の CTO 技術アンテナ(2018年6月6日) – MarketEnterprise Tech Blog

Leave a Reply

Your email address will not be published.




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