Install GPU version of tensorflow

Step by step installation of CUDA Toolkit, cuDNN and tensorflow GPU version for python 3.5 and python3.6

The objective of this tutorial is to help you install GPU version of tensorflow on python version 3.6 on 64 bit Ubuntu. We will be installing the tensorflow GPU version 1.0.0 along with CUDA toolkit 8.0 and cuDNN 5.1. If you are looking to install the latest version of tensorflow instead, I recommend you check out, How to install Tensorflow 1.5.0 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. While it is technically possible to install GPU version of tensorflow 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 before starting with the tutorial.

All right, let’s get to it now. First, check to see if your Nvidia driver is installed. Open up a terminal and type:

lspci -v

You will see something like: Check if nvidia driver is installed via terminal

Since I have NVIDIA GeForce 840M on my computer, I am seeing this on my screen. Your result may be different depending on what graphics card your computer has.

If you don’t have the graphics drivers installed, you can install it easily via terminal using the command:

sudo apt-get install nvidia-current-updates nvidia-settings

Now that you have your graphics driver installed, you are ready to install GPU version of tensorflow. You can check the appropriate version of CUDA Toolkit and cuDNN for the corresponding tensorflow version in the link.  Scroll to the bottom of the page and you’ll see something like:

tensorflow gpu cudnn and cuda version

The steps needed to be taken in order to install GPU version of tensorflow for our selected version are as follows:

  1. Install CUDA Toolkit 8.0
  2. Download cuDNN 5.1, adding it’s contents to your CUDA directory
  3. Install GPU version of TensorFlow

Install CUDA Toolkit 8.0

Now, to install CUDA Toolkit 8.0, you will need to have a CUDA developer account. If you do not have one, register for it, and then you can log in and access the downloads. To download the file, click here. It will redirect you to nvidia’s developer portal download page for CUDA 8.0. Download the runfile.

Download CUDA Toolkit 8.0

Once the download is completed, install the CUDA Toolkit 8.0. Navigate to wherever you saved the .run file (by default Downloads folder). Run the command ./cuda_8.0.61_375.26_linux.run.

Press and hold the space key for a few seconds and wait for the license agreement to pass by. Accept the agreement. Be careful NOT TO install the graphics driver. Make sure you say no to that, otherwise say yes to everything else, and keep the defaults for paths. When all is said and done, it will likely say you didn’t fully install it, since you didn’t install the graphics drivers. That’s fine, just make sure it says that you installed the Toolkit. If not, read the error and see why it failed. You should see something like:

Cuda 8.0 installation

Now that the CUDA Toolkit 8.0 is installed, we need to download cuDNN 5.1, adding it’s contents to the CUDA directory.

Download cuDNN 5.1, adding it’s contents to your CUDA directory

Now we need to download and setup the cuDNN files. Download cuDNN 5.1. Go to the downloads directory, and extract this. If you are on GUI, you can just right click, and extract. This will extract to a folder called cuda, which we will need to merge with our official CUDA directory, located: /usr/local/cuda/. To do this, open a terminal to your downloads and type the commands below:

cd ~/Downloads
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

Now, finally, we just need to export the system path to CUDA elements:

$ sudo nano ~/.bashrc

Go to the very end of this file, and add:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
export CUDA_HOME=/usr/local/cuda

Now we are ready to install GPU version of tensorflow.

Install GPU version of tensorflow

Here, we will be using the version for Python 3.6 on 64 bit Linux, so, type the command below: If you want to learn how to install python 3.6 on Ubuntu, check out this other tutorial, Install python 3.6 on Ubuntu.

sudo apt-get install python3-pip python3-dev

export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.0.0-cp36-cp36m-linux_x86_64.whl

sudo pip3 install --upgrade $TF_BINARY_URL

If you’re using Python 3.5 (the default version of python on ubuntu) for the second command, use this instead:

export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.0.0-cp35-cp35m-linux_x86_64.whl

Now let’s check to see if tensorflow is installed correctly. Open your python 3.6 terminal

python3.6

Now do

import tensorflow

You should see something like:

Install tensorflow GPU on python 3.6

Congratulation! You have successfully installed GPU version of tensorflow on python 3.6.

 

1 Trackbacks & Pingbacks

  1. How to install Tensorflow 1.4.1 GPU with CUDA Toolkit 9.1 and cuDNN 7.0.5 for Python 3 on Ubuntu 16.04-64bit | Python 3.6

Leave a Reply

Your email address will not be published.