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.

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

  1. Thanks for tutorial but i get this error can you please help me ?
    Invalid path to CUDA 9.2 toolkit. /usr/local/cuda-9.2/lib64/libcudart.so.9.2 cannot be found

  2. I am getting the following error at Step 12:
    Invalid path to CUDA 9.2 toolkit. /usr/local/cuda-9.2/lib64/libcudart.so.9.2 cannot be found

    I’ve followed it step by step as mentioned.
    I checked the /usr/local/cuda-9.2 directory, there is no file with name libcudart.so.9.2 (there is symbolic link, but the link is invalid).
    What could be a possible issue?

  3. Can you hypothetically use this for r1.9 now? They only difference I think should be in pull request step.

    I also have problem with nvidia-smi, I keep getting this:

    “NVIDIA-SMI has failed because it couldn’t communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.”

    Even though it seems to installed correctly. I had problem installing ans install was stuck at some point like this : https://github.com/teejee2008/ukuu/issues/81#issuecomment-397867269

    I think it is installed correctly but I can’t run SMI… any other way to confirm CUDA installation?

    • If you can’t run nvidia-smi then there is problem with communication to gpu. It may be due to faulty driver, unsupported kernel, faulty gpu. There is not problem with Tensorflow. Check your kernel and downgrading it may fix this. Also you can install driver only from nvidia site then issue nvidia-smi to check if gpu is fully working.

  4. Hi. I followed all the steps up to 8, but I got nvidia-smi: command not found.
    Is my linux kernel not supported?
    uname -r
    4.13.0-43-generic
    Thank you for the tutorial

    • Use kernel 4.10 from boot menu > advanced options and start from first step. Also seek in comments for further info.

  5. Wow this stuff ages fast:
    E: Unable to locate package cuda-9-2
    E: Unable to locate package cuda-drivers

  6. Thanks for the guide. After following everything, I get “AttributeError: module ‘tensorflow’ has no attribute ‘constant'” when I try and verify the installation. Any idea what’s causing this?

  7. I did the full installation per your excellent tutorial and did the validation of the TF Hello code successfully but all seems to be working on CPU and not gpu. When I checked devices I could only see CPU and no gpu. I then downgraded to cuda 9.0 with TF 1.4 and still have the same problem. I cannot seem able to get TF run on gpu. Please advise Thank you

  8. Hi, nice tuto; tahnsk ! I see small error in step 9 :
    sudo cp -R cuda/lib64/* /usr/local/cuda-9.2/lib6
    instead of
    sudo cp -R cuda/lib64/* /usr/local/cuda-9.2/lib64

  9. Your tutorial was excellent, well done on the clarity of presentation and steps. I did all the steps per your instructions on Ubuntu 16.04 and all were very successful to install TF 1.8 with Cuda 9.2 and did the last step of validation of TF and that worked. However I thought all along I was using gpu and not CPU. When I checked the devices it on the ipython terminal it only showed CPU and no gpu. How can I get the TF gpu working or get TF running on gpu. Why I cannot see the gpu device. Please advise. Thank you.

    • Hi, when you run tf.Session() it will display information about gpu. If it is not displaying about gpu then make sure that you have correctly configured to enable CUDA while running ./configure.

        • Well in a constant effort to know why TF 1.8 is not running on gpu with cuda 9.2, I installed the nvidia cuda9.2 samples which are not covered in this tutorial but realised they are important as they can point to whether there is an active gpu device and consistent with the hardware of the system. In brief I compiled and then ran. /deviceQuery. If all is configured correctly then you get a table which shows the gpu device and the consistency with the hardware and the rest passed! So in otherwords the configuration of cuda 9.2 is all correct and yet TF 1.8 cannot run in gpu ( which is tested by running tf.session()). Has anyone seen this or have any ideas, I have exhausted all attempts after fee days of trying. Something is preventing the gpu to kick in and I don’t know how to solve this. Anyone help is greatly appreciated. Thx

            • Thank you Arun, will do but yes nvidia -smi is running, not only that but after compiled cuda 9.2 successfully and run the test. /deviceQuery per nvidia test I got the table as on their website for cuda 9.2 installation which proves that cuda is configured and running correctly. I hope your suggestion will solve this puzzle

            • Thank you Arun, will do. yes nvidia -smi is running, not only that but after compiled cuda 9.2 samples successfully, i also ran the test ./deviceQuery per nvidia post installation validation I got the table as on their website for cuda 9.2 installation which proves that cuda is configured and running correctly. I hope your suggestion will solve this puzzle

            • Hi Arun , first I truly thank you for listening to my issues and try to help . greatly appreciated . I downloaded your whl folder. I created an env tensorflow1.8 within the environment within miniconda3 but I am afraid i still when i check devices I only see cpu . When I tried to enforce gpu using this command in my py file : with tf.device(‘/gpu:0’):…….
              I get the following errors:
              runfile(‘/home/na/.config/spyder-py3/temp.py’, wdir=’/home/na/.config/spyder-py3′)
              /home/na/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
              from ._conv import register_converters as _register_converters
              [name: “/device:CPU:0”
              device_type: “CPU”
              memory_limit: 268435456
              locality {
              }
              incarnation: 13712653139854458133
              ]
              Traceback (most recent call last):

              File “”, line 1, in
              runfile(‘/home/na/.config/spyder-py3/temp.py’, wdir=’/home/na/.config/spyder-py3′)

              File “/home/na/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”, line 705, in runfile
              execfile(filename, namespace)

              File “/home/na/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”, line 102, in execfile
              exec(compile(f.read(), filename, ‘exec’), namespace)

              File “/home/na/.config/spyder-py3/temp.py”, line 17, in
              print (sess.run(c))

              File “/home/na/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, line 900, in run
              run_metadata_ptr)

              File “/home/na/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, line 1135, in _run
              feed_dict_tensor, options, run_metadata)

              File “/home/na/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, line 1316, in _do_run
              run_metadata)

              File “/home/na/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py”, line 1335, in _do_call
              raise type(e)(node_def, op, message)

              InvalidArgumentError: Cannot assign a device for operation ‘MatMul’: Operation was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0 ]. Make sure the device specification refers to a valid device.
              [[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device=”/device:GPU:0″](a, b)]]

              Caused by op ‘MatMul’, defined at:
              File “/home/na/anaconda3/lib/python3.6/site-packages/spyder/utils/ipython/start_kernel.py”, line 269, in
              main()
              File “/home/na/anaconda3/lib/python3.6/site-packages/spyder/utils/ipython/start_kernel.py”, line 265, in main
              kernel.start()
              File “/home/na/anaconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py”, line 478, in start
              self.io_loop.start()
              File “/home/na/anaconda3/lib/python3.6/site-packages/zmq/eventloop/ioloop.py”, line 177, in start
              super(ZMQIOLoop, self).start()
              File “/home/na/anaconda3/lib/python3.6/site-packages/tornado/ioloop.py”, line 888, in start
              handler_func(fd_obj, events)
              File “/home/na/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py”, line 277, in null_wrapper
              return fn(*args, **kwargs)
              File “/home/na/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py”, line 440, in _handle_events
              self._handle_recv()
              File “/home/na/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py”, line 472, in _handle_recv
              self._run_callback(callback, msg)
              File “/home/na/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py”, line 414, in _run_callback
              callback(*args, **kwargs)
              File “/home/na/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py”, line 277, in null_wrapper
              return fn(*args, **kwargs)
              File “/home/na/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py”, line 283, in dispatcher
              return self.dispatch_shell(stream, msg)
              File “/home/na/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py”, line 233, in dispatch_shell
              handler(stream, idents, msg)
              File “/home/na/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py”, line 399, in execute_request
              user_expressions, allow_stdin)
              File “/home/na/anaconda3/lib/python3.6/site-packages/ipykernel/ipkernel.py”, line 208, in do_execute
              res = shell.run_cell(code, store_history=store_history, silent=silent)
              File “/home/na/anaconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py”, line 537, in run_cell
              return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
              File “/home/na/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py”, line 2728, in run_cell
              interactivity=interactivity, compiler=compiler, result=result)
              File “/home/na/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py”, line 2856, in run_ast_nodes
              if self.run_code(code, result):
              File “/home/na/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py”, line 2910, in run_code
              exec(code_obj, self.user_global_ns, self.user_ns)
              File “”, line 1, in
              runfile(‘/home/na/.config/spyder-py3/temp.py’, wdir=’/home/na/.config/spyder-py3′)
              File “/home/na/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”, line 705, in runfile
              execfile(filename, namespace)
              File “/home/na/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py”, line 102, in execfile
              exec(compile(f.read(), filename, ‘exec’), namespace)
              File “/home/na/.config/spyder-py3/temp.py”, line 14, in
              c = tf.matmul(a, b)
              File “/home/na/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py”, line 2122, in matmul
              a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
              File “/home/na/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py”, line 4279, in mat_mul
              name=name)
              File “/home/na/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py”, line 787, in _apply_op_helper
              op_def=op_def)
              File “/home/na/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, line 3392, in create_op
              op_def=op_def)
              File “/home/na/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py”, line 1718, in __init__
              self._traceback = self._graph._extract_stack() # pylint: disable=protected-access

              InvalidArgumentError (see above for traceback): Cannot assign a device for operation ‘MatMul’: Operation was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0 ]. Make sure the device specification refers to a valid device.
              [[Node: MatMul = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false, _device=”/device:GPU:0″](a, b)]]

              help please as I cannot run deep learning ConvNN efficiently on CPU

            • Check your python package is from virtual environment it should not be from this “/home/na/anaconda3/lib/python3.6/site-packages/tensorflow/python/client/session.py” see the last part of tutorial to create virtual environment using virtualenv. Check targeted python using pip -V.
              Or if you want to install into ‘/home/na/anaconda3/lib/python3.6/site-packages/’ then use pip install –upgrade –force-reinstall tensorflow*whl

          • If you are able to run nvidia-smi then you have active gpu. If your cuda9.2 installation is bad then tensorflow will not build successfully if configured which cuda enabled.

  10. I’m getting
    “tensorflow-1.8.0-cp36-cp36m-linux_x86_64.whl is not a supported wheel on this platform.”
    error on Ubuntu16.04 1050ti

    • Make sure to install it to python3.6, create virtualenv with python 3.6 and use pip to install.

Leave a Reply

Your email address will not be published.




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