Install OpenCV with CUDA for Conda

Everytime I compile OpenCV from source, I hate myself for not writing this up before. Not doing the same mistake again. There are great tutorials on installing OpenCV by PyImage Search (see References), however they work for system-level Python with virtualenv. These are my notes on building OpenCV 3 with CUDA on Ubuntu 16.04 with Anaconda environment in case those tutorials did not work, e.g. you cannot find the file.


This assumes a running Anaconda distribution as the default Python environment (check which python ). If you want to install with CUDA support, CUDA and CuDNN libraries should be installed and enabled (check nvcc --version ).

Install dependencies:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential cmake pkg-config
$ sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev libgtk-3-dev
$ sudo apt-get install libatlas-base-dev gfortran

Optional dependencencies (not necessary unless you know why you need them):

$ sudo apt-get install libopenblas-dev liblapack-dev libeigen3-dev libtheora-dev libvorbis-dev sphinx-common
$ sudo apt-get libtbb-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev
$ sudo apt-get libavutil-dev libavfilter-dev libavresample-dev ffmpeg

Download and unzip OpenCV and OpenCV's extra modules:

$ cd
$ mkdir opencv
$ cd opencv
$ wget -O
$ wget -O
$ unzip
$ unzip
$ rm -f *.zip

Create new Conda environment and install NumPy:

$ conda create -n cv python=3.6
$ source activate cv
(cv) $ conda install numpy

Locate environment-specific Python directories:

(cv) $ export python_exec=`which python`
(cv) $ export include_dir=`python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())"`
(cv) $ export library=`python -c "import distutils.sysconfig as sysconfig; print(sysconfig.get_config_var('LIBDIR'))"`
(cv) $ export default_exec=`which python3.6`

Configure and install

Configure build with CMake:

(cv) $ cd ~/opencv/opencv-3.4.5
(cv) $ mkdir build
(cv) $ cd build

At this point, it is important to double check the output of CMake. Make sure your output contains something along those lines:

--   Python 3:
--     Interpreter:                 /home/daniel/anaconda3/envs/cv/bin/python (ver 3.6)
--     Libraries:                   /home/daniel/anaconda3/envs/cv/lib/ (ver 3.6.8)
--     numpy:                       /home/daniel/anaconda3/envs/cv/lib/python3.6/site-packages/numpy/core/include (ver 1.15.4)
--     packages path:               /home/daniel/anaconda3/envs/cv/lib/python3.6/site-packages

Unless you see the appropriate paths to Interpreter and Libraries, do not proceed.

Otherwise, proceed as usual:

(cv) $ make -j6
(cv) $ sudo make install
(cv) $ sudo ldconfig

Finally, OpenCV should be located in /usr/local/lib/python3.6/site-packages and you can symlink the cv2 directory to your Conda environment(s):

(cv) $ ln -s /usr/local/lib/python3.6/site-packages/cv2 ~/anaconda3/envs/cv/lib/python3.6/site-packages/cv2

Test your installation.