CUDA and CUDNN installation for tensorflow gpu

Deepak Mishra
2 min readApr 19, 2018

--

Hey guys, today I am going to share the commands to install cuda and cudnn to run tensorflow on gpu. I faced used trouble in installing cuda and its dependencies on ubuntu. Lets start the installation.

CUDA-9.0 installation

wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
sudo dpkg -i cuda-repo-ubuntu1604–9–0-local_9.0.176–1_amd64-deb

As it finishes it will display a command and will ask you to add a key. Run that command. If no command to add key is displayed then run

sudo apt-key add /var/cuda-repo-9–0-local/7fa2af80.pub

Then to complete the cuda installation run-

sudo apt-get update
sudo apt-get -y install cuda-9.0
sudo reboot

To check whether cuda is installed correctly and to check its version we need to install nvidia-cuda-toolkit.

sudo apt install nvidia-cuda-toolkit
nvcc — version

Now we need to install cublas

wget https://developer.nvidia.com/compute/cuda/9.0/Prod/patches/1/cuda-repo-ubuntu1604-9-0-local-cublas-performance-update_1.0-1_amd64-deb
sudo dpkg -i cuda-repo-ubuntu1604–9–0-local-cublas-performance-update_1.0–1_amd64-deb
sudo apt-get update
sudo apt-get upgrade -y

Then we have to install CUDNN version7

wget https://github.com/ashokpant/cudnn_archive/raw/master/v7.0/libcudnn7_7.0.5.15-1+cuda9.0_amd64.deb
sudo dpkg -i libcudnn7_7.0.5.15–1+cuda9.0_amd64.deb
sudo apt-get update
sudo apt-get upgrade -y

Now we need cuda Profile Tools Interface

sudo apt-get install cuda-command-line-tools-9–0

We are done with cuda and cudnn installation, now we need to add their path to bashrc file to set environment variables

sudo vi ~/.bashrc

Add the following lines at the bottom of bashrc file and save it.

export PATH=${PATH}:/usr/local/cuda-9.0/binexport CUDA_HOME=${CUDA_HOME}:/usr/local/cuda:/usr/local/cuda-9.0export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda-9.0/lib64export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/extras/CUPTI/lib64

Then update bashrc file using the following command:

source ~/.bashrc

The next step is to reboot and install tensorflow gpu.

sudo reboot
sudo apt-get update
sudo apt-get upgrade -y
sudo pip3 install — upgrade tensorflow-gpu

To check whether your tensorflow is using gpu or not , execute the following python code.

import tensorflow as tf
if tf.test.gpu_device_name():
print(‘Default GPU Device: {}’.format(tf.test.gpu_device_name()))
else:
print(“Please install GPU version of TF”)

This is it. Hope you guys find it useful. Feel free to comment.

--

--

Deepak Mishra

Manager RnD @Tata Communications Ltd, Delhi, India. Graduated from DA-IICT.