Easy Steps to Install Tensorflow GPU with CUDA for Python 3


If you are into machine learning or parallel computing, TensorFlow is one of the frameworks you should be using. In this article, we will be installing Tensorflow GPU solution, along with CUDA Toolkit 9.1 and also cuDNN 7.1.2. The focus here will be the set up of your Ubuntu OS for proper usage of Tensorflow. We will also be using the current stable version of Ubuntu 16.04 64bit. Moreover, the installation will be done for Python 3.

Tensorflow is the most popular open source software library for competition purposes. By using Tensorflow, you can use the capabilities of your GPU and the processing power associated with it. Not only that both CPU and GPU processing power can be utilized because of API provided with it. A perfect solution for researchers, developers, and students who need high computational power to complete their project.

Things to keep in mind before following this guide:

1. Do a fresh install of Ubuntu. If you use virtualization, the guide will work fine. However, it is not recommended to use virtualization as you will not be able to access the full GPU and CPU power.

2. Make sure your GPU have compute capability of 3.0 or above. If your GPU doesn’t have the required compute power, the installation might fail.

3. Make sure that your Ubuntu is 64-bit. Tensorflow doesn’t work on 32-bit versions.

Software Version that we are going to install

We have already mentioned the version that we are going to install. However, before we start, let’s go through it once more.

Tensorflow: 1.7.0
Cuda ToolKit: 9.1
cuDNN: 1.7.2
Ubuntu: 16.04 – 64 bit version
Nvidia GPU Compute needed > 3.0
Python: 3.0

How to install Tensorflow GPU with CUDA Toolkit 9.1 and cuDNN 7.1.2 for Python 3 on Ubuntu 16.04-64bit

1. Updating your system
The first step is to update your system. This will ensure that you can download all the dependencies without any problem. Run the code in the terminal.

2. Check if your GPU is CUDA-Capable or not
The next step is to check if your GPU is CUDA-Capable or not. It can be done by using the simple command as below.

If the command doesn’t send back any output, this means that you need to update the database of the PCIe devices. To update it you need to run the below code.

Run the initial command again to see the status of your GPU. If it shows something like “GeForce 840M 5.0”, then the card is CUDA capable.

3. Check if Linux version is supported or not.
As we mentioned, we need to make sure that your Ubuntu version is 64-bit. It can be checked using the following command.

If the command returns x86_64, then you are good to go.

4. Dependencies
Our next step is to install the required dependencies. To do so, run the following command(s)

5. Installing the Linux Kernel Header
To install the header, you first need to find out the version of your kernel. Run the following command to do so.

Ifdone, note it down. The kernel version that our Ubuntu version had was, “4.10.0-28-generic”.

Now, run the following command with the same kernel version.

6. Getting the Nvidia CUDA toolkit and installing it.
You can get the Nvidia CUDA toolkit, either directly from their website: https://developer.nvidia.com/cuda-downloads. Once you are there, download the Linux installer. You can also use the direct download link if you want.
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.1.85-1_amd64.deb

To install it, you need to run the following four commands in sequence.

After successful installation, you will see a message as “done”
After it’s installation, you need to reboot so that nVidia drivers are loaded properly.

7. Adding Path
Open up the nano editor or any editor of your choice. We will use nano for this purpose. We will edit the .bashrc file.

The above command will open up the bashrc file. Add the lines of code at the end of the file.

To save the file, use the keyboard combo, crtl+x

Now, check if the installation is done properly or not by running the command below.

8. Installing cuDNN 7.1.2:
To install cuDNN 7.1.2, you need to go the official nVidia developer website and download the following files.

Developer Library: cuDNN v7.1.2 Developer Library for Ubuntu16.04 (Deb)
Runtime Library: cuDNN v7.1.2 Runtime Library for Ubuntu16.04 (Deb)
User Guide and Code Sample: cuDNN v7.1.2 Code Samples and User Guide for Ubuntu16.04 (Deb)

Once downloaded, you need to unpack them one by one. To do so, you need to run the following code.

Last, but not the least, you can verify it by typing the following commands.

If everything goes right, you will get a message of “Test passed!”

9. Installing final dependencies

– Bazel

– libcupti

After downloading these files, you need to install it using the following command.

10. Installing Tensorflow and configuring it
First, you need to reload the environment variables.

Now, it’s time to download TensorFlow. Let’s do it. To download the 1.7.0 version, type the following command(s)

Now the configuration setup will start. Enter the necessary details asked by configuration wizard. Press “Y” to the following option: Jemalloc as malloc support, Google Cloud Platform support, Hadoop File System support, Amazon S3 File System support, and CUDA support. All the other options should be set to “N.”

That’s it! You have completed installing Tensorflow GPU on your Linux machine.

Now, can you follow the above steps without any problems? Comment below and let us know.


Please enter your comment!
Please enter your name here