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.
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
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 https://github.com/Itseez/opencv.git cd opencv git checkout 3.1.0 cd .. git clone https://github.com/Itseez/opencv_contrib.git 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.
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 cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=OFF \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..
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)
(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 * cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=OFF \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv_contrib/modules \ -D BUILD_EXAMPLES=ON ..
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/libpython3.5m.so (ver 3.5.1) -- numpy: /home/amey/.pyenv/versions/lasagne/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.
After this, build and install OpenCV:
make -j4 sudo make install
Create a symbolic link
All that remains is to create a symlink to the OpenCV library that was (or should’ve been at least) installed to
cd ~/.pyenv/versions/lasagne/lib/python3.5/site-packages ln -s /usr/local/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so cv2.so
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:
python3 >>> import cv2 >>> cv2.__version__ '3.1.0' >>>
Of course, the
'3.1.0' version number is what I installed, yours should reflect the version that you installed.