Deep learning setup for Ubuntu 16.04: Tensorflow 1.2, keras, opencv3, python3, cuda8 and cudnn5.1

Important: All the steps were last tested on 6/21/2017. Details on my system build can be found here.

I recently installed ROS on my host machine. ROS changed opencv path and messed something with nvidia drivers, so had to reinstall all software. Luckily was able to recover most of the data.

Some things i wish I knew before,

  • opencv is not compatible with cuda8. There is a work around, that I used here.
  • secure boot needs to be disabled from bios before taking any of these steps. Not doing so may result in the dreaded infinite login loop. Learned this the hardway.
  • Avoid installing from binaries. Cuda and nvidia drivers can be installed from binary files ( as .run ) too. This required disabling lightdm and starting it after installing nvidia drivers. After this, I kept getting issues where monitor was not recognized on reboot at random.
  • Do compiled opencv, so it can be used in virtual env (I forgot this many times). Although in this method, the cv2 module is installed in both python2 and python3 dist-packages, so making symlinks etc is not needed
  • (this is just for me) NEVER install ROS on native host. For some reason ROS packages interfere with everything, from nvidia driver to resetting path for opencv. This I suspect is the root cause for the issue I had. So am never going to install ROS on my deep learning computer directly. Sticking to VM, or a dedicated computer is the best.

Step 0: Install google chrome

Install Google chrome. I prefer chrome over mozilla firefox, so I installed that first from here.

Step 1: Prepare computer

Update OS, install cmake, fortran, g++ etc.

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential cmake g++ gfortran
sudo apt-get install git pkg-config python-dev
sudo apt-get install software-properties-common wget
sudo apt-get autoremove
sudo rm -rf /var/lib/apt/lists/*

Step 3: Install NVIDIA drivers

Next step is to install correct nvidia driver. I have a titan X gpu, so I will install nvidia-375. If you are not sure of your GPU, you can use

lspci | grep -i nvidia 

This in terminal gives,

Next step is to add proprietary repository of nvidia drivers.

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-375

Once nvidia driver is installed, restart the computer. You can verify the driver using the following command.

cat /proc/driver/nvidia/version

The command above should produce output like this,

Step 4: Cuda-8 installation

Next step is to install cuda-8. On the link, you can select your system and download the appropriate package file. I tried using the run file, but had issues while booting, so reinstalled ubuntu 16.04 and installed cuda from deb package.

The package file is about 2GB, so it may take a while to download. On my computer it took about 10 minutes. Installation steps are the same as that on their website, but writing it here again so one may copy and paste. Note, the installation takes about a minute or so.

Next step is to add libraries to .bashrc file,

echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

After sourcing the bashrc file, the CUDA version can be verified using

nvcc -V

Step 5: cuDNN 5.1

Next step is to install cuDNN. cuDNN can be downloaded from NVIDIA’s webpage Extract cuDNN and install.

cd ~/Downloads/
tar xvf cudnn*.tgz
cd cuda
sudo cp */*.h /usr/local/cuda/include/
sudo cp */libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

Step 6: Install python stuff

Next step is to install standard python libraries,

sudo apt-get update && apt-get install -y python-numpy python-scipy python-nose                                           python-h5py python-skimage python-matplotlib                                   python-pandas python-sklearn python-sympy
sudo apt-get clean && sudo apt-get autoremove
sudo rm -rf /var/lib/apt/lists/*

Next install more python goodies like virtual environment, pip, pip3, jupyter notebook, etc.

sudo apt-get update
sudo apt-get install git python-dev python3-dev python-numpy python3-numpy build-essential python-pip python3-pip python-virtualenv swig python-wheel libcurl3-dev
sudo apt-get install -y libfreetype6-dev libpng12-dev
pip3 install -U matplotlib ipython[all] jupyter pandas scikit-image

Step 7: Install openBLAS from its repository

OpenBLAS is linear algebra library that is faster than standard Atlas.

mkdir ~/git
cd ~/git
git clone
cd OpenBLAS
make FC=gfortran -j16
sudo make PREFIX=/usr/local install

Step 8: Install tensorflow

Next install tensorflow, easiest method is to use pip3

pip3 install --upgrade tensorflow-gpu

If everything goes well, tensorflow should be available in the new environment. This can be verified using python in the following steps,

>>> import tensorflow as tf
>>> a = tf.constant(5)
>>> b = tf.constant(6)
>>> sess = tf.Session()
// this should print bunch of messages showing device status etc. // If everything goes well, you should see gpu listed in device
>> sess.close()

Screen shot below shows these commands running in the terminal.

Step 9: Install keras

Install keras using pip3

sudo pip3 install keras

Verify keras installation using following commands, screen shot below.

>>> import keras

Once tensorflow and keras are installed, the last step is to install opencv. To do so, deactivate tensorflow3 environment using deactivate. I installed openCV host directly.

Step 10: Install opencv

Install packages to read different image formats.

sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

Install qt5 for

sudo apt install qtbase5-dev

Install libraries to video format

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

Install gtk for using GUI

sudo apt-get install libgtk2.0-dev

Install libatlas for opencv functions

sudo apt-get install libatlas-base-dev gfortran

Install hdf5 library

sudo apt-get install libhdf5-serial-dev

Clone openCV from repository where opencv is installed with cuda8.

git clone 
git checkout 3.1.0-with-cuda8

Instal opencv with python 3.

cd ~/opencv
mkdir build
cd build

Compile and install opencv

make -j16
sudo make install

Step 10: Virtual environments

Last step is to use virtual environments to organize projects. For now, I will use tensorflow3. Create a virtual environment named tensorflow3. A different more appropriate name can be chosen.

virtualenv --system-site-packages -p python3 tensorflow3

Next, source the new environment and install tensorflow

source ~/tensorflow3/bin/activate

Once the virtual environment is sourced, the command prompt must change as (tensorflow3). You can verify tensorflow as follows.

Code/text editor

Next step is to add your favorite code/text editor. I prefer sublime text for python scripts due to vibrant colors, and use atom for markdown documents before posting on web. I use atom mostly because I found it represents compiled documents most similar to how they appear on net. An example is the page here from Advanced Control Systems course I developed while at SUNY-Stony Brook.

Sublime text: Installation

sudo add-apt-repository ppa:webupd8team/sublime-text-3
sudo apt-get update
sudo apt-get install sublime-text-installer

Sublime text: Uninstallation

Uninstallation step, in case something better comes along.

sudo apt-get remove sublime-text-installer


Below are links I used to compile these codes.


Next steps

The next steps are to install a good text editor (sublimetext or atom), remote desktop software and run linux box headless (better if you can get display emulator).

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.