Skip to content

Instantly share code, notes, and snippets.

Created September 8, 2015 16:01
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?

Compiling OpenCV 3 with Python bindings and pyenv


Luckily Ubuntu has support for getting the build dependencies for a package directly via apt-get:

sudo apt-get build-dep opencv

In addition, we'll need a more modern CMake:

sudo add-apt-repository ppa:george-edison55/cmake-3.x
sudo apt-get update
sudo apt-get install cmake

Setting up the environment

Create somewhere to install OpenCV:

mkdir -p $HOME/opt/opencv
cd $HOME/opt/opencv

Getting the source

cURL is our friend. Use the -L option to process redirects.

curl -L | tar xvz
curl -L | tar xvz

Configuring and compiling

cd opencv-3.0.0
mkdir release; cd release
    OPENCV_EXTRA_MODULES_PATH=$HOME/opt/opencv/opencv_contrib-3.0.0/modules ..

Set python include dirs manually (both INCLUDE_DIRS and INCLUDE_DIRS2) and set library. Paths will be of form $HOME/.pyenv/versions/X.Y.Z/include/pythonX.Y and $HOME/.pyenv/versions/X.Y.Z/lib/ For Python 3, the pythonX.Y part may be pythonX.Ym.

make -j8 all install

Environment setup script

Add an environment setup script at ~/opt/opencv/

cat >$HOME/opt/opencv/ <<EOI
# source this file to set up environment
export OPENCV_PREFIX=$HOME/opt/opencv/
export PYTHONPATH=$OPENCV_PREFIX/lib/python3.4/site-packages:$OPENCV_PREFIX/lib/python2.7/site-packages


source $HOME/opt/opencv/
python -c 'import cv2'
python2 -c 'import cv2'
Copy link

hiagop commented Dec 2, 2015

Excuse my lack of knowledge, but where should I set those INCLUDE_DIRS on?

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