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 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
echo "deb /" | sudo tee /etc/apt/sources.list.d/cuda.list

For Ubuntu 17.10 and Ubuntu 18.04 :

sudo apt-key adv --fetch-keys
echo "deb /" | 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

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. Thanks for great instructions. I successfully installed TensorFlow rc1.10 with cuda 9.2 and cuDNN 7.14 on Ubuntu 18.04. I’ve seen segfault while running bazel build and needed to repeat a few times until as it’s described in the article. I used –job $(grep -c ^processor /proc/cpuinfo) to build everything in parallel and it facilitated the build process little bit.

  2. Is it possible to install tensorflow*.whl in a conda env? A conda env would help me install and compile other packages I need more effectively.

    I get a ‘tensorflow-1.8.0-cp35-cp35m-linux_x86_64.whl is not a supported wheel on this platform’ error when I use pip and conda just says the the package is not available.

    • Yes it possible if your are using 64 bit version of conda. Activate the environment and install pip in environment using conda then use pip to install the whl.

  3. Hello,
    with this installation i get the Error
    ImportError: Could not import tensorflow. Do not import tensorflow from its source directory; change directory to outside the TensorFlow source tree, and relaunch your Python interpreter from there.
    If i start the from ~/tensorflow.
    Outside ~/tensorflow i get the Error
    ModuleNotFoundError: No module named ‘tensorflow_hub’

    The is working inside ~/tensorflow, but not outside.


    • Create a virtual environment and install whl inside. Change the directory to other location outside tensorflow source then activate virtual env. Then it may work.

      • Thank you,
        now it works.
        Here my steps for other people:
        mkdir tensorflow
        cd tensorflow
        virtualenv –system-site-packages -p python3 tf_1.10_cuda9.2
        source ~/tensorflow/tf_1.10_cuda9.2/bin/activate
        pip3 install ~/tensorflow-1.10.0rc0-cp36-cp36m-linux_x86_64.whl
        cd tensorflow/tf_1.10_cuda9.2/
        pip3 install tensorflow-hub
        cd example_code/
        python3 –image_dir ~/flower_photos

        ps: i use the new tensorflow 1.10


  4. Hi, got an error on step 8 nvidia-smi.
    my linux kernel is 4.13.0-46-generic

    Any clue on what I should do?
    I also couldn’t update deadsnakes (no release file)and I can seem to figure out why.

    using ubuntu 17.10, should I upgrade to 18.04?

    kind regards!

    • Install linux header generic 4.10 or 4.4 with linux image and linux all. Then try again by switching to linux kernal from advanced options from boot menu. By adding deadsnakes repo ppa you can use it.

  5. This has been more helpful to me than the instructions on the actual TensorFlow site.

    But when I run “bazel build –config=opt //tensorflow/tools/pip_package:build_pip_package” it fails with an error “…BUILD:5:1: no such package ‘@local_config_python//…” and later in the error message it says “Python Configuration Error: Invalid python library path: /usr/bin/python3”

    After Googling, I have tried the various suggestions on that (running from the specific python called in the configure, setting the PYTHON_BIN_PATH via export either on the fly before the bazel build command or just right in .bashrc and then source ~/.bashrc to reload it, or even in the .bazelrc to set “–action_env=PYTHON_BIN_PATH=/usr/bin/python3”

    I’ve have tried referencing it as the anaconda python or the /usr/bin/ one and I get the same error either way.

    If you have any suggestions, I would love to get this working.

  6. I found this walkthrough incredibly helpful. However, when I attempt to build tensorflow using bazel in step 13. I get a series of Cuda Configuration Errors. Each one claiming it ‘Cannot find cuda library’. After looking in the folders, I realised I do have, libcublas_device.a and libcublas_static.a but not the 9.2 version.

    Could you let me know why this file seems to be omitted from my folders and what I can do to fix it?

    Thank you in advance

    • Which method of cuda installation you had used, i prefer network deb. Was running ./configure successful?

    • try export LD_LIBRARY_PATH=/usr/local/cuda/lib64/ then bazel build --config=opt --config=cuda tensorflow/tools/pip_package:build_pip_package --action_env="LD_LIBRARY_PATH=${LD_LIBRARY_PATH}"

      • Hi, thanks for the quick reply. I tried your suggestion and I still receive the same error. I originally downloaded the deb local version of CUDA but I just installed the deb network version after removing the previous local version but this has not worked either. ./configure does work successfully.

        Here is a copy of the error message I keep receiving:

        WARNING: The following configs were expanded more than once: [cuda]. For repeatable flags, repeats are counted twice and may lead to unexpected behavior.
        Loading: 0 packages loaded
        ERROR: error loading package ‘tensorflow/tools/pip_package’: Encountered error while reading extension file ‘cuda/build_defs.bzl’: no such package ‘@local_config_cuda//cuda’: Traceback (most recent call last):
        File “/home/conorlewis/tensorflow/third_party/gpus/cuda_configure.bzl”, line 1142
        File “/home/conorlewis/tensorflow/third_party/gpus/cuda_configure.bzl”, line 1014, in _create_local_cuda_repository
        _find_libs(repository_ctx, cuda_config)
        File “/home/conorlewis/tensorflow/third_party/gpus/cuda_configure.bzl”, line 655, in _find_libs
        _find_cuda_lib(“cublas”, repository_ctx, cpu_value, c…, …)
        File “/home/conorlewis/tensorflow/third_party/gpus/cuda_configure.bzl”, line 587, in _find_cuda_lib
        auto_configure_fail((“Cannot find cuda library %s” %…))
        File “/home/conorlewis/tensorflow/third_party/gpus/cuda_configure.bzl”, line 210, in auto_configure_fail
        fail((“\n%sCuda Configuration Error:%…)))

        Cuda Configuration Error: Cannot find cuda library
        INFO: Elapsed time: 0.969s
        INFO: 0 processes.
        FAILED: Build did NOT complete successfully (0 packages loaded)
        FAILED: Build did NOT complete successfully (0 packages loaded)

        Thanks for your help again, I really appreciate it!

      • I moved to a different virtual machine and the walkthrough worked perfectly. I must have messed something up while attempting to install tensorflow using previous walkthroughs or something.

        Thanks again for the wonderful walkthrough, it helped me out enormously!

  7. I already did NVIDIA Cuda setup 5 times and this is the only tutorial which works without any modification. Congrats!

    I only have one question regarding version of Python, default python is 3.5 can you give additional description how to switch python to 3.6 before installing tensorflow?

  8. Good afternoon,

    I’m trying to follow your guide so I can get tensorFlow GPU running on a fresh install of Ubuntu 18.04. When I run “sudo add-apt-repository ppa:deadsnakes/ppa” to add the repository, I get the following errors:

    W: GPG error: Release: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY F60F4B3D7FA2AF80
    E: The repository ‘ Release’ is not signed.

    Has the repo been updated or changed?

    Thank you,
    Robert Chastain

  9. I am getting error regarding, all the symlinks required exist.

    ERROR: /home/aparna/.cache/bazel/_bazel_aparna/08567858dec7c600aecfb5c1613d0af8/external/local_config_cuda/cuda/BUILD:1226:1: declared output ‘external/local_config_cuda/cuda/cuda/lib/’ is a dangling symbolic link

Leave a Reply

Your email address will not be published.

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