What you’ll need

An Nvidia GPU (of course, where else will you get CUDA cores?)

A Linux Distro (since I’m talking solely about Linux Distros)

Internet connection

Anaconda for Python 3(things will be somewhat same for Python2 as well)

You can also use the Python that came with your system. Create a venv to avoid hassle.

Patience, you’ll need it.

Getting started

Now I’m assuming you already have downloaded and installed Anaconda or planning to use your system supplied Python. Whatever it is, let’s get started setting up an environment first.

A note though, for Deep Learning, Machine Learning, Data Science bla bla, using Anaconda is always beneficial.

Yeah you should get the reference

Setting up Python

Head over to https://www.anaconda.com/download/#linux and download either the Python3 or Python2 version(whichever you prefer to use). For Linux, the installer is basically a bash script that you need to run from command line. So let’s start the download and let it finish. Then open up your download folder, and rename the file as anaconda.sh — this makes it easier to run it on command line (or if you’re a command line ninja like my friend Asiful “Lotu” Nobel, keep the name as it is). Now open terminal for the same folder and make the script executable and run it using the following commands.

sudo chmod +x anaconda.sh

./anaconda.sh

Now you know the drill, scroll down, agree to EULA, install and be careful at the last line. The installer asks you to prepend the path to anaconda installation to your path variables. Don’t do that when on Linux(it may break core system functionalities). Tell the installer you don’t want to. You’re done installing. Now time to initiate.

First — add anaconda to path in a safer way. Instead of prepending, append it to path. This will make sure when system wants to run it’s python dependent processes, it gets the system python. Open your .bashrc using your preferred text editor. Nano, Vi, VS Code whatever it is(I prefer VS Code). After opening add the following line at the end of the file. Before this, copy the path where anacondas binaries have been installed. Usually it gets installed on your user’s home folder. So path should be something like /home/anaconda/bin

export PATH=$PATH:/path_to_anaconda_bin_folder

Save the file. Then do the following.

source .bashrc

This make sure your bash shell is updated with the changes in .bashrc you just made. However, since system will always be called first, how do we call anaconda python? Let’s create an alias or a different name for that. Let’s call anaconda python conpy. So again open up .bashrc and add this line at the end.

alias conpy=/path_to_anaconda_bin_folder/python

Save the file again and update your shell using source. And we’re done with initial part. Now my suggestion would be, create a conda env (anaconda) or virtualenv (for system python).

conda create -n deep_learning python=3.6 # or, for system python

virtualenv deep_learning

Now activate one of these.

# for anaconda

source activate deep_learning # for system python

source deep_learning/bin/activate

Now we’re good to go for the next “Tricky” Part — installing CUDA libraries.

CUDA and cuDNN installation

The latest version of CUDA Toolkit and cuDNN at the time of this writing are 9 and 7 respectively. However, Tensorflow and some other frameworks support CUDA 8 and cuDNN 6. They’re also backwards compatible to some extent. The best way to install these tools is to download them fron Nvidia’s website :

https://developer.nvidia.com/cuda-downloads . Head over there, sign up and then download CUDA Toolkit 8.0 and cuDNN 6 for CUDA 8. Don’t get hyped and download CUDA 9.

After downloading, extract and get inside the extracted folder. There, you’ll find a folder named cuda. Copy it to /opt/cuda directory. Do the same for cuDNN. Copy it to /opt/cudnn.

There’s an alternate way as well. Your distro’s repositories may contain the installers. In this case you just need to pick the correct version. I’m using Manjaro. And CUDA 8 Toolkit is available in AUR. (However the cuDNN version in AUR is kinda messy since it asks you to install CUDA 9 too and fails since you have CUDA 8 installed, weird!). So I had to download cuDNN manually.

After copying is done, it’s time to install a framework and test! So let’s get into the env we created earlier.

Testing green …..

I’ll be using Tensorflow for this one. If one of them works, all should work too! Install the package:

pip install tensorflow-gpu

Let it install. Then try the following command.

python -c "import tensorflow"

Now this may or may not throw and error saying tensorflow was unable to find the CUDA library. In most of the cases it is libcudnn.so.6 file related problem. We need to create a symlink to fix this problem.

ln -s /opt/cuda/lib64/libcudnn.so.6 /path_to_anaconda/envs/deep_learning/lib/libcudnn.so.6 # same for system python venv as well, just get to site-packages folder of that env

Now try importing tensorflow again. It’ll work this time.

How about a small program?

Run it and you should see the following output.