How to Install OpenCV 3.0 and Python 3.4+ on Ubuntu

转载 2015年10月31日 01:37:41


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

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:

Install developer tools used to compile OpenCV 3.0:

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

Install a few libraries used to read video formats from disk:

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

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

Step 2: Setup Python (Part 1)

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

Note that I have specifically indicated python3  when installing pip . If you do not supplypython3 , 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  virtualenv  and 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):

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:

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:

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:

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

If you end up getting a Permission denied error related to pip’s .cache  directory, like this:

Figure 1: If you get a "Permission Denied" error, no worries -- that's an easy fix!

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:

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

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

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 and checkout the3.0.0  version:

We’ll also need to grab the 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):

Time to setup the build:

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

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.

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:

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:

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/

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

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

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:

Notice how I am changing the name from  to  — 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  file):

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

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

Again, this is a very important step, so be sure that you have the  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 importcv2 :

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

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

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!


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.


31 Responses to Install OpenCV 3.0 and Python 3.4+ on Ubuntu

  1. Sébastien Vincent July 21, 2015 at 3:58 pm # 

    Hi Adrien,
    Thanks a lot! I followed your tutorial, and at last, I’ve been able to install OpenCV3 with Python3.4 on my Ubuntu VM.

    I have a related question : your instruction is to update the .bashrc with
    “export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3”
    so python3.4 will become the default interpreter in every future virtualenv.

    Is it still possible to create virtual environments with python2.7?

    • Adrian Rosebrock July 21, 2015 at 6:58 pm # 

      Hey Sébastien, you can still create virtual environments using Python 2.7. Using a command like this should work:

      $ mkvirtualenv foo --python python2.7

      • Sébastien Vincent July 27, 2015 at 3:14 pm # 

        Last question: is it safe to delete the opencv/build directory after install? Or must we keep it forever. Its size is 2.9Gb…

        • Adrian Rosebrock July 28, 2015 at 6:38 am # 

          As long as you have ran sudo make install, you can safely delete the build directory.

          • Sébastien Vincent July 28, 2015 at 2:10 pm # 

            ok, thanks.

  2. Ferdi Güler July 26, 2015 at 3:41 pm # 

    Hi Adrian,
    Such a good tutorial, would you mind explaining the installation steps if I choose to use standard virtual environment coming with Python3.4 (Python3 -m venv foo) instead of virtualenv?


    • Adrian Rosebrock July 26, 2015 at 4:48 pm # 

      Hey Ferdi, I’m actually unfamiliar with the virtual environment that comes with Python 3.4 (I’m just migrating to Python 3 myself — previously all I could use was Python 2.7). Do you have a link where I can read more about it?

      • Ferdi Güler July 27, 2015 at 4:41 am # 

        Hi Again, thanks for your reply. More information about Python’s native virtual environment can be found in the following link. It would be nice if can use it since it comes with Python 3.4 by default and there is no need to install any other 3rd party tools

        • Adrian Rosebrock July 27, 2015 at 6:05 am # 

          Awesome, thanks for passing along the link I’ll be sure to read up on it.

  3. chetan July 31, 2015 at 2:59 am # 

    Sir i am getting error as shown below. I have installed as per your instructions but still unable to install correctly. pls help me.

    root@chetan-VirtualBox:~# workon cv
    workon: command not found
    root@chetan-VirtualBox:~# python
    Python 2.7.6 (default, Jun 22 2015, 18:00:18)
    [GCC 4.8.2] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named cv2

    • Adrian Rosebrock July 31, 2015 at 7:06 am # 

      Hey Chetan — if you are getting an error related to the workon command, then it’s most likely that your ~/.bash_profile file was not updated corrected or was not reloaded via the source command. Go back to the “Step 2: Setup Python (Part 1)” section and ensure you have updated your ~/.bash_profile file to reflect the changes I suggested.

  4. Meareg August 3, 2015 at 4:38 am # 

    Hello adrian, very nice step-by-step tutorial. Thanks man!! :)
    I had to spend some time before figuring out I had to comment out export ‘PYTHONPATH=$PYTHONPATH:/usr/lib/local/python2.7/dist-packages’ (if such line exists in your ~/.bashrc file) and replace it with
    ‘PYTHONPATH=$PYTHONPATH:/usr/lib/local/python3.4/dist-packages’ or simply replace 2.7 with 3.4
    This will allow our openCV3.0 compilation to choose the python3.4 interpreter specifically and to include the opencv3.0’s bindings for python3.4 as shown above in step 3’s cmake output.

    • Adrian Rosebrock August 3, 2015 at 6:42 am # 

      Nice catch!

      • Mike Ream August 20, 2015 at 9:41 pm # 

        OK…here is what I did wrong….I had to exit in the middle of this process and when I came back into my terminal I was not on the cv environment when I did the cmake command. I tried running it again with no luck. I then removed the entire build directory and started again making sure I entered “workon cv” to be sure I was in the virtual environment. I followed the steps and all seems OK now.

        • Adrian Rosebrock August 21, 2015 at 7:17 am # 

          Nice, I’m glad it’s working for you Mike!

          And yes, to all other readers: if you leave your computer, restart it, open up a new terminal, etc. be sure to use the workon command to re-enter your virtual environment.

  5. Tony August 20, 2015 at 1:52 pm # 


  6. Mike Ream August 20, 2015 at 10:05 pm # 

    Sorry for all the comments. I also get the following output when I type cv2.__version__: ‘3.0.0-dev’ . Are other people getting the dev version? I was surprised to get a different output.

    • Adrian Rosebrock August 21, 2015 at 7:18 am # 

      After pulling down the repository from GitHub did you run git checkout 3.0.0? It seems like you might have forgotten the checkout command.

      • Mike Ream August 24, 2015 at 10:18 am # 

        Your suspicions were correct. I could have sworn that I ran the “git checkout 3.0.0″command, but I must have forgot it. After I followed the steps again, being more careful I do get the correct version installed.

        • Adrian Rosebrock August 24, 2015 at 11:11 am # 

          Fantastic, I’m glad it was just a git checkout issue :-)

  7. Tafadzwa August 25, 2015 at 4:56 am # 

    i’ve encountered an error whilst running “sudo pip3 install virtualenv virtualenvwrapper”…
    The response,”Cannot fetch base index………”

  8. Tafadzwa September 3, 2015 at 9:46 am # 

    Thanks a lot…..After running the command “cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON ..” i have encountered this error,”CMake Error at 3rdparty/ippicv/downloader.cmake:75 (message):
    ICV: Failed to download ICV package: ippicv_linux_20141027.tgz.
    Status=22;”HTTP response code said error”

    How can that be resolved??

    • Adrian Rosebrock September 3, 2015 at 12:21 pm # 

      That sounds like an error related to your internet connection and not OpenCV. The ICV package needs to be downloaded and for whatever reason, the the download is failing — likely due to a connectivity issue (either on your end or OpenCV’s).

  9. Nitin September 10, 2015 at 11:10 am # 

    If anyone found dist-package instead of site-package, then use these commands and i guess it will work…

    • Adrian Rosebrock September 11, 2015 at 6:34 am # 

      Thanks for sharing Nitin!

  10. Jonathan October 7, 2015 at 3:47 am # 


    I’ve run through the compile and make processes with no warnings or errors. When I run sudo ldconfig from the build directory, there is no output. And when I try to look for the opencv *.so files, I cannot find them (neither in /usr/local/lib/python3.4/dist-packages or site-packages directories. Do you know what might be causing the problem ? What information can I provide to help you understand the issue ? Thanks in advance – and for the great tutorial !

    • Adrian Rosebrock October 7, 2015 at 6:28 am # 

      Hey Jonathan — be sure to check for .so files in the build/lib directory. This has happened to me a few times before and then I just manually moved it to the site-packages directory.

  11. Milos October 21, 2015 at 11:11 am # 

    Nice, works out of the box. Although, I installed virtual environment with just “virtualenv env –python python3.4”.

    • Adrian Rosebrock October 22, 2015 at 6:20 am # 

      Awesome, I’m glad the tutorial worked for you Milos! :-)

  12. Shiva September 30, 2015 at 7:59 am # 

    Oops, looks like I’d forgotten to update the .bashrc file! It works fine now.

    My other query still stands!



