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 [email protected]: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
@MarcosBernal
Copy link

Thank you for the tutorial, I followed it using an old laptop with ubuntu 16.04 and a GeForce GT 555M.

I add a couple of comments, in case someone needs a configuration similar to mine, that is basically using SURF, SVM and ANN in opencv.

I cloned the repository of opencv_contrib in the same parent where opencv-3.1.0 is located and I moved to the 3.1.0 version of the opencv_contrib repository git checkout 3.1.0.

opencv/
- opencv/opencv-3.1.0/
- opencv/opencv_contrib/

Then, I added several params to the cmake step:
Such as:

  • -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules for adding the modules
  • -D BUILD_opencv_java=OFF because I am not using java
  • -D CUDA_GENERATION=Auto for compiling focusing on my gpu arch, it makes the compilation way faster
  • -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_NVCUVID=1 -D WITH_CUFFT=ON -D WITH_EIGEN=ON -D WITH_IPP=ON
    because I need to use these libraries (maybe not all, but just in case)

I built opencv and checked the installation using a gpu sample of opencv.

cd OPENCV_PATH/samples/gpu/
g++ -o surf_keypoint_matcher surf_keypoint_matcher.cpp `pkg-config opencv --cflags --libs`
./surf_keypoint_matcher --left IMAGE1 --right IMAGE2

@dheera
Copy link

dheera commented Apr 21, 2017

Additions:

  • cmake failed on the first try. After some googling I needed to install libqt4-dev. Maybe add the following to the tutorial for more bulletproof "nailed-it-on-the-first-try" installation
    suda apt-get -y install python-numpy python3-numpy
    sudo apt-get -y install build-essential cmake git pkg-config
    sudo apt-get -y install libjpeg8-dev libtiff-dev libjasper-dev libpng12-dev
    sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
    sudo apt-get -y install libgtk2.0-dev
    sudo apt-get -y install libatlas-base-dev gfortran
    sudo apt-get -y install python3.5-dev
    sudo apt-get -y install python2.7-dev
    sudo apt-get -y install libhdf5-dev
    sudo apt-get -y install libqt4-dev
  • Change [email protected]: to http://github.com/ for those not using SSH keys.

@Ugenteraan
Copy link

Ugenteraan commented Jun 26, 2017

For those having this particular error :
"CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
HDF5_C_INCLUDE_DIR (ADVANCED)
used as include directory in directory /home/.../code/opencv-3.1.0/opencv/modules/python/python2"

You can fix it by installing these:
sudo apt-get -y install libhdf5-dev
sudo apt-get -y install libqt4-dev

Thanks to dheera.

@gandarain
Copy link

Can i ask u a question ???

Error when try this 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" ..

This is the error message
CMake Error: The source directory "/home/panjaitan/code/opencv-3.1.0" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.

@gandarain
Copy link

anyone can help?

@gandarain
Copy link

Can i ask u a question ???

Error when try this 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" ..

This is the error message
CMake Error: The source directory "/home/panjaitan/code/opencv-3.1.0" does not appear to contain CMakeLists.txt.
Specify --help for usage, or press the help button on the CMake GUI.

@gnc10
Copy link

gnc10 commented Oct 25, 2017

If you are still unable to solve this, you have to run CMAKE command inside build folder in /home/panjaitan/code/opencv-3.1.0.
To do that first navigate to /home/panjaitan/code/opencv-3.1.0 using this command: cd /home/panjaitan/code/opencv-3.1.0
Then create the build folder like so: mkdir build
Lastly go into build directory by: cd build
Then run the CMAKE command

@aktivkohle
Copy link

Thanks for that, initial attempts to get this installed failed due to an incompatibility with Cuda 8.0.
The instructions above ran to completion, but it also wanted gstreamer which can be installed with
sudo apt-get install libgstreamer-plugins-base1.0-dev debhelper gstreamer1.0-doc
as per baedert/corebird#189

Then, there was the issue that it didn't really install cv2 into my python 3.6 library (in its pyenv-virtualenv). It now works!! Here is a tweak on @filitchp ' s CMAKE command (run with the pyenv-virtualenv selected in the build directory) I used to get it to work:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") \
-D PYTHON_EXECUTABLE=/home/aktivkohle/.pyenv/shims/python3 \
-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"  \
..

@gandarain
Copy link

Anyone know how to run example code opencv with cuda ?

@gandarain
Copy link

i have dual os, windows and ubuntu, is that okay if i run this in my ubuntu os ?

@ntomita
Copy link

ntomita commented Jan 24, 2018

Got an error while building it. Here's what it says:

[ 68%] Building CXX object modules/cudawarping/CMakeFiles/opencv_cudawarping.dir/src/resize.cpp.o
[ 68%] Linking CXX shared library ../../lib/libopencv_cudawarping.so
[ 68%] Built target opencv_cudawarping
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

I followed all the steps except installing drivers (cuz it's already in) and cuda8.0 is available. My machine is Ubuntu 16.04 with GTX TitanXp. Any thoughts?

@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