Title: Install OpenCV 3.0 and Python 3 on Ubuntu - PyImageSearch

URL Source: https://pyimagesearch.com/2015/07/20/install-opencv-3-0-and-python-3-4-on-ubuntu/

Published Time: 2015-07-20T14:00:21+00:00

Markdown Content:
[![Image 1: ubuntu_cv3py3_facedetection](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20480'%3E%3C/svg%3E)](https://pyimagesearch.com/practical-python-opencv/?src=opencv3-install)

A couple weeks ago I provided step-by-step install instructions to [setup OpenCV 3.0 and Python 2.7+ on your Ubuntu machine](https://pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/).

However, one of the huge benefits of migrating to OpenCV 3.0 is the new Python 3.4+ support. In the previous 2.4.X releases of OpenCV, only Python 2.7+ was supported. But now, we can _finally_ leverage Python 3.4+ in our new projects.

In the remainder of this blog post, I’ll detail how to install OpenCV 3.0 with Python 3.4+ bindings on your Ubuntu 14.04+ system. If you have followed along from the previous tutorial, you’ll notice that many of the steps are the same (or at least very similar), so I have condensed this article a bit. That said, be sure to pay special attention when we start working with CMake later in this tutorial to ensure you are compiling OpenCV 3.0 with Python 3.4+ support!

**UPDATE:** The tutorial you are reading now covers how to install OpenCV 3.0 with Python 3.4+ bindings on _**Ubuntu 14.04**_. This tutorial _**still works perfectly**_, but if you want to install OpenCV on the newer _**Ubuntu 16.04**_ with OpenCV 3.1 and Python 3.5+, please use this freshly updated tutorial:

[https://pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/](https://pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/)

* * *

A few weeks ago I covered how to install [OpenCV 3.0 and Python 2.7+ on Ubuntu](https://pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/), and while this was a great tutorial (since many of us are still using Python 2.7), I think it’s really missing out on one of the major aspects of OpenCV 3.0 — _**Python 3.4+ support!**_

That’s right, up until the v3.0 release, OpenCV _only_ provided bindings to the Python 2.7 programming language.

And for many of us, that was okay. As scientific developers and researchers, it’s a pretty standard assumption that we’ll be sequestered to Python 2.7.

However, that’s starting to change. Important scientific libraries such as NumPy, SciPy, and scikit-learn are now providing Python 3 support. And now OpenCV 3.0 joins the ranks!

In general, you’ll find this tutorial very similar to the previous one on [installing OpenCV 3.0 and Python2.7 on Ubuntu](https://pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/), so I’m going to condense my explanations of each of the steps as necessary. If you would like to full explanation of each step, please refer to the previous OpenCV 3.0 article. Otherwise, simply follow along with this tutorial and you’ll have OpenCV 3.0 and Python 3.4+ installed on your Ubuntu system in less than 10 minutes.

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 formats_ from disk:

$ sudo apt-get install libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev

Install a few libraries used to read _video formats_ from disk:

$ sudo apt-get install 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 https://bootstrap.pypa.io/get-pip.py
$ sudo python3 get-pip.py

Note that I have **_specifically indicated `python3`_** when installing `pip` . If you do not supply `python3` , then Ubuntu will attempt to install `pip` on your Python 2.7 distribution, which is not our desired intention.

Alright, so I’ve said it before on the PyImageSearch blog, and I’ll see it again. **_You should really be using virtual environments for Python development!_**

We’ll be using [virtualenv](https://virtualenv.pypa.io/en/latest/) and [virtualenvwrapper](https://virtualenvwrapper.readthedocs.org/en/latest/) in this tutorial. These packages allow us to create _**entirely separate and independent Python environments**_, ensuring that we don’t junk up our system Python install (and more importantly, so we can have a separate Python environment for each of our projects).

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

$ sudo pip3 install virtualenv virtualenvwrapper

Again, notice how I am specifying `pip3` instead of just `pip` — I’m just making it explicitly obvious that these packages should be installed for Python 3.4.

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/virtualenvwrapper.sh

Notice how I am pointing `VIRTUALENVWRAPPER_PYTHON` to where our Python 3 binary lives on our Ubuntu system.

To make these changes take affect, you can either open up a new terminal or reload your `~/.bashrc` file:

$ source ~/.bashrc

Finally, let’s create our `cv` virtual environment where we’ll be doing our computer vision development using OpenCV 3.0 and Python 3.4:

$ mkvirtualenv cv

Step 2: Setup Python (Part 2)
-----------------------------

We’re halfway done setting up Python. But in order to compile OpenCV 3.0 with Python 3.4+ bindings, 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](http://www.numpy.org/) into our `cv` virtual environment:

$ pip install numpy

If you end up getting a _Permission denied_ error related to pip’s `.cache` directory, like this:

[![Image 2: Figure 1: If you get a "Permission Denied" error, no worries -- that's an easy fix!](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20480'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/06/ubuntu_pip_permission_error.jpg)

**Figure 1:** If you get a “Permission Denied” error, no worries — that’s an easy fix!

Then simply delete the cache directory and re-run the NumPy install command:

$ sudo rm -rf ~/.cache/pip/
$ pip install numpy

And you should now have a nice clean install of NumPy:

[![Image 3: Figure 2: Deleting the .cache/pip directory and re-running pip install numpy will take care of the problem.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20480'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/06/ubuntu_numpy_installed.jpg)

**Figure 2:** Deleting the .cache/pip directory and re-running pip install numpy will take care of the problem.

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

Alright, our system is all setup now! Let’s [pull down OpenCV from GitHub](https://github.com/Itseez/opencv) and checkout the `3.0.0` version:

$ cd ~
$ git clone https://github.com/Itseez/opencv.git
$ cd opencv
$ git checkout 3.0.0

_**Update (3 January 2016):** You can replace the `3.0.0` version with whatever the current release is (as of right now, it’s `3.1.0` ). Be sure to check [OpenCV.org](http://opencv.org/category/news) for information on the latest release._

We’ll also need to grab the [opencv\_contrib](https://github.com/itseez/opencv_contrib) repo as well (for more information as to why we need `opencv_contrib` , take a look at my [previous OpenCV 3.0 Ubuntu install post](https://pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/)):

$ cd ~
$ git clone https://github.com/Itseez/opencv\_contrib.git
$ cd opencv\_contrib
$ git checkout 3.0.0

Again, make sure that you checkout the _**same version**_ for `opencv_contrib` that you did for `opencv` above, otherwise you could run into compilation errors.

Time to setup the build:

$ cd ~/opencv
$ mkdir build
$ cd build
$ cmake -D CMAKE\_BUILD\_TYPE=RELEASE \\
	-D CMAKE\_INSTALL\_PREFIX=/usr/local \\
	-D INSTALL\_C\_EXAMPLES=ON \\
	-D INSTALL\_PYTHON\_EXAMPLES=ON \\
	-D OPENCV\_EXTRA\_MODULES\_PATH=~/opencv\_contrib/modules \\
	-D BUILD\_EXAMPLES=ON ..

_**Update (3 January 2016):** In order to build OpenCV `3.1.0` , you need to set `-D INSTALL_C_EXAMPLES=OFF` (rather than `ON` ) in the `cmake` command. There is a bug in the OpenCV v3.1.0 CMake build script that can cause errors if you leave this switch on. Once you set this switch to off, CMake should run without a problem._

Let’s take a second to look at my CMake output:

[![Image 4: Figure 3: It's a good idea to inspect the output of CMake to ensure the proper Python 3 interpreter, libraries, etc. have been picked up.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20480'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/06/ubuntu_cv3py3_cmake_output.jpg)

**Figure 3:** It’s a good idea to inspect the output of CMake to ensure the proper Python 3 interpreter, libraries, etc. have been picked up.

Notice how CMake has been able to pick up our Python 3 interpreter! This indicates that OpenCV 3.0 will be compiled with our Python 3.4+ bindings.

Speaking of compiling, let’s go ahead and kickoff the OpenCV compile process:

$ make -j4

Where the _4_ can be replaced with the number of available cores on your processor to speedup the compilation time.

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/`

[![Image 5: Figure 4: The Python 3.4+ OpenCV 3.0 bindings are now installed in /usr/local/lib/python3.4/site-packages/](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20480'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/06/ubuntu_cv3py3_compiled.jpg)

**Figure 4:** The Python 3.4+ OpenCV 3.0 bindings are now installed in /usr/local/lib/python3.4/site-packages/

Here, our OpenCV bindings are stored under the name `cv2.cpython-34m.so`

_**Be sure to take note of this filename, you’ll need it in just a few seconds!**_

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:

$ cd ~/.virtualenvs/cv/lib/python3.4/site-packages/
$ ln -s /usr/local/lib/python3.4/site-packages/cv2.cpython-34m.so cv2.so

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

So now when you list the contents of the `cv` virtual environment’s `site-packages` directory, you’ll see our OpenCV 3.0 bindings (the `cv2.so` file):

[![Image 6: Figure 5: In order to access the OpenCV 3.0 bindings from our Python 3.4+ interpreter, we need to sym-link the cv2.so file into our site-packages directory.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20480'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/06/ubuntu_cv3py3_symlinked.jpg)

**Figure 5:** In order to access the OpenCV 3.0 bindings from our Python 3.4+ interpreter, we need to sym-link the cv2.so file into our site-packages directory.

_**Again, t****his is a very important step, so be sure that you have the `cv2.so` file in your virtual environment, otherwise you will not be able to import OpenCV in your Python scripts!**_

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

**Nice work! You have successfully installed OpenCV 3.0 with Python 3.4+ bindings (and virtual environment support) on your Ubuntu system!**

But before we break out the champagne and beers, let’s confirm the installation has worked. First, ensure you are in the `cv` virtual environment, then fire up Python 3 and try to import `cv2` :

$ workon cv
$ python
>>> import cv2
>>> cv2.\_\_version\_\_
'3.0.0'

Here’s an example of me importing OpenCV 3.0 using Python 3.4+ on my own Ubuntu system:

[![Image 7: Figure 6: OpenCV 3.0 with Python 3.4+ bindings has been successfully installed on the Ubuntu system!](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20480'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2015/06/ubuntu_cv3py3_installed.jpg)

**Figure 6:** OpenCV 3.0 with Python 3.4+ bindings has been successfully installed on the Ubuntu system!

As you can see, OpenCV 3.0 with Python 3.4+ bindings has been successfully installed on my Ubuntu system!

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

In this tutorial I have demonstrated how to install OpenCV 3.0 with Python 3.4+ bindings on your Ubuntu system. This article is very similar to our previous tutorial on [installing OpenCV 3.0 and Python 2.7 on Ubuntu](https://pyimagesearch.com/2015/06/22/install-opencv-3-0-and-python-2-7-on-ubuntu/), but takes advantage of OpenCV 3.0’s new Python 3+ support, ensuring that we can use the Python 3 interpreter in our work.

While having Python 3.4+ support is really awesome and is certainly the future of the Python programming language, I would also advise you to take _special care_ when considering migrating from Python 2.7 to Python 3.4. For many scientific developers, the move from Python 2.7 to 3.4 has been a slow, arduous one. While the big Python packages such as NumPy, SciPy, and scikit-learn have made the switch, there are still other smaller libraries that are dependent on Python 2.7. That said, if you’re a scientific developer working in computer vision, machine learning, or data science, you’ll want to be careful when moving to Python 3.4 as you could easily pigeonhole your research.

Over the coming weeks the OpenCV 3.0 install-fest will continue, so if you would like to receive email updates when new install tutorials are released (such as installing OpenCV 3.0 with Homebrew, installing OpenCV 3.0 on the Raspberry Pi, and more), please enter your email address in the form below.

![Image 8](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Join the PyImageSearch Newsletter and Grab My FREE 17-page Resource Guide PDF

Enter your email address below to **join the PyImageSearch Newsletter** and **download my FREE 17-page Resource Guide PDF** on Computer Vision, OpenCV, and Deep Learning.