Ubuntu + Deep Learning Software Installation Guide | by Nick Condo | Towards Data Science

I recently built a deep learning machine after enrolling in Udacity’s Self-Driving Car Engineer Nanodegree. You can read about my build here: Build a Deep Learning Rig for $800. In this post, I will describe the necessary steps to go from a clean build to training deep neural networks. There are a number of good installation guides out there — particularly this one from floydhub that much of this is based on — but I found myself having to dig through many different resources to get everything installed properly. The goal of this article is to consolidate all the necessary resources into one place.
Even before installing your OS, you should perform some basic checks to ensure your system is up and running properly. For this, I recommend following along with First 5 Things to Do with a New PC Build from Paul’s Hardware YouTube channel. He goes on to install Windows, but the first half of the video applies to any machine regardless of OS.
First, download Ubuntu 16.04.2 LTS, the latest long-term support version of Ubuntu. Then, create a bootable USB stick with the Ubuntu ISO. You can follow the official Ubuntu instructions here if you are on macOS, or here if you are on Windows. Once you have the Ubuntu ISO loaded onto your USB stick, insert it into your new build and power on the machine. To install Ubuntu, you will need to access the boot menu, which is the F11 key for me, and choose to boot from the USB drive. From there, you can simply follow the installation instructions on the screen. You can also refer to Ubuntu’s community wiki Quick Guide: Installing Ubuntu from a USB memory stick for more details.
From the terminal, run the following commands to update packages and install some essential software:
sudo apt-get updatesudo apt-get upgradesudo apt-get install build-essential cmake g++ gfortran git vim pkg-config python-dev software-properties-common wgetsudo apt-get autoremovesudo rm -rf /var/lib/apt/lists/*
You will need to download the correct driver based on your GPU model, which you can check with the following command:
lspci | grep -i nvidia
Check the Proprietary GPU Drivers PPA repository to find the current release for your GPU. As of the time of this writing the latest release for the GeForce 10 Series is 381.22, however I opted to go with the 375.66 — the current long-lived branch release. Install the driver with the following commands:
sudo add-apt-repository ppa:graphics-drivers/ppasudo apt-get updatesudo apt-get install nvidia-375
Then restart your computer:
sudo shutdown -r now
To double check that the driver has installed correctly:
cat /proc/driver/nvidia/version
Download the CUDA 8.0 toolkit from Nvidia’s site. Go to the Downloads directory (or where ever you chose to save it) and install CUDA:
sudo dpkg -i cuda-repo-ubuntu1604*amd64.debsudo apt-get updatesudo apt-get install cuda
Add CUDA to the environment variables:
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrcecho 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrcsource ~/.bashrc
Check that the correct version of CUDA is installed:
nvcc -V
Restart your computer:
sudo shutdown -r now
Download the cuDNN library from Nvidia’s site. You will need to first sign up for Nvidia’s developer program before you can download cuDNN. They state that this could take up to a couple of days, but I received access almost instantly. At the time of this writing, cuDNN v5.1 is the version officially supported by TensorFlow, so hold off on v6.0 unless you know it is supported (they are currently working on it). After downloading, go to your Downloads directory to extract and copy the files:
tar xvf cudnn*.tgzcd cudasudo cp -P */*.h /usr/local/cuda/include/sudo cp -P */libcudnn* /usr/local/cuda/lib64/sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
Download the Anaconda installer from the Continuum site. I chose the Python 2.7 version, but you can choose whichever version you want to be your default Python:
bash Anaconda2-4.4.0-Linux-x86_64.sh
Answer ‘yes’ when the installer asks if you’d like to add Anaconda to your path (unless you don’t want it as your default Python). Once you have Anaconda installed, you can create a new conda environment with all of the necessary deep learning libraries. I will use Udacity’s starter kit as an example, since that’s what I’ve used for many of my projects. Download (or copy and paste) the environment-gpu.yml file from the CarND-Term1-Starter-Kit. You can change the name of the environment to whatever you like at the top of the file. From the directory which you saved the environment-gpu.yml file:
conda env create -f environment-gpu.yml
And to activate your new environment:
source activate carnd-term1
Now you can test to make sure everything is working properly by running TensorFlow’s builtin MNIST example model (no longer works for TensorFlow version ≥ 1.0). Note that the following command will automatically download the 12 MB MNIST dataset on the first run:
python -m tensorflow.models.image.mnist.convolutional
If everything is working for you at this point, congratulations — you are now ready to classify hotdogs!