Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gachiemchiep/5b75cad5c717cc2bdbae8d516d72edf6 to your computer and use it in GitHub Desktop.
Save gachiemchiep/5b75cad5c717cc2bdbae8d516d72edf6 to your computer and use it in GitHub Desktop.
Installing OpenCV 3.1 on Ubuntu 16.04 with Cuda 8 support

This is a guide for installing OpenCV 3.1 on Ubuntu 16.04 with Cuda 8 support. This has been tested using a system with a GeForce GTX 1060 and on one with a GeForce GTX 1080.

Nvidia Drivers with Compiz

Install Nvidia drivers

# Start clean
sudo apt purge nvidia-*
# Add the PPA
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update

For Cuda support we need the compatible driver version (generally NOT the newest). For Cuda 8 driver version 367.57 was used.

sudo apt-get install nvidia-367

UPDATE: now it seems that nvidia-367 actually installs nvidia-375 due to security reasons. See https://devtalk.nvidia.com/default/topic/1000667/cuda-setup-and-installation/failed-to-initialize-nvml-driver-library-version-mismatch/

Fixing Unity if it doesn't boot (from http://askubuntu.com/questions/760356/ubuntu-16-04-unity-no-desktop-just-background-wallpaper):

sudo rm -fr ~/.cache/compizconfig-1
sudo rm -fr ~/.compiz

Then try this if your session not loading :

sudo rm -fr ~/.Xauthority
sudo rm -fr ~/.config/autostart

Reinstall compiz

sudo apt-get install --reinstall ubuntu-desktop unity compizconfig-settings-manager upstart

In addition on some systems the secure boot BIOS settings have to be modified. On the Asus x99-a II motherboard the Secure Boot option is set to "Windows Only" by default. Setting it to "Other OS" allows Ubuntu to use Nvidia drivers (see http://askubuntu.com/a/761281)

Cuda Setup

Install CUDA 8 and CuDNN 5.1 from Nvidia's developer site: https://developer.nvidia.com/cuda-downloads Download cuda 8.0 and run the following:

cd ~/Downloads
sudo dpkg -i cuda-repo-ubuntu1604-8-0-local_8.0.44-1_amd64.deb
sudo apt update
sudo apt install cuda

NOTE: techincally the following step is not required for OpenCV since this is the Cuda Deep Learning library, but it is included here for conveneince (it is required for libraries such as Caffe).
Get the "cuDNN v5.1 Library for Linux" from https://developer.nvidia.com/rdp/cudnn-download (need to register) and then run the following:

sudo tar -xvf cudnn-8.0-linux-x64-v5.1.tgz -C /usr/local

Then add the following to your ~/.bashrc

vim ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/cuda/lib64
export PATH=$PATH:/usr/local/cuda-8.0/bin

OpenCV Setup

sudo apt-get install cmake cmake-qt-gui

Get OpenCV 3.1 from this fork for CUDA 8 support:

mkdir ~/code/opencv-3.1.0
cd ~/code/opencv-3.1.0
git clone git@github.com:daveselinger/opencv.git .
git checkout 3.1.0-with-cuda8

See the following for more details: opencv/opencv#6677

Adapted from this guide: https://github.com/BVLC/caffe/wiki/Ubuntu-16.04-or-15.10-Installation-Guide Package pre-reqs

sudo apt install --assume-yes build-essential cmake git pkg-config unzip ffmpeg qtbase5-dev python-dev python3-dev python-numpy python3-numpy
sudo apt install libhdf5-dev
sudo apt install --assume-yes libgtk-3-dev libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev
sudo apt install --assume-yes libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt install --assume-yes libv4l-dev libtbb-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev
sudo apt install --assume-yes libvorbis-dev libxvidcore-dev v4l-utils

Configure CMake

cd ~/code/opencv-3.1.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D WITH_CUBLAS=ON -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..

You should see the following after cmake finishes:

--   Other third-party libraries:
...
--     Use Cuda:                    YES (ver 8.0)
...
--   NVIDIA CUDA
--     Use CUFFT:                   YES
--     Use CUBLAS:                  YES
--     USE NVCUVID:                 NO
--     NVIDIA GPU arch:             20 21 30 35
--     NVIDIA PTX archs:            30
--     Use fast math:               NO

Finally build OpenCV!

# Build!
make -j $(($(nproc) + 1))
sudo make install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment