Compile OpenCV for Anaconda Python

OpenCV supports Python well. This procedure was tested with Ubuntu Linux on laptop and Raspberry Pi, and assumes preferred Python exe is aliased to (it runs when you type) python. Check which python to be sure it’s NOT pointing to /usr/bin/python or this install will not work. Optionally install free Intel IPP, TBB and/or MKL as well.

Linux prereqs include:

apt install cmake gcc g++ git libjpeg-dev libpng-dev libtiff5-dev libavcodec-dev libavformat-dev libswscale-dev pkg-config libgtk2.0-dev libopenblas-dev libatlas-base-dev liblapack-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavcodec-dev libavutil-dev libavfilter-dev libavformat-dev libavresample-dev ffmpeg

MacOS prereqs include:

brew install git cmake pkg-config jpeg libpng libtiff openexr eigen tbb

Download the latest OpenCV Source code zip file, then:


cmake --build release

cmake --install release

If you have trouble with Cmake, consider cmake-gui or using the simplest Cmake script at the bottom of this page. Especially for embedded systems like the Raspberry Pi, consider make -j -l 2 to avoid over-temperature and under-voltage warnings (in general when compiling on Raspberry Pi, not just for OpenCV).

The build results in something like this Gist:

Test opencv-python with:

from time import time
from numpy import uint8
from numpy.random import rand
import cv2

Nf = 500

def fpsopencv(dat):
    tic = time()
    for i in dat:
        cv2.waitKey(1) #integer milliseconds, 0 makes wait forever
    return Nf / (time()-tic)

imgs = (rand(Nf,xy[0],xy[1])*255).astype(uint8)
fps = fpsopencv(imgs)


This should play a random noise movie.

If experiencing errors like:

“ModuleNotFoundError: No module named ‘cv2’”

Ensure that desired Python site-packages found with

python -c "import sys; print(next(p for p in sys.path if 'site-packages' in p))"


  • Windows: cv2.*.pyd
  • Linux: cv2.*.so

ImportError: ~/miniconda3/lib/ version `GLIBCXX_3' not found (required by ~/miniconda3/lib/

Use a more up to date system libstdc++ by

cd ~/miniconda3/lib
ln -s /usr/lib/x86_64-linux-gnu/

If you get error

/usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: No such file or directory #include_next <stdlib.h> 


~/miniconda3/lib/ undefined symbol: _ZTINSt6thread6_StateE

then try using g++ 5

  • Disable use of Nvidia CUDA when compiling OpenCV via -DWITH_CUDA=OFF. Because Cuda takes so much longer to compile, even if you have the GPU, maybe first try without CUDA, to see if OpenCV3 is going to work for you, then recompile with CUDA.
  • To avoid the undefined reference to TIFFOpen@LIBTIFF_4.0 type errors, add the -DBUILD_TIFF=ON option

If you get the error like

lib/ version GLIBC_2.15 not found (required by /usr/lib/x86_64-linux-gnu/

then try as a fix to GLIBC_2.15 not found

cd ~/anaconda3/lib

If you get error

SIGILL illegal instruction (Core dumped)

when typing

import cv2

try to get more information by typing in Terminal

sudo sysctl -w kernel.core_pattern=core
ulimit -c unlimited

and rerun the command that gave the core dumped error. Now you’ll have a file core in that directory. Type in Terminal

gdb python core

to get hints about the failure.

Minimal Install

This has just the stuff needed for a basic OpenCV install for Python 3.



libjasper-dev was removed from Ubuntu.

Download free Intel oneMKL, extract and type


sudo is not needed; MKL will install to ~/intel


If trouble installing OpenCV into Python, consider adding to your cmake shell command:

-DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)")
-DPYTHON_EXECUTABLE=$(python -c "import sys; print(sys.executable)")
-DPYTHON_INCLUDE_DIR=$(python -c "import sysconfig; print(sysconfig.get_config_var('INCLUDEPY'))")
-DPYTHON_PACKAGES_PATH=$(python -c "import sysconfig; print(sysconfig.get_config_var('LIBDEST'))")