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



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

  1. Hi there.
    I tried to install python2.7 but when I tried to build pip on step 13, I got error as follow:

    ERROR: /home/ai2/tensorflow/tensorflow/BUILD:533:1: Executing genrule //tensorflow:tf_python_api_gen_v1 failed (Exit 1)
    Traceback (most recent call last):
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/tools/api/generator/create_python_api.py”, line 27, in
    from tensorflow.python.tools.api.generator import doc_srcs
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/__init__.py”, line 70, in
    from tensorflow.python.framework.framework_lib import * # pylint: disable=redefined-builtin
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/framework/framework_lib.py”, line 52, in
    from tensorflow.python.framework.importer import import_graph_def
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/framework/importer.py”, line 27, in
    from tensorflow.python.framework import function
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/framework/function.py”, line 37, in
    from tensorflow.python.ops import variable_scope as vs
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/ops/variable_scope.py”, line 206, in
    “””
    AttributeError: ‘int’ object attribute ‘__doc__’ is read-only
    Target //tensorflow/tools/pip_package:build_pip_package failed to build
    Use –verbose_failures to see the command lines of failed build steps.
    INFO: Elapsed time: 1.538s, Critical Path: 0.78s
    INFO: 0 processes.
    FAILED: Build did NOT complete successfully

  2. ERROR: /home/ai2/tensorflow/tensorflow/BUILD:533:1: Executing genrule //tensorflow:tf_python_api_gen_v1 failed (Exit 1)
    Traceback (most recent call last):
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/tools/api/generator/create_python_api.py”, line 27, in
    from tensorflow.python.tools.api.generator import doc_srcs
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/__init__.py”, line 70, in
    from tensorflow.python.framework.framework_lib import * # pylint: disable=redefined-builtin
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/framework/framework_lib.py”, line 52, in
    from tensorflow.python.framework.importer import import_graph_def
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/framework/importer.py”, line 27, in
    from tensorflow.python.framework import function
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/framework/function.py”, line 37, in
    from tensorflow.python.ops import variable_scope as vs
    File “/home/ai2/.cache/bazel/_bazel_ai2/55bd9e231767610e349e09a8b377d60e/execroot/org_tensorflow/bazel-out/host/bin/tensorflow/create_tensorflow.python_api_1.runfiles/org_tensorflow/tensorflow/python/ops/variable_scope.py”, line 206, in
    “””
    AttributeError: ‘int’ object attribute ‘__doc__’ is read-only
    Target //tensorflow/tools/pip_package:build_pip_package failed to build
    Use –verbose_failures to see the command lines of failed build steps.
    INFO: Elapsed time: 1.538s, Critical Path: 0.78s
    INFO: 0 processes.
    FAILED: Build did NOT complete successfully

  3. Hello Arun,

    Thanks for the detailed article. I am stuck at the following line –

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

    Gives me the output-
    WARNING: The following configs were expanded more than once: [cuda]. For repeatable flags, repeats are counted twice and may lead to unexpected behavior.
    ERROR: /home/user1/tensorflow/WORKSPACE:3:1: name ‘http_archive’ is not defined
    ERROR: Error evaluating WORKSPACE file
    ERROR: error loading package ”: Encountered error while reading extension file ‘closure/defs.bzl’: no such package ‘@io_bazel_rules_closure//closure’: error loading package ‘external’: Could not load //external package
    ERROR: error loading package ”: Encountered error while reading extension file ‘closure/defs.bzl’: no such package ‘@io_bazel_rules_closure//closure’: error loading package ‘external’: Could not load //external package
    INFO: Elapsed time: 0.127s
    INFO: 0 processes.

    Can you pls help?

  4. Hello Arun,

    Thanks for the detailed article. I am stuck at the following line –

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

    Gives me the output-
    WARNING: The following configs were expanded more than once: [cuda]. For repeatable flags, repeats are counted twice and may lead to unexpected behavior.
    ERROR: /home/user1/tensorflow/WORKSPACE:3:1: name ‘http_archive’ is not defined
    ERROR: Error evaluating WORKSPACE file
    ERROR: error loading package ”: Encountered error while reading extension file ‘closure/defs.bzl’: no such package ‘@io_bazel_rules_closure//closure’: error loading package ‘external’: Could not load //external package
    ERROR: error loading package ”: Encountered error while reading extension file ‘closure/defs.bzl’: no such package ‘@io_bazel_rules_closure//closure’: error loading package ‘external’: Could not load //external package
    INFO: Elapsed time: 0.127s
    INFO: 0 processes.

    Can you pls help?

  5. Hi, thank you for this tutorial, it is very useful and helped me a lot!

    When I run ‘bazel build –config=opt –config=cuda //tensorflow/tools/pip_package:build_pip_package’, I get the following error:

    WARNING: The following configs were expanded more than once: [cuda]. For repeatable flags, repeats are counted twice and may lead to unexpected behavior.
    DEBUG: /home/rian/.cache/bazel/_bazel_rian/c2dfeece50f3a3a340ea5bfd5a3559fb/external/bazel_tools/tools/cpp/lib_cc_configure.bzl:115:5:
    Auto-Configuration Warning: ‘TMP’ environment variable is not set, using ‘C:\Windows\Temp’ as default
    ERROR: /home/rian/tensorflow/third_party/python_runtime/BUILD:5:1: no such package ‘@local_config_python//’: Traceback (most recent call last):
    File “/home/rian/tensorflow/third_party/py/python_configure.bzl”, line 308
    _create_local_python_repository(repository_ctx)
    File “/home/rian/tensorflow/third_party/py/python_configure.bzl”, line 270, in _create_local_python_repository
    _check_python_lib(repository_ctx, python_lib)
    File “/home/rian/tensorflow/third_party/py/python_configure.bzl”, line 213, in _check_python_lib
    _fail((“Invalid python library path: %…))
    File “/home/rian/tensorflow/third_party/py/python_configure.bzl”, line 28, in _fail
    fail((“%sPython Configuration Error:%…)))
    Python Configuration Error: Invalid python library path: /usr/bin/python3
    and referenced by ‘//third_party/python_runtime:headers’
    ERROR: Analysis of target ‘//tensorflow/tools/pip_package:build_pip_package’ failed; build aborted: Analysis failed
    INFO: Elapsed time: 1.242s
    INFO: 0 processes.
    FAILED: Build did NOT complete successfully (3 packages loaded)
    currently loading: tensorflow/core/kernels … (3 packages)

    Anyone know how to resolve this issue?

    Many thanks

  6. Hi there
    Ive got stock at step 13 when I am building bazel pkg:
    I installed Cuda 10.1 and Cudnn 7.5.1 which was compatible with that.
    when I am trying to build the package i receive the error says:

    Cuda Configuration Error: Cannot find cuda library libcublas.so.10.1
    WARNING: Target pattern parsing failed.
    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/amir/tensorflow/third_party/gpus/cuda_configure.bzl”, line 1447
    _create_local_cuda_repository(repository_ctx)
    File “/home/amir/tensorflow/third_party/gpus/cuda_configure.bzl”, line 1220, in _create_local_cuda_repository
    _find_libs(repository_ctx, cuda_config)
    File “/home/amir/tensorflow/third_party/gpus/cuda_configure.bzl”, line 799, in _find_libs
    _find_cuda_lib(“cublas”, repository_ctx, cpu_value, c…, …)
    File “/home/amir/tensorflow/third_party/gpus/cuda_configure.bzl”, line 720, in _find_cuda_lib
    auto_configure_fail((“Cannot find cuda library %s” %…))
    File “/home/amir/tensorflow/third_party/gpus/cuda_configure.bzl”, line 317, in auto_configure_fail
    fail((“\n%sCuda Configuration Error:%…)))

    Cuda Configuration Error: Cannot find cuda library libcublas.so.10.1
    INFO: Elapsed time: 0.107s
    INFO: 0 processes.
    FAILED: Build did NOT complete successfully (0 packages loaded)
    currently loading: tensorflow/tools/pip_package

  7. Dear Arun,
    thank you very much for the tutorial. I ran it once, and everything was working as it was supposed to.
    However, when nvidida and the kernel got updated, I got the error message ‘Failed to start nvidia persistence daemon’.
    Re-installing my system and re-running the tutorial got me to step 7. Upon the reboot, I got to the lock-in screen but the the screen blackened and was not usable any more.

    I am running Ubuntu 18.04, Kernel 4.15.0-46-generic, Nvidia Quadro K5200.
    Do you have any idea if there is another solution to the problem, or generally, what to do in this situation, meaning how to keep the system stable and updated?

  8. $ uname -r
    4.15.0-43-generic

    $ nvidia-smi
    Failed to initialize NVML: Driver/library version mismatch

Leave a Reply

Your email address will not be published.




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