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

This tutorial is for building tensorflow 1.12 GPU from source along with CUDA 10 and cuDNN 7.3.1

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

Using latest version of Tensorflow provides you latest features and optimization, using latest CUDA Toolkit provides you speed improvement with latest gpu support and using latest CUDNN greatly improves deep learing training time.

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 python-dev python3-dev python-pip python3-pip

Step 5: Install linux kernel header:

Goto terminal and type:

uname -r

You can get like “4.15.0-36-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 10.0:

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 18.04 :

sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/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-10-0 cuda-drivers

You can also install cuda toolkit following instructions from here and it is recommended to use deb[network].

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

Step 8: Go to terminal and type:

echo 'export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-10.0/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.

You can check your cuda installation using following sample:

cuda-install-samples-10.0.sh ~
cd ~/NVIDIA_CUDA-10.0_Samples/5_Simulations/nbody
make
./nbody



12 Comments on How to install Tensorflow GPU with CUDA 10.0 for python on Ubuntu

  1. Hello thanks a lot for the tutorial. I cannot complete step 14.
    If I do:
    python
    import tensorflow as tf
    I get:

    Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
    [GCC 7.3.0] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import tensorflow as tf
    Traceback (most recent call last):
    File “”, line 1, in
    File “/home/gbes/.local/lib/python2.7/site-packages/tensorflow/__init__.py”, line 24, in
    from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
    File “/home/gbes/.local/lib/python2.7/site-packages/tensorflow/python/__init__.py”, line 49, in
    from tensorflow.python import pywrap_tensorflow
    File “/home/gbes/.local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py”, line 74, in
    raise ImportError(msg)
    ImportError: Traceback (most recent call last):
    File “/home/gbes/.local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py”, line 58, in
    from tensorflow.python.pywrap_tensorflow_internal import *
    File “/home/gbes/.local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py”, line 28, in
    _pywrap_tensorflow_internal = swig_import_helper()
    File “/home/gbes/.local/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.

    See https://www.tensorflow.org/install/errors

    for some common reasons and solutions. Include the entire stack trace
    above this error message when asking for help.
    >>>
    ————————————
    And when I run from python 3
    >>> from tensorflow import tf
    Traceback (most recent call last):
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow.py”, line 58, in
    from tensorflow.python.pywrap_tensorflow_internal import *
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py”, line 28, in
    _pywrap_tensorflow_internal = swig_import_helper()
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py”, line 24, in swig_import_helper
    _mod = imp.load_module(‘_pywrap_tensorflow_internal’, fp, pathname, description)
    File “/usr/lib/python3.6/imp.py”, line 243, in load_module
    return load_dynamic(name, filename, file)
    File “/usr/lib/python3.6/imp.py”, line 343, in load_dynamic
    return _load(spec)
    ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File “”, line 1, in
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/__init__.py”, line 24, in
    from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/__init__.py”, line 49, in
    from tensorflow.python import pywrap_tensorflow
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow.py”, line 74, in
    raise ImportError(msg)
    ImportError: Traceback (most recent call last):
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow.py”, line 58, in
    from tensorflow.python.pywrap_tensorflow_internal import *
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py”, line 28, in
    _pywrap_tensorflow_internal = swig_import_helper()
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py”, line 24, in swig_import_helper
    _mod = imp.load_module(‘_pywrap_tensorflow_internal’, fp, pathname, description)
    File “/usr/lib/python3.6/imp.py”, line 243, in load_module
    return load_dynamic(name, filename, file)
    File “/usr/lib/python3.6/imp.py”, line 343, in load_dynamic
    return _load(spec)
    ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

    Failed to load the native TensorFlow runtime.

    See https://www.tensorflow.org/install/errors

    for some common reasons and solutions. Include the entire stack trace
    above this error message when asking for help.
    ———————————————————-
    I have no clue what to do next.
    I tried the virtual environment installation and it worked, but only for python and not for python3, and whenever I get out of the virtual environment it stops working.
    Thanks for the help

  2. Hello ! Thanks a lot for the tutorial.
    I followed it except for the version of CNN at my time of installation is version 2.4.2. I have the following mistake while importing tensorflow in python3 or python
    Python 3.6.7 (default, Oct 22 2018, 11:32:17)
    [GCC 8.2.0] on linux
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import tensorflow as tf
    Traceback (most recent call last):
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow.py”, line 58, in
    from tensorflow.python.pywrap_tensorflow_internal import *
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py”, line 28, in
    _pywrap_tensorflow_internal = swig_import_helper()
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py”, line 24, in swig_import_helper
    _mod = imp.load_module(‘_pywrap_tensorflow_internal’, fp, pathname, description)
    File “/usr/lib/python3.6/imp.py”, line 243, in load_module
    return load_dynamic(name, filename, file)
    File “/usr/lib/python3.6/imp.py”, line 343, in load_dynamic
    return _load(spec)
    ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File “”, line 1, in
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/__init__.py”, line 24, in
    from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/__init__.py”, line 49, in
    from tensorflow.python import pywrap_tensorflow
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow.py”, line 74, in
    raise ImportError(msg)
    ImportError: Traceback (most recent call last):
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow.py”, line 58, in
    from tensorflow.python.pywrap_tensorflow_internal import *
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py”, line 28, in
    _pywrap_tensorflow_internal = swig_import_helper()
    File “/usr/local/lib/python3.6/dist-packages/tensorflow/python/pywrap_tensorflow_internal.py”, line 24, in swig_import_helper
    _mod = imp.load_module(‘_pywrap_tensorflow_internal’, fp, pathname, description)
    File “/usr/lib/python3.6/imp.py”, line 243, in load_module
    return load_dynamic(name, filename, file)
    File “/usr/lib/python3.6/imp.py”, line 343, in load_dynamic
    return _load(spec)
    ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory

    Failed to load the native TensorFlow runtime.

    See https://www.tensorflow.org/install/errors

    for some common reasons and solutions. Include the entire stack trace
    above this error message when asking for help.

  3. Thank you so much for this extremely useful tutorial! I had to tweak some things because I needed to install different version of the software needed (such as cuDNN and NCCL), but other than that, your instructions were vital! Thank you again

  4. This is a great tutorial. I’ve finished installing CUDA 10.0. Stuck at Step 12: Installing Bazel. I get the following error:

    Uncompressing..[./bazel-0.17.2-installer-linux-x86_64.sh]
    End-of-central-directory signature not found. Either this file is not
    a zipfile, or it constitutes one disk of a multi-part archive. In the
    latter case the central directory and zipfile comment will be found on
    the last disk(s) of this archive.
    note: ./bazel-0.17.2-installer-linux-x86_64.sh may be a plain executable, not an archive
    unzip: cannot find zipfile directory in one of ./bazel-0.17.2-installer-linux-x86_64.sh or
    ./bazel-0.17.2-installer-linux-x86_64.sh.zip, and cannot find ./bazel-0.17.2-installer-linux-x86_64.sh.ZIP, period

    Can you please help?

  5. On part 9 I think will be best approach because will solve the issue related to cudnn.h library
    $ cd folder/extracted/contents
    $ sudo cp -P include/cudnn.h /usr/include
    $ sudo cp -P lib64/libcudnn* /usr/lib/x86_64-linux-gnu/
    $ sudo chmod a+r /usr/lib/x86_64-linux-gnu/libcudnn*

  6. I made whl file while using cuda-10.0 path but it is still finding libcublas.so.9.0
    Do you guys think that I need to make it one more time?

    • Remove old tensorflow first. Also check python and pip path, use pip -V It will be better to use virtualenv to create new environment then install the wheel to the environment.

  7. thank you so much, because of you,
    after 1000000000000 times of trial, at last did it!
    again, appreciate it

  8. Hey, this was super useful for me. I’ve tried installing Tensorflow-GPU 6 times prior to this and this one worked like a charm. Thank you. I’m using NVIDIA TESLA K80

Leave a Reply

Your email address will not be published.




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