Skip to content

Instantly share code, notes, and snippets.

@filitchp
Last active December 12, 2019 21:36
Show Gist options
  • Save filitchp/5645d5eebfefe374218fa2cbf89189aa to your computer and use it in GitHub Desktop.
Save filitchp/5645d5eebfefe374218fa2cbf89189aa 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
@ntomita
Copy link

ntomita commented Jan 24, 2018

Never mind, actually I didn't checkout a branch indicated in the instruction. Everything went well. Thanks!

@neelindresh
Copy link

neelindresh commented Feb 7, 2018

modules/core/CMakeFiles/pch_Generate_opencv_core.dir/build.make:62: recipe for target 'modules/core/precomp.hpp.gch/opencv_core_RELEASE.gch' failed
modules/viz/CMakeFiles/pch_Generate_opencv_viz.dir/build.make:62: recipe for target 'modules/viz/precomp.hpp.gch/opencv_viz_RELEASE.gch' failed
make[2]: *** [modules/viz/precomp.hpp.gch/opencv_viz_RELEASE.gch] Error 1
CMakeFiles/Makefile2:3432: recipe for target 'modules/viz/CMakeFiles/pch_Generate_opencv_viz.dir/all' failed
make[1]: *** [modules/viz/CMakeFiles/pch_Generate_opencv_viz.dir/all] Error 2
make[2]: *** [modules/core/precomp.hpp.gch/opencv_core_RELEASE.gch] Error 1
make[1]: *** Waiting for unfinished jobs....
CMakeFiles/Makefile2:1896: recipe for target 'modules/core/CMakeFiles/pch_Generate_opencv_core.dir/all' failed
make[1]: *** [modules/core/CMakeFiles/pch_Generate_opencv_core.dir/all] Error 2
[ 19%] Generating precomp.hpp.gch/opencv_cudaimgproc_RELEASE.gch
[ 19%] Generating precomp.hpp.gch/opencv_cudabgsegm_RELEASE.gch
[ 19%] Generating precomp.hpp.gch/opencv_cudafilters_RELEASE.gch
[ 19%] Built target pch_Generate_opencv_cudabgsegm
[ 19%] Built target pch_Generate_opencv_cudafilters
[ 19%] Built target pch_Generate_opencv_cudaimgproc
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2

showing this error. can anybody help?

@guruvishnuvardan
Copy link

guruvishnuvardan commented Feb 17, 2018

Hi,
I have installed the Opencv 3.1 with the following URL:https://www.pyimagesearch.com/2016/07/11/compiling-opencv-with-cuda-support/, post installation, there is no cv2.so file created in /usr/local/lib/python2.7/site-packages or build/lib. Please find the screen shot
cv2 so

Can anyone pls help me fix this, as its crucial for my projects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment