How to Setup Ubuntu 16.04 with CUDA, GPU, and other requirements for Deep Learning

Kapil Varshney
7 min readJul 27, 2018

--

I am writing this guide so that I can refer back to it whenever I am setting up a Deep learning machine for Computer Vision. I realized the need of it after the 3rd time I was trying to setup my machine. The earlier two types of attempts (I setup the system multiple times in both types) were with reluctance to do away with Windows on my system. My earlier attempts:

  1. Setup a Virtual Machine on Windows and then install Ubuntu and required Python packages.
    This was a good way to get started with coding. But, there are limitations to a VM. I could not utilise 100% of my system hardware and GPU wasn’t accessible at all.
  2. Setup Linux and everything else mentioned in this blog on a USB drive and use it to directly boot my system. Again, there were limitations to it — the read/write speeds were slow, I couldn’t accommodate large datasets, and occasional “Bus Error”,to name a few.
  3. Now, Ubuntu is the only OS on my system, utilizing all of RAM (16 GB), SSD (256 GB), and GPU (Nvidia GeForce 940MX 2 GB). I am using a Lenovo Yoga 710 laptop.
  4. Addition: I have surpassed my laptop’s system specifications, in terms of the hardware requirements for my projects, esp. GPU. Now, I am using a friend’s system, remotely via SSH, which has a 11 GB Nividia 1080i GPU. I’ll write about it later.

The prerequisite to this guide is to have Ubuntu ready on your system. You can follow my earlier blogs to get that done.

  1. Create a bootable USB drive with Ubuntu 16 installer
  2. Installing Ubuntu 16 using a USB drive

This guide heavily follows Adrian Rosebrock’s guide on Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python. The only difference being that Adrian’s guide is focused on setting up a remote system (AWS or another system). I am just trying to present how I used Adrian’s guide to setup my personal laptop.

Adrian is one of the best experts on Computer Vision and Deep Learning, and I follow his blogs on pyimagesearch.com. You guys should check it out too (especially, if you want to gain expertise in Computer Vision), if you already haven’t.

Let’s breakdown the major steps here:

  1. Install required libraries
  2. Install Nvidia drivers for the GPU
  3. Install CUDA + CuDNN
  4. Install and setup a virtual environment with Python libraries (including Tensorflow and Keras)
  5. Install Open CV 3
  6. Install mxnet (optional)

Step 1: Install required libraries

You can download this file or copy paste to run in one go.

Step 2: Install Nvidia Drivers for the GPU

This is assuming you have an Nvidia GPU on your machine.

Go to Additional Drivers and select the NVIDIA binary driver. The latest driver is automatically detected by Ubuntu. The latest version in this case being 384.130 from nvidia-384. Click on “Apply Changes”. It will take a few minutes to install the driver. Restart your computer once the changes have been applied.

Step 3: Installing Cuda and CuDNN

Download the CUDA Toolkit 8.0 from the following download page: https://developer.nvidia.com/cuda-80-ga2-download-archive

Make selections as indicated in this screenshot, and download the Base Installer. (You can also download the Patch and install it the same way as the Base installer after CUDA is successfully installed.)

Run the CUDA installer file using the following commands.

chmod +x ./cuda_8.0.61_375.26_linux.run
sudo ./cuda_8.0.61_375.26_linux.run

Once the installer launches, you’ll have to hit enter many times to go through the agreement terms. Press and hold Space key for quick scroll.

At the end of the Agreement, you need to type in “accept” when it prompts. When prompted for “Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 375.26?”, enter “no”. We have already installed the required driver in Step 2. If you enter “yes”, the installation will throw an error:

Installing the NVIDIA display driver…
It appears that an X server is running. Please exit X before installation. If you’re sure that X is not running, but are getting this error, please delete any X lock files in /tmp.

Just keep following the prompts after that. For most of the prompts you can answer with a “y”. Just hit enter to select the default path when prompted for the toolkit location. Keep the same location for CUDA Samples. If you keep the default option or specify a different path, just be careful in the later part of this guide where we run a sample file to test the CUDA installation.

Note: The default install location of CUDA Toolkit is: /usr/local/cuda-8.0

You should see a similar log.

Now, the next steps would be to add a couple of “paths” as mentioned in the installer log in the previous screenshot.

You can use the editor of your choice. I usually use nano.

nano ~/.bashrc (To launch the GNU nano editor)

And, add the following lines:

export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64

While in nano -
Ctrl+O : save
Ctrl+X : exit

Once the bashrc file has been updated, let’s source it to bring the changes in effect.

source ~/.bashrc

You don’t have to do this every time. Ubuntu automatically sources .bashrc every time you launch a new terminal.

Once we update the bashrc, we can run a sample file to check whether the installation was successful.

cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery

If you don’t see Results = PASS, there’s probably something that you have missed from the previous steps. Fix it before moving ahead.

Install CuDNN

For this part you need to first create a free account with Nvidia (if you already don’t have one) and log in. Then, download cuDNN 6.0 for CUDA 8. You’ll have to access the archives and download the highlighted file from the website.

Once the file has downloaded in your Downloads folder, we can unzip the file and copy all the files in lib64 and include to the folders by the same names in usr/local/cuda, respectively. And, you are all set. You can use the following command to do so:

Step 4: Setup virtual environment, Python libraries, Tensorflow and Keras

Let’s go ahead and create a “deep learning” virtual environment where we will install all the required Python libraries, Tensorflow and Keras. The very first step will be to install the Python package manager “pip” that will make it convenient to install all the required Python libraries including the virtual environment.

To understand why you need a virtual environment, give this article a read.

Next, add the following lines in .bashrc file in the home directory, like how we did while installing CUDA.

nano ~/.bashrc (To launch the GNU nano editor)

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

Now, let’s make a virtual environment and work on it. I’ll call it dl (short for deep learning, if it isn’t that obvious). Here are a few commands that can come handy while working with virtual env:

Notice the “(dl)” before the prompt. This indicates that the virtual environment dl is active now.

Let’s proceed with installing the libraries that we’ll require for our deep learning and computer vision projects. These are in no way all the libraries that will suffice your work, but only the most popular and important ones. You might have to install more packages from time to time according to the requirements of the project you are working on.

Ensure that you execute the following commands in the virtual environment. Also, you don’t need to use “sudo” while installing packages in a virtual environment.

To check whether your installations are working, just import them in python.

>>>import tensorflow

There is a slight possibility that you might get the following error (with tensorflow):

Do the following checks :
1. Make sure you have CUDA 8.0
2. Make sure /usr/local/cuda is symbolically linked to /usr/local/cuda-8.0
3. libcublas.so.8.0 should exist in /usr/local/cude/lib64
4. Check if the PATH and LD_LIBRARY_PATH has been correctly initialised in the .bashrc file and the file has been sourced.

Step 5 : Install OpenCV 3

I’ll be using OpenCV 3.3 to install on my system. You can use the following commands to install OpenCV 3.

Check the installation by importing OpenCV in python. Note: OpenCV 3.3 is called by the name “cv2”.

Step 7: Install MXNet

Just follow these commands to install MXNet on your system:

Congratulations, your Deep Learning machine is all ready to do those Image classification projects you have been wanting to try. I hope this helps you get started with Deep Learning projects on your own system. Please let me know if you have any questions/concerns/suggestions.

--

--

Kapil Varshney

Data Scientist (Computer Vision) @ Esri R&D New Delhi. Here to share what I learn and do. Connect with me at https://www.linkedin.com/in/kapilvarshney14/