A couple weeks ago I provided step-by-step install instructions to setup OpenCV 3.0 and Python 2.7+ on your Ubuntu machine.
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!
How to Install OpenCV 3.0 and Python 3.4+ on Ubuntu
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:
A few weeks ago I covered how to 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, 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)
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 and virtualenvwrapper 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
$ 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
$ 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 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:
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:
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 and checkout the
$ 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 for information on the latest release.
$ 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:
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
Here, our OpenCV bindings are stored under the name
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.so — this is so Python can import our OpenCV bindings using the name
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
Again, this 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
$ 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:
As you can see, OpenCV 3.0 with Python 3.4+ bindings has been successfully installed on my Ubuntu system!
What's next? I recommend PyImageSearch University.
25 total classes • 37h 19m video • Last updated: 7/2021
★★★★★ 4.84 (128 Ratings) • 10,597 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:
- ✓ 25 courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 25 Certificates of Completion
- ✓ 37h 19m on-demand video
- ✓ Brand new courses released every month, 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 400+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
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, 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.
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.