Install OpenCV 3.1.0 with Python 3.5+ in Ubuntu 14.04

Apr 19, 2016

Getting OpenCV 3 up and running with Python 3.5+ on Ubuntu 14.04 is a pain in the ass. For the most part the instructions here are the same as in this guide, with a few changes needed specifically for Python 3.5+. I’m going to assume that you already have pyenv and pyenv-virtualenv installed for the purpose of this guide.

Install prerequisites

sudo apt-get update
sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libjpeg8-dev libtiff4-dev libjasper-dev
sudo apt-get install -y libpng12-dev libavcodec-dev
sudo apt-get install -y libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install -y libgtk2.0-dev libatlas-base-dev gfortran

Install python headers

This is the slightly tricky part. We want OpenCV to work with Python 3.5+, but Ubuntu 14.04 only comes with Python 3.4.x. So, we’ll have to turn to an external repo:

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install -y python3.5-dev

This will download and install the headers and libraries for the latest release of Python 3.5.x. This number will vary as this post gets older, and I don’t know of a way to check exactly which version is installed. However, for OpenCV to work with virtualenv, we’ll need to know the exact version of python3 headers that are installed. I’ll explain how to do this next.

Fetch opencv and opencv_contrib

The 3.1.0 in the commands below was the latest released version of OpenCV at the time of this post. Check OpenCV News for the latest release number when you run this, and substitute below if it’s different.

cd /tmp
git clone
cd opencv
git checkout 3.1.0

cd ..
git clone
cd opencv_contrib
git checkout 3.1.0

Try (and fail) to build OpenCV

We first need to modify the OpenCV build configuration to use python3.5 instead of python3.4.

gedit /tmp/opencv/cmake/OpenCVDetectPython.cmake

In the file that opens, find a line that starts with find_python(3.4 and change it to find_python(3.5. Save the file and quit the editor.

At this point, OpenCV configuration will fail because we haven’t set up python and numpy yet. The only reason for the following step is to find out the version of python3 headers and libs that was installed.

cd /tmp/opencv
mkdir build && cd build
-D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib/modules \

cmake will succeed, print a long list of configuration variables. Look for lines that look like this:

--   Python 3:
--     Interpreter:  /home/amey/.pyenv/shims/python3.5 (ver 3.5.1)

The (ver 3.5.1) is the part we’re interested in. That’s the python version we’ll have to use.

Create python environment and install numpy

pyenv virtualenv 3.5.1 cv
pyenv activate cv
pip install numpy

Build OpenCV for real

cd /tmp/opencv/build
rm -rf *
-D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib/modules \

If everything goes according to plan, the output will change and you’ll see something like:

-- Python 3:
--   Interpreter:    /home/amey/.pyenv/shims/python3.5 (ver 3.5.1)
--   Libraries:      /usr/lib/x86_64-linux-gnu/ (ver 3.5.1)
--   numpy:          /home/amey/.pyenv/versions/cv/lib/python3.5/site-packages/numpy/core/include (ver 1.11.0)
--   packages path:  lib/python3.5/site-packages

As you can see, cmake has picked up a lot more info this time around. If you still only see an Interpreter line and nothing else.. well, I’m sorry I guess.

Edit - Try this alternative command, sourced from the guide for anaconda:

cmake -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DENABLE_AVX=ON -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DWITH_VTK=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=$(python3 -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(which python3) -DPYTHON_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..

After this, build and install OpenCV:

make -j4
sudo make install

Optional - Create a symbolic link

(Edit - This is optional because if you built and installed OpenCV using the Anaconda command above, the library has been installed in the pyenv directory already.)

All that remains is to create a symlink to the OpenCV library that was (or should’ve been at least) installed to /usr/local/lib.

cd ~/.pyenv/versions/cv/lib/python3.5/site-packages
ln -s /usr/local/lib/python3.5/site-packages/

If you get an error while running the ln command, check the /usr/local/lib/python3.5/site-packages/ directory. It’s possible that your library might have been built with a different name. If that directory is empty, well, sorry again.


If nothing went wrong, you should now be able to test the OpenCV installation. Activate the virtualenv if it isn’t already active and try the following:

>>> import cv2
>>> cv2.__version__

Of course, the '3.1.0' version number is what I installed, yours should reflect the version that you installed.