Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gachiemchiep/6461895ab494af1e584d67d71e086dbb to your computer and use it in GitHub Desktop.
Save gachiemchiep/6461895ab494af1e584d67d71e086dbb to your computer and use it in GitHub Desktop.
Installing OpenCV 3.4.3 on Ubuntu 16.04 with Cuda 9.2 support

This is a guide for installing OpenCV 3.4.1 on Ubuntu 16.04 with Cuda 9 support. This has been tested using a system with a Quadro K620 .

Nvidia Drivers

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 9.2 driver version 396.44 was used.

sudo apt-get install nvidia-396

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 9.2 and CuDNN 7.2 from Nvidia's developer site: https://developer.nvidia.com/cuda-92-download-archive Download cuda 9.2 and run the following:

`sudo dpkg -i cuda-repo-ubuntu1604_9.2.148-1_amd64.deb`
`sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub`
`sudo apt-get update`
`sudo apt-get 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-9.2-linux-x64-v7.3.1.20.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-9.2/bin

Anaconda setup

Considering you are familiar with anaconda. So you use anaconda to create an environment call "opencv" which used python2.7

$ which python
~/miniconda2/envs/tanarobo/bin/python

OpenCV Setup

sudo apt-get install cmake cmake-qt-gui

Get OpenCV 3.4.1 from Official :

mkdir ~/Tools/
git clone https://github.com/opencv/opencv
cd opencv
git checkout -b v3.4.1 3.4.1

Get extra module from extra

mkdir ~/Tools/
git clone https://github.com/opencv/opencv_contrib
cd opencv_contrib
git checkout -b v3.4.1 3.4.1

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 ~/Tools/opencv
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D WITH_CUDA=ON \
    -D CUDA_GENERATION=Auto \
    -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 \
    -D BUILD_TIFF=ON \
    -D ENABLE_CXX11=ON \
    -D WITH_PROTOBUF=OFF \
    -D BUILD_opencv_legacy=OFF \
    -D ENABLE_PRECOMPILED_HEADERS=OFF \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
    -D PYTHON_EXECUTABLE=/opt/miniconda2/envs/opencv/bin/python \
    -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..

You should see the following after cmake finishes:

--     Eigen:                       YES (ver 3.2.92)
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
-- 
--   NVIDIA CUDA:                   YES (ver 9.2, CUFFT CUBLAS)
--     NVIDIA GPU arch:             30 35 37 50 52 60 61 70
--     NVIDIA PTX archs:
-- 

Finally build OpenCV!

# Build!
make -j $(($(nproc) + 1))
sudo make install

Link to the anaconda environment. See https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/ for more detail

cd ~/minoconda2/envs/opencv/lib/python2.7/site-packages/
ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so

Check it

import cv2
cv2.__version__
print cv2.getBuildInformation()

The output should contain something like this

  OpenCV modules:
    To be built:                 aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev cvv datasets dnn dnn_objdetect dpm face features2d flann freetype fuzzy hdf hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python2 python_bindings_generator reg rgbd saliency sfm shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto


  NVIDIA CUDA:                   YES (ver 9.2, CUFFT CUBLAS FAST_MATH)
    NVIDIA GPU arch:             30 35 37 50 52 60 61 70
    NVIDIA PTX archs:

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