Easy Steps to Install Tensorflow GPU with CUDA for Python 3

0
1715

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.

sudo apt-get update 
sudo apt-get upgrade

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.

lspci | grep -i nvidia

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.

update-pciids

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.

uname -m && cat /etc/*release

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)

sudo apt-get install build-essential 
sudo apt-get install cmake git unzip zip 
sudo apt-get install python2.7-dev python3.5-dev python3.6-dev pylint

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.

uname -r

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.

sudo apt-get install linux-headers-$(uname -r)

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.

sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo dpkg -i cuda-repo-ubuntu1604_9.1.85-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda-9.1

After successful installation, you will see a message as “done”
message-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.

nano ~/.bashrc

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

export PATH=/usr/local/cuda-9.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-9.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

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.

source ~/.bashrc
sudo ldconfig
nvidia-smi

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.

sudo dpkg -i libcudnn7_7.1.2.21-1+cuda9.1_amd64.deb
sudo dpkg -i libcudnn7-dev_7.1.2.21-1+cuda9.1_amd64.deb
sudo dpkg -i libcudnn7-doc_7.1.2.21-1+cuda9.1_amd64.deb

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

cp -r /usr/src/cudnn_samples_v7/ $HOME
cd  $HOME/cudnn_samples_v7/mnistCUDNN
make clean && make
./mnistCUDNN

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

9. Installing final dependencies

– Bazel

sudo apt-get install openjdk-8-jdk
wget https://github.com/bazelbuild/bazel/releases/download/0.11.1/bazel_0.11.1-linux-x86_64.deb
sudo dpkg -i bazel_0.11.1-linux-x86_64.deb

– libcupti

sudo apt-get install libcupti-dev
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc

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

sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel

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

source ~/.bashrc
sudo ldconfig

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

wget https://github.com/tensorflow/tensorflow/archive/v1.7.0.zip
unzip v1.7.0.zip
cd tensorflow-1.7.0
./configure

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here