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

For 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-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

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: ~
cd ~/NVIDIA_CUDA-10.0_Samples/5_Simulations/nbody

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

  1. Hi,

    Thanks for the tutorial… i’m still unable to do it!

    I’m stuck at step 13:

    [email protected]:~/master/tensorflow/tensorflow$ bazel build –config=opt –config=cuda //tensorflow/tools/pip_package:build_pip_package
    WARNING: The following rc files are no longer being read, please transfer their contents or import their path into one of the standard rc files:
    INFO: Options provided by the client:
    Inherited ‘common’ options: –isatty=1 –terminal_columns=204
    INFO: Reading rc options for ‘build’ from /home/diicic/master/tensorflow/.tf_configure.bazelrc:
    ‘build’ options: –action_env PYTHON_BIN_PATH=/usr/bin/python3 –action_env PYTHON_LIB_PATH=/usr/lib/python3/dist-packages –python_path=/usr/bin/python3 –define with_ignite_support=true –define with_xla_support=true –action_env TF_NEED_OPENCL_SYCL=0 –action_env TF_NEED_ROCM=0 –action_env TF_NEED_CUDA=1 –action_env CUDA_TOOLKIT_PATH=/usr/local/cuda-10.0 –action_env TF_CUDA_VERSION=10.0 –action_env CUDNN_INSTALL_PATH=/usr/local/cuda-10.0 –action_env TF_CUDNN_VERSION=7 –action_env NCCL_INSTALL_PATH=/usr/lib/x86_64-linux-gnu –action_env NCCL_HDR_PATH=/usr/include –action_env TF_NCCL_VERSION=2 –action_env TF_CUDA_COMPUTE_CAPABILITIES=7.5 –action_env TF_CUDA_CLANG=0 –action_env GCC_HOST_COMPILER_PATH=/usr/bin/gcc –config=cuda
    ERROR: Config value cuda is not defined in any .rc file

    I had to install NCCL from deb package since I couldn’t find a tgz file…

    please help!

    • If you have configured cuda while running ./configure then you can exclude –config=cuda also use the same version of bazel mentioned in tutorial and use fresh virtualenv or uninstall previous tensorflow using pip. Also use release branch.

  2. During bazel build I still have the following error:

    ERROR: /home/me/tensorflow/cc/BUILD:422:1: Executing genrule //tensorflow/cc:user_ops_genrule failed (Exit 127)
    bazel-out/host/bin/tensorflow/cc/ops/user_ops_gen_cc: error while loading shared libraries: cannot open shared object file: No such file or directory

    Is there any solution for this?

    • I think you have configured for cuda 9.2 so it is seeking for 9.2 related libraries. If you want to build for cuda 9.2 than install cuda 9.2 first.

  3. Worked perfectly on my new RTX 2080. I have done about 10 failed attempts but this one was spot-on. Hero!

  4. Dear Mr Mandal

    It’s my first time to install Linux OS(Ubuntu 16.04 LTS) and tensorflow with GPU version(Actually, I want to install Keras and Theano). Following your instructions step by step in terminal, everthing is ok. However, when I close the terminal and open it again, Step 14 can’t be finished successfully. When I type
    “python” and “import tensorflow as tf”, then errors happen:
    Python 2.7.12 (default, Dec 4 2017, 14:50:18)
    [GCC 5.4.0 20160609] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import tensorflow as tf
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named tensorflow

    When I type “python”,”import tensorflow as tf” and “hello = tf.constant(‘Hello, TensorFlow!’)”
    ,then errors happen:


    Python 3.5.2 (default, Nov 23 2017, 16:37:01)
    [GCC 5.4.0 20160609] on linux
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import tensorflow as tf
    >>> hello = tf.constant(‘Hello, TensorFlow!’)
    Traceback (most recent call last):
    File “”, line 1, in
    AttributeError: module ‘tensorflow’ has no attribute ‘constant’

    If I run in terminal

    cd tensorflow

    cd tensorflow_pkg

    source tf_1.12_cuda10.0/bin/activate


    import tensorflow as tf
    hello = tf.constant(‘Hello, TensorFlow!’)
    sess = tf.Session()

    then the terminal output right results. It seems the problem is related to the installation path. But I don not know how to solve it.

    When I install Theano, I type “sudo pip install theano” or “~sudo pip3 install theano”,errors happen:


    sudo pip install theano
    Traceback (most recent call last):
    File “/usr/bin/pip”, line 9, in
    from pip import main
    ImportError: cannot import name main
    sudo pip3 install theano
    Traceback (most recent call last):
    File “/usr/bin/pip3”, line 9, in
    from pip import main
    ImportError: cannot import name ‘main’

    When I install Keras, I type “sudo pip install keras” or “sudo pip3 install keras”,errors happen:


    sudo pip install keras
    Traceback (most recent call last):
    File “/usr/bin/pip”, line 9, in
    from pip import main
    ImportError: cannot import name main


    sudo pip3 install keras
    Traceback (most recent call last):
    File “/usr/bin/pip3”, line 9, in
    from pip import main
    ImportError: cannot import name ‘main’


    Alternatively, I install Keras from GitHub:

    git clone

    cd keras

    sudo python3 install

    The installation is completed:

    sudo python3 install

    However, when I run the example, errors happen:


    python3 examples/
    Using TensorFlow backend.
    Traceback (most recent call last):
    File “examples/”, line 9, in
    import keras
    File “/usr/local/lib/python3.5/dist-packages/Keras-2.2.4-py3.5.egg/keras/”, line 3, in
    File “/usr/local/lib/python3.5/dist-packages/Keras-2.2.4-py3.5.egg/keras/utils/”, line 6, in
    File “/usr/local/lib/python3.5/dist-packages/Keras-2.2.4-py3.5.egg/keras/utils/”, line 9, in
    File “/usr/local/lib/python3.5/dist-packages/Keras-2.2.4-py3.5.egg/keras/backend/”, line 89, in
    File “/usr/local/lib/python3.5/dist-packages/Keras-2.2.4-py3.5.egg/keras/backend/”, line 5, in
    ImportError: No module named ‘tensorflow’

    I can’t solve these problems. Can you help me? Thanks.

    • I think you have used python from source dir of tensorflow. It is clearely written in tutorial. Cd to any other dir then run python.

      • What does that mean? I am running the program on : /home/pi/Documents/.
        And the keras is installed : /usr/local/lib/python3.5/dist-packages (tensorflow as well as keras).
        How do I move the installation outside /usr/local/lib/python3.5?
        I understand, it is unable to check for python program within this directory so unable to run python itself.

        Then is there anyway to keep keras and tensorflow installation targets outside of this place?

        • Hello this is a great post, but i am having the following error and I do not know what to do after step 13 I get the following error bazel build –config=opt –config=cuda //tensorflow/tools/pip_package:build_pip_package

          Error:tensorflow/BUILD:533:1: Executing genrule //tensorflow:tf_python_api_gen_v1 failed (Exit 1)

2 Trackbacks & Pingbacks

  1. How to install Tensorflow GPU with CUDA 10.0 for python on Windows | Python 3.6
  2. How to install Tensorflow 1.7.0 using official pip package | Python 3.6

Leave a Reply

Your email address will not be published.

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