Tensorflow on Ubuntu 16.04, Cuda 8.0 + Cudnn + OpenCV

These are the steps required, to install tensorflow r0.11 with GPU support on a fresh install of ubuntu 16.04. Some credits go to Justin, for his wonderful tutorial.

Compiling the current opencv/master at this point resulted in cuda errors when running tensorflow. The solution was to use the latest opencv 2.4.13 release instead.

Prerequisites

1
sudo apt-get install git screen vim python-numpy swig python-dev python-wheel python-pip unzip

Prepare for nvidia driver install

Disable nouveau, create

1
sudo vim /etc/modprobe.d/blacklist-nouveau.conf

with content

1
2
blacklist nouveau
options nouveau modeset=0

and then

1
2
sudo update-initramfs -u
sudo reboot

Cuda toolkit 8.0

Download cuda toolkit 8.0 for 16.04

1
wget https://developer.nvidia.com/compute/cuda/8.0/prod/local_installers/cuda_8.0.44_linux-run

Before installing, stop the GUI to allow nvidia driver installation

1
sudo service lightdm stop
1
sudo sh cuda_8.0.44_linux.run

Cudnn 5.1.5

Download cudnn 5.1.5 and install

1
2
3
4
tar -xzvf cudnn-8.0-linux-x64-v5.1.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*

Environment variables

Update ~/.bashrc file

1
vim ~/.bashrc

Append at the bottom

1
2
3
4
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:$CUDA_HOME/lib"
export PATH="$CUDA_HOME/bin:$PATH"

Apply updated environment variables to current session

1
source ~/.bashrc

Tensorflow

Install bazel

1
2
3
4
5
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.io/bazel-release.pub.gpg | sudo apt-key add -
sudo apt-get update
sudo apt-get install bazel
sudo apt-get upgrade bazel

Clone branch r0.11

1
git clone -b r0.11 https://github.com/tensorflow/tensorflow

Get your compute capability from here

Configure

  • yes to gpu support
  • set your compute capability
  • cuda 8.0
  • cudnn 5.1.5
1
2
cd tensorflow/
./configure

Compile

1
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

Build pip wheel

1
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

Install tensorflow

1
sudo pip install -I --upgrade --force-reinstall /tmp/tensorflow_pkg/tensorflow-0.11*

Test tensorflow

1
python -m tensorflow.models.image.mnist.convolutional

OpenCV 2.4

Download latest 2.4.x release for linux.

1
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

Download and extract

1
2
wget https://github.com/Itseez/opencv/archive/2.4.13.zip
unzip opencv-2.4.13.zip

Compile

1
2
3
4
5
cd opencv-2.4.13
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
make

Install

1
sudo make install

Notes

When running tensorflow within screen, use /bin/bash to execute ~/.bashrc and correctly set all environment variables

1
screen -s -/bin/bash