chasep255 chasep255 - 7 months ago 111
Python Question

Ubuntu, how to install OpenCV for python3?

I want to install OpenCV for python3 in ubuntu 16.04. Fist I tried running

sudo apt-get install python3-opencv
which is how I pretty much install all of my python software. This could not find a repository. The install does work however if I do
sudo apt-get install python-opencv
this issue with this is that by not adding the three to python it installs for python 2 which I do not use. I would really perfer not to have to build and install from source so is there a way I can get a repository? I also tried installing it with pip3 and it could not find it either.


Well, this will be a lengthy answer, so let's start :

Step 1: Install prerequisites : Upgrade any pre-installed packages:

$ sudo apt-get update
$ sudo apt-get upgrade

Install developer tools used to compile OpenCV 3.0:

$ sudo apt-get install build-essential cmake git pkg-config

Install libraries and packages used to read various image and videos formats from disk:

$ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev

Install GTK so we can use OpenCV’s GUI features:

$ sudo apt-get install libgtk2.0-dev

Install packages that are used to optimize various functions inside OpenCV, such as matrix operations:

$ sudo apt-get install libatlas-base-dev gfortran

Step 2: Setup Python (Part 1)

Let’s get pip , a Python package manager, installed for Python 3:

$ wget
$ sudo python3

Let’s use our fresh pip3 install to setup virtualenv and virtualenvwrapper :

$ sudo pip3 install virtualenv virtualenvwrapper

Now we can update our ~/.bashrc file (place at the bottom of the file):

# virtualenv and virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/
$ source ~/.bashrc
$ mkvirtualenv cv

Step 2: Setup Python (Part 2)

we’ll need to install the Python 3.4+ headers and development files:

$ sudo apt-get install python3.4-dev

OpenCV represents images as NumPy arrays, so we need to install NumPy into our cv virtual environment:

$ pip install numpy

Step 3: Build and install OpenCV 3.0 with Python 3.4+ bindings

$ cd ~
$ git clone
$ cd opencv
$ git checkout 3.0.0
$ cd ~
$ git clone
$ cd opencv_contrib

$ git checkout 3.0.0 Time to setup the build:

$ cd ~/opencv
$ mkdir build
$ cd build
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \

Let's start OpenCV compile process :

$ make -j4

Assuming OpenCV 3.0 compiled without error, you can now install it on your system:

$ sudo make install
$ sudo ldconfig

Step 4: Sym-link OpenCV 3.0

If you’ve reached this step, OpenCV 3.0 should now be installed in /usr/local/lib/python3.4/site-packages/.

Here, our OpenCV bindings are stored under the name

However, in order to use OpenCV 3.0 within our cv virtual environment, we first need to sym-link OpenCV into the site-packages directory of the cv environment, like this: (Be sure to take note of

$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
$ ln -s /usr/local/lib/python3.4/site-packages/

Notice how I am changing the name from to — this is so Python can import our OpenCV bindings using the name cv2 .

Step 5: Test out the OpenCV 3.0 and Python 3.4+ install

$ workon cv
$ python
>>> import cv2
>>> cv2.__version__

Hope that helps. Also, credit to Adrian Rosebrock on his post. It worked for me as a charm.