参考 方法来自stackoverflow conda install --channel ...
  • mark199345
  • mark199345
  • 2016年11月25日 23:02
  • 11701

Python3 安装Opencv方法

到 下载所需的版本,注意版本号的对应。 opencv_python-3.1.0-cp35-cp35m-win_a...
  • HuangZhang_123
  • HuangZhang_123
  • 2017年03月19日 22:26
  • 3376

OpenCV在Python上的调用( import cv2的解决办法)

OpenCV是一个图像处理的经典库,而且关键是一直在升级。 为了在Python中调用opencv,特别是看到好多代码都是import cv2,而这个在python中是无法直接进行pip in...
  • sparkexpert
  • sparkexpert
  • 2016年06月22日 21:07
  • 41097


  • lwplwf
  • lwplwf
  • 2017年03月12日 13:26
  • 43040


  • superdont
  • superdont
  • 2015年06月08日 16:07
  • 22153

pip install whl 错误版本解决(暨在win7 python3下装 opencv)

非常感谢博客:,同时感谢stackoverflow非官方的python库  http://www.l...
  • wingfox117
  • wingfox117
  • 2017年05月26日 06:50
  • 2044

Windows7 64位+python3.4环境下安装opencv3.0的方法

在win7 64位系统下+python3.4环境下安装opencv3.0主要过程如下: 首先在windows的python扩展包网址:
  • qingyuanluofeng
  • qingyuanluofeng
  • 2016年06月01日 21:12
  • 8987

Window 64bit环境Python3 + OpenCV3开发环境搭建

1. Python3的安装可直接到下载 1)点击主页Downloads下的Windows 2)64位电脑下载Windows X86-64 execut...
  • YanDianAlan
  • YanDianAlan
  • 2017年02月16日 19:58
  • 3803

win10 中anaconda 安装python3版本的opencv

开发环境 win10 anaconda python3.5 安装opencvpip install opencv-python再通过 pip install opencv-contrib-py...
  • little_bobo
  • little_bobo
  • 2017年12月06日 21:52
  • 58


Ubuntu 16.04LTS版本,安装OpenCV3.0+环境。 转的文章,亲测可用! t
  • github_33934628
  • github_33934628
  • 2016年11月10日 22:50
  • 4839
您举报文章:How to Install OpenCV 3.0 and Python 3.4+ on Ubuntu