Making your Ubuntu deep learning ready

Installing Nvidia driver, Cuda, cuDNN, TensorFlow, Keras & PyTorch in Ubuntu

Kantajit Shaw
Towards Data Science

--

Photo by freestocks on Unsplash

I have stumbled upon many guides to install deep learning libraries, Cuda, cuDNN & nvidia-driver. But I have never been successful in one go following only one guide. So, I decided to write this article. First, this guide is to install deep learning libraries ( TensorFlow, Keras, PyTorch) with GPU support. CPU support is pretty straight-forward, so I won’t be talking about that.

You need a

NVIDIA® GPU card with CUDA® Compute Capability 3.5 or higher.

You can check whether your GPU is compatible or not by visiting this link.

Installing Nvidia driver

Run these following commands. It adds the necessary repository to download the Nvidia driver. It will

sudo add-apt-repository ppa:graphic-drivers/ppasudo apt updateubuntu-drivers devices | grep nvidia

It will show you the driver versions compatible with your GPU card. I have an RTX 2060 super GPU card. It shows

driver : nvidia-430 — third-party free recommended

It may also show multiple entries depending on the GPU card you are using. Example

driver : nvidia-340 — distro non-free
driver : nvidia-304 — distro non-free
driver : nvidia-384 — distro non-free recommended

Install the suitable version. Your driver will decide the minimum version of CUDA you are going to install. You can find the table here.

sudo apt install nvidia-xxx

Repace xxx with your desired version. Reboot and check the installation

sudo reboot
lsmod|grep nvidia
nvidia-smi

Installing CUDA and cuDNN

Download the CUDA installer from here. I prefer installing from the runfile. Install and then add the paths in PATH and LD_LIBRARY_PATH variable. Replace the path of Cuda (in my case /usr/local/cuda-10.1/ ) with your path.

./cuda_10.1.105_418.39_linux.runecho “export LD_LIBRARY_PATH=/usr/local/cuda-10.1/lib64:$LD_LIBRARY_PATH”>> ~/.bashrcecho “export PATH=/usr/local/cuda-10.1/bin:$PATH”>> ~/.bashrc

Visit this table get the tested build versions. Check the version of cuDNN you need to install. Download the cuDNN from here. I prefer the zipped version.

After downloading unzip and copy the files to CUDA directory.

tar -xzvf cudnn-x.x-linux-x64-v8.x.x.x.tgz
sudo cp cuda/include/cudnn*.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*

Install Tensorflow, Keras, Pytorch

Install TensorFlow and Keras using

pip install tensorflow-gpu==2.2.0 keras

To install PyTorch with GPU support visit this link. Select Version, OS, Language, package installer, CUDA version and then follow the highlighted portion of the following image to install.

There you have it. Now verify your installation using these python scripts.

Tensorflow code

from tensorflow.python.client import device_lib

def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.physical_device_desc for x in local_device_protos if x.device_type == 'GPU']
get_available_gpus()

Pytorch code

import torchdef get_available_gpus():
return [ torch.cuda.get_device_properties(i) for i in range(torch.cuda.device_count())]
get_available_gpus()

There you have it.

All the libraries are installed. Now turn your CREATIVE mode on.

References

https://stackoverflow.com/a/38580201

--

--

Deep learning enthusiast, interested in Computer Vision and Natural Language Processing problems.