Install OpenCV 3.0 and Python 3.4+ on Ubuntu


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:

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 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  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 the 3.0.0  version:

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 for information on the latest release.

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):

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:

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:

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 import cv2 :

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.

, , ,

201 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.

    • Wagon February 15, 2016 at 9:19 pm #

      I as well had to rename


      Everything worked great installed version 3.1.0 on Ubuntu 14.04

      Adrian you by far have the best tutorials out there. Super appreciate what you do.

      • Adrian Rosebrock February 16, 2016 at 3:41 pm #

        Thanks for the kind words 🙂 And congrats on getting OpenCV installed!

        • Shweta Philip December 15, 2016 at 2:33 pm #

          Hi I cannot find the cv2 files in dist-packages as well as site-packages and everything worked without error up to this step.

          • Adrian Rosebrock December 18, 2016 at 9:05 am #

            If make exited without error then there was like an issue with your CMake configuration, specifically with the Python 3 section. I would double-check the “Python 3” output. Also, this tutorial has been updated via this one, so there is a chance that you are following the wrong tutorial.

  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!

      • Thomas D. September 26, 2016 at 1:14 pm #


        i use your guide today at xubuntu 16.04.1 LTS.
        My link is now:
        ln -s /usr/local/lib/python3.5/dist-packages/

        regards Thomas

        • Adrian Rosebrock September 27, 2016 at 8:44 am #

          Thanks for sharing Thomas! I’ll actually have an updated tutorial covering Ubuntu 16.04 LTS online within the next couple of weeks.

  10. 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!

  11. 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.

      • savolla May 23, 2017 at 7:26 am #

        did you moved all those files???

  12. 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! 🙂

  13. Almazi October 27, 2015 at 3:47 pm #

    Thanks for this great tutorial which worked pretty good. But everytime I start terminal the “workon command: not found” then I have to go thru exporting the virtualenv and virtualenvwrapper. Why is it happening and is it normal? Is there any way to make a virtual terminal which will work for cv directly?

    • Adrian Rosebrock November 3, 2015 at 10:43 am #

      Hey Almazi — just to clarify: only the workon command is not found? The mkvirtualenv and rmvirtualenv commands work?

      • Andrea November 12, 2015 at 10:10 am #

        Hi Adrian, thanks a lot for your tutorial.
        I’m having the same problem as Almazi, and I get also the same message when executing mkvirtualenv and rmvirtualenv, how can a deal with it?
        Thanks in advance

        • Adrian Rosebrock November 12, 2015 at 12:09 pm #

          Hey Andrea — you might want to take a look at the “Troubleshooting” section of this post. While the instructions are for the Raspberry Pi, you can apply the same logic to fix the virtualenv commands.

  14. peemji November 1, 2015 at 1:36 pm #

    I’m working on a SIFT project that need matplotlib as prerequisite but I can’t build(หรือ compile) it. Could you give me any example or instruction?

    • Adrian Rosebrock November 3, 2015 at 10:19 am #

      Please see my reply to Sommai above.

  15. Sommai Dev November 2, 2015 at 4:27 am #

    I’m working on a SIFT project that need matplotlib as prerequisite but I can’t build it to virualane . Could you give me any example or instruction?

    • Adrian Rosebrock November 3, 2015 at 10:17 am #

      Please see this tutorial on how to install matplotlib into the cv virtual environment.

  16. Evan November 5, 2015 at 11:57 pm #

    The best, most straightforward installation procedure I’ve ever read. THANKS!!!

    • Adrian Rosebrock November 6, 2015 at 6:19 am #

      Thanks so much Evan, I’m glad it worked for you! 🙂

  17. Baptiste November 8, 2015 at 12:16 pm #

    Works great in Terminal, but i can’t import cv2 in idle or spyder ? how do configure them to work in your virtual cv ? Thanks !

    • Adrian Rosebrock November 8, 2015 at 3:19 pm #

      I’m not familiar with the Sypder IDE, but if it’s anything like PyCharm, you just need to set the proper virtual environment for the project. Please see this post for more information.

      • Baptiste November 10, 2015 at 1:38 am #

        It works !!! with Eclipse or Pycharm … I don’t understand why not with my spyder ? I Have yet some trouble to solve with matplotlib… (I will try your patch of your blog ) – Thank you very much !

  18. waspinator November 18, 2015 at 10:51 am #

    have you heard of Conda/Anaconda? It makes managing your packages and environments easier. I think it would be a better fit for computer vision researchers and practitioners than pip/virtualenv which are designed more for python programmers.

    • Adrian Rosebrock November 18, 2015 at 6:54 pm #

      Yes, I’ve heard of and used conda before. They actually used to include OpenCV in their pre-compiled packages but it was removed awhile back. I also don’t talk about it publicly, but the Continuum company and myself had a pretty big falling out (at least on my end of things). You won’t see me use their products on the blog.

    • Derrick February 25, 2017 at 11:18 am #

      I agree

  19. Jack December 7, 2015 at 8:02 pm #

    I’m looking to connect my “cv” virtualenv that was created in this tutorial to PyCharm (as per one of your other tutorials) but I can’t figure out where the “cv” virtualenv is stored. As per this guide, where do I look to find that virtual environment? (also what is it’s final extension?)
    Thanks a million,

    Ahh, of course. I try to dig around as long as I can before posting stupid comments. I found the .virtualenvs dir inside my home directory then all I had to do was point the PyCharm interpreter at .virtualenvs/cv/bin/python3.4 (as reference for anyone else in my position).
    Thanks for the guide!

  20. saborae December 9, 2015 at 9:22 pm #

    Hey, I did as you instructed and I get the similar output on the cmake but after the packages path: site-packages, there’s another line that says python(for build): 2.7

    Also, compiling with -j4, I get several warnings all .cpp or .hpp files. Anyway, the install works, with my camera, video files, all fine but now, how do I use opencv on my regular python install? should I redo all the steps without virtualenv?

    • Adrian Rosebrock December 10, 2015 at 6:52 am #

      You can ignore the “for build” section, that part of the CMake script is buggy. As for using OpenCV in your regular Python install, just the file into your regular site-packages directory (if it’s not already there). From there, you’ll be able to import OpenCV outside of the virtual environment.

  21. Hacklavya December 17, 2015 at 1:42 pm #

    I want to keep both
    will this tutorial keep older one intact ?

    Actually I want all following on my system, so that I can use according to need:-
    1. opencv 3.0 & python 3.4
    2. opencv 3-0 & python 2.7
    3. opencv 2.4 & python 2.7
    How can I have all the three ?

    • Adrian Rosebrock December 18, 2015 at 5:56 am #

      It certainly is possible, but it’s non-trivial. The trick is to compile and install OpenCV 3.0 (for either Python version first). But that is the only time you run make install. Afterwards, you create separate Python virtual environments for the remaining two installs. Then you compile your respective Python versions. But only keep the build directory after compiling. You can then sym-link in the bindings from the respective build/lib directories into the correct Python virtual environments.

      Like I said, it’s not easy to do — but it’s absolutely possible.

  22. Tom Vandermolen December 21, 2015 at 2:11 pm #

    FYI, for those who are using Anaconda as their python install, here’s a site that goes through installing OpenCV 3 (worked for me):

    • Adrian Rosebrock December 21, 2015 at 5:30 pm #

      Thanks for sharing Tom!

    • srinath January 14, 2016 at 4:09 am #

      Thanks a ton sir. This is what I was looking. I did everything what is in this blog and didn’t get the library file. I was thinking it had something to do with anaconda. But thanks a lot. Now how to update the package in future? Until and unless the user updates, we can’t do anything?

  23. armin December 23, 2015 at 8:20 am #

    Can i install different versions of python on one machine too? e.g python 3.4 with opencv 3 in one virtual environment and python 2.7 with opencv 2.4 in other one?

    • Adrian Rosebrock December 23, 2015 at 1:09 pm #

      You can, but it’s not a trivial process. See my reply to Hacklavya above for more information.

  24. alfian fermana December 24, 2015 at 1:30 pm #

    sudo python3

    i cant execute this . can you tell me how ASAP. thank you

    I dont understand this line

    “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.”

    how to do that? thank you please help

    • Adrian Rosebrock December 25, 2015 at 12:34 pm #

      Why can’t you execute the command? Are you getting an error message?

  25. Ashwin January 8, 2016 at 12:29 pm #

    I am using ubuntu 14.04 and python2.7.6 was already in this. I installed opencv and python3.4.
    opencv can be imported using python2.7 but cant be used by python3.4

    • Adrian Rosebrock January 8, 2016 at 1:12 pm #

      Double check your output of CMake and sure Python 3.4 was properly picked up. It sounds like OpenCV support was compiled for Python 2.7.

  26. David January 10, 2016 at 10:55 pm #

    This tutorial is great, but I had one problem with it that wound up taking me a day to debug.

    The current version of cmake installed by aptitude does not search for python libraries beyond version 3.3, which means it couldn’t find the libraries my 3.4 version of python.

    The solution is add an option to cmake: “-DPYTHON_LIBRARIES=/usr/lib/x86_64-linux-gnu/”

    Once I added this configuration option, then magically appeared (albeit in “/dist-packages/”, not “/site-packages/”) and all was resolved.

    • Adrian Rosebrock January 11, 2016 at 6:37 am #

      Thanks for sharing your solution David!

  27. Matt January 13, 2016 at 10:33 am #

    Thanks, Adrian! Nice work.

    • Adrian Rosebrock January 13, 2016 at 2:01 pm #

      Thanks Matt! 🙂

  28. Alan January 24, 2016 at 12:34 pm #

    Brilliant writeup, and great job with the site. Not sure if you studied with him, but it has Ramit’s fingerprint all over it…in a good way!

    On AWS Ubuntu, attempting to import cv2 gave:

    The temporary fix for this is to associate null device with raw1394 (firewire, which we don’t need anyways).

    • Adrian Rosebrock January 25, 2016 at 4:19 pm #

      Thanks for sharing Alan! And which Ramit is that 😉 I assume Sethi?

  29. Gustavo February 4, 2016 at 1:20 pm #

    Nice article… I will test it using Gnome Ubuntu 16.04 Alpha 2 🙂 Thanks

  30. Liya February 5, 2016 at 12:02 am #

    Hi Adrian,

    It would be great if you can have a similar tutorial for setting up open cv on Red Hat OS.


  31. asitha March 3, 2016 at 5:57 am #

    hi Adrian,
    is it possible to install opencv 3 on a raspberry pi 2 which is running ubuntu?

    • Adrian Rosebrock March 3, 2016 at 6:58 am #

      Yes, you can absolutely install OpenCV 3 on a Raspberry Pi that is running Ubuntu. The same instructions apply.

  32. PeterPan March 8, 2016 at 4:58 pm #

    Hey Adrian,

    great tutorial – thank you very much!

    You could save a lot of time and disc space by just downloading and extracting the release archives of opencv and opencv_contrib instead of cloning the whole git repository.

    For version 3.1.0, they can be found here:

    • Adrian Rosebrock March 8, 2016 at 5:18 pm #

      Thanks for sharing the tip!

  33. Qubix March 12, 2016 at 7:22 am #

    Thanks a lot for the tutorial . I managed to install everything in Kubuntu 15.10. Works great. However, I have a question. Why do you have to be inside of this cv environment (workon cv) to be able to import cv. If I just open up a terminal and write python3 , then import cv2, it does not work. Could you explain a bit just what that environment is and why this library does not work outside of it?

    • Adrian Rosebrock March 13, 2016 at 10:21 am #

      For a more detailed discuss of Python virtual environments (and why they are important), I would suggest giving this article a read. The short answer is that virtual environments give you independent and sequestered environments for each of your projects — that way, you don’t have to worry about conflicting library versions from one project to the next.

  34. luis mendoza April 4, 2016 at 2:17 pm #

    Hello could you helpme?

    just after I writo: impor cv2:
    I I get:
    Traceback (most recent call last):
    ImportError: No mudule named ‘cv2’

  35. slava April 13, 2016 at 10:03 am #

    Hey, Adrian.
    How can i cancel all changes i did?
    I want to try all steps from the beginning with a clear system?

    • Adrian Rosebrock April 13, 2016 at 6:51 pm #

      It depends on what steps you want to restart from. But in general, the best way to simply restart and clear the system is to re-install Ubuntu and start fresh.

  36. Helios April 23, 2016 at 4:45 pm #

    I followed the tutorial and it worked for me.
    But everytime when I need to use OpenCV, i should run “mkvirtualenv cv” and then use python.

    Is there any way to avoid this virtual environment?

    • Adrian Rosebrock April 25, 2016 at 2:06 pm #

      Using Python virtual environments is a best practice and I highly recommend you use them. That said, if you want to skip using them, just ignore all steps that relate to virtualenv and virtualenvwrapper.

  37. Paul April 23, 2016 at 9:48 pm #

    Thanks for this Adrian. A nice clean way to get things set up. I experimented inside a xubuntu 14.04 VM also, just to keep things even cleaner..

    However, I found for modules other than the ones starting with x, like xfeatures2d, the other contrib modules don’t export to python2/3. I checked my ../build/modules/python3/pyopencv_generated_include.h for the modules, and they are not listed. I checked here to see why not all contrib modules are listed in that file:

    Seems the headers are not properly tagged with export macros for opencv3…
    Else, i’m doing something wrong…

    Adrain, or anyone else, did you get all the contrib modules working? Does anyone have an example of objectnessBing working following this method?


    • Adrian Rosebrock April 25, 2016 at 2:04 pm #

      Hey Paul — not all modules in opencv_contrib have Python bindings. This could be because the modules are now and experimental, or there has not been a request to create the Python bindings. Again, it’s normal for not all modules in opencv_contrib to have Python bindings.

  38. Ashing April 26, 2016 at 7:58 am #

    Hi Adrian,
    Thanks a lot! I followed your tutorial, I installed openCV 3.1.0 with python3.5. on my Raspbarry pi 3 with ubuntu 16.04 mate.

    I just met one error before .
    Need to change the name “” to “” in my case.
    Just said thank you again,OpenCV is really interesting .

    • Adrian Rosebrock April 26, 2016 at 5:09 pm #

      Thanks for sharing the update Ashing!

  39. Yasin May 5, 2016 at 12:11 am #

    Thanks a lot for the great tutorial!
    One thing that confuses me: the package name.
    So even though we are using opencv 3, we nees to import “cv2”?? Not “cv3”?
    Thank you.

    • Adrian Rosebrock May 5, 2016 at 6:44 am #

      Yep, it’s quite confusing! The name of the package for both OpenCV 2.4 and OpenCV 3 is cv2. I’m not sure why they didn’t change the name to cv3 for OpenCV 3, but that’s just how it is!

  40. oglop May 7, 2016 at 11:13 am #

    Thank you It worked for python3!

  41. Ahmed Henteti May 11, 2016 at 4:52 pm #

    Thank you

  42. Aditya May 13, 2016 at 9:03 am #

    Hey Adrian!
    I installed 3.1.0 on Ubuntu 15.10 with this guide and it works like a charm! Thanks!
    I have a question– Now after installing openCV with python, if I want to use it with C/C++ what do I need to do?

    • Adrian Rosebrock May 13, 2016 at 11:33 am #

      I don’t formally support C/C++ code on the PyImageSearch blog, but you’ll need to use g++ to compile your code.

  43. Abhishek Shetty May 17, 2016 at 9:31 am #

    This works great. But I need to install pycuda with this opencv installation. What procedure should be followed?? Coz I need them both for same program.

  44. Mayank May 18, 2016 at 6:58 am #

    Hey Adrian!!
    I am currently using opencv 2.4 with python 2.7. Now i want to upgrade to opencv 3 but is it recommended to upgrade python too. Will python 2.7 be not effective with OpenCV 3 ??

    • Adrian Rosebrock May 19, 2016 at 6:06 pm #

      You certainly don’t need to upgrade to Python 3 if you don’t want to. OpenCV 3 is just as compatible and effective with Python 2.7 as it is Python 3.

  45. Alex May 18, 2016 at 7:52 pm #

    Thank you, only by your guide I’ve successfully installed OpenCV 3.1.0 on a clean Ubuntu 16.04 with Python 3.5, everything went smoothly. At first I tried Ubuntu 14.04 but something went wrong and there was no cv2*.so file in site-packages after installation.

    • Adrian Rosebrock May 19, 2016 at 6:01 pm #

      Congrats on getting OpenCV installed Alex!

  46. Richard May 21, 2016 at 7:21 pm #

    Hi Adrian,

    I am trying to install OpenCV along with the extra modules but is still running into issues with sift being not defined.

    I have followed your instructions step by step and also calling sift with:

    sift = cv2.xfeatures2d.SIFT_create()

    I am still getting: ‘module’ object has no attribute ‘xfeatures2d;.

    Please advise.

    Much appreciation in advance.

    • Adrian Rosebrock May 23, 2016 at 7:29 pm #

      Double-check your output of CMake and ensure that xfeatures2d is listed in the set of OpenCV modules to be compiled. If it’s not, then it’s likely that your path to the opencv_contrib directory is incorrect.

  47. John Boss May 28, 2016 at 2:16 am #

    First of all great tutorial. I have done everything as mentioned in the blog. I am able to use cv2.imshow() with images. When I try to use cv2.imshow() in videocapture(I am using logitech webcam) no errors are generated but no window with video opens up. When I try to store one of those frame as images it works. So how do I get video feed window opened? (Note: I am using Linux Mint 17.3 xfce)

    • Adrian Rosebrock May 29, 2016 at 1:58 pm #

      How are you accessing your system? Natively with keyboard and monitor? Or via SSH or VNC? I also haven’t tried accessing a webcam via Linux Mint, so I’m not sure if that would cause a problem or not.

  48. Linus May 29, 2016 at 2:05 pm #

    Thanks Adrian for this easy to understand tutorial, even for Ubuntu beginners like me!

    • Adrian Rosebrock May 29, 2016 at 3:26 pm #

      No problem, I’m happy I could help! 🙂

  49. Jade June 8, 2016 at 5:55 am #

    Thank you for your article, very useful I managed easily to install OpenCV working with Python thanks to your explanations 🙂

    • Adrian Rosebrock June 9, 2016 at 5:28 pm #

      Congrats on getting OpenCV installed, that’s awesome! 🙂

  50. Tom C June 11, 2016 at 4:33 pm #

    Hi Adrian, thanks for the in-depth tutorial, but I ran into a problem and am interested to see if you might know what’s up here.

    I’m attempting to install opencv 3.1.0 with python 3.4.3 on Ubuntu 14.04. After running sudo make install and sudo ldconfig (which gives no output) I end up with a bunch of libopencv_*.so files in /usr/local/lib/ (corresponding to different opencv modules, I believe) and all the same ones in opencv/build/lib/, however I have no in my /usr/local/lib/python3.4/dist-packages/ folder, nor a copy of it in the opencv/build/lib folder. I attempted to find it using sudo updatedb && locate in case it was created somewhere else, but had no success.

    I saw that another commenter here had a similar issue, then upgraded to Ubuntu 16.04, and then was successful, but upgrading from 14.04 isn’t quite an option for me right now. Do you have any idea what I can do differently in compilation / configuration, or with my current results, in order to get opencv working?



    • Adrian Rosebrock June 12, 2016 at 9:34 am #

      Hey Tom — if you don’t have a .so file for your OpenCV bindings in opencv/build/lib, then it’s likely that the bindings were not built. Go back to your CMake step and check the list of libraries to be compiled. Ensure that “python3” is in that list.

  51. Ashish June 18, 2016 at 12:31 am #

    Hey man just wanted to say you are doing awesome work providing education for free, thanks a ton.

  52. Ashish June 18, 2016 at 1:44 am #

    Hello Adrian i wanted to know what if i do not wish to work in some virtual environment but would rather work on an IDE such as pycharm , what should I do for that after i have gone through all steps mentioned here.

    • Adrian Rosebrock June 18, 2016 at 8:09 am #

      If you would like to work in PyCharm, you can simply create a new PyCharm project and update the “Project Interpreter”, just like I do in this tutorial.

  53. GK June 22, 2016 at 7:35 pm #

    I installed on xubuntu 14.04, all steps are the same. There are few modifications. After following all steps I found no file. Turned out, cmake could not find python libraries. So I had to add option
    -D PYTHON_LIBRARIES=/usr/lib/python3.4/config-3.4m-i386-linux-gnu/
    to cmake configure. But surprisingly, it still didn’t work and cmake was failing to find python 3.4 lib headers. Only after I deleted CMakeCache.txt and executed cmake command, it was able to pick up py3.4 libs.

    • Adrian Rosebrock June 23, 2016 at 1:12 pm #

      Thanks for sharing your experience!

  54. GK June 24, 2016 at 12:46 pm #

    I wanted to use OCR example and teserract to build as a part of Open cv. To do this you need to add libtesseract-dev, libtesseract-ocr and libleptonica-dev before configuring cmake.

  55. chandan dwivedi June 27, 2016 at 1:37 am #

    I am also getting cv2.__version__: ‘3.0.0-dev’ , I tried to run git checkout 3.0.0 but problem is that i have downloaded zip file from github so it shows “fatal: Not a git repository (or any of the parent directories): .git” will it cause any problem?

    • Adrian Rosebrock June 28, 2016 at 10:56 am #

      This shouldn’t cause a problem, but keep in mind that you’re on the development branch. Since you downloaded the .zip rather than the repo, that is why the git command is not working.

  56. Akashmahalik July 1, 2016 at 12:08 pm #

    for the command
    mkvirtualenv cv it says command not found but till that step everything went correct

    • Adrian Rosebrock July 1, 2016 at 2:55 pm #

      Please see the “Troubleshooting” section of this post — it details suggestions on how to resolve this problem.

      • akash mahalik July 1, 2016 at 10:37 pm #

        what does “(place at the bottom of the file”) signify ? to which file shall we add this ?

        • Adrian Rosebrock July 5, 2016 at 1:56 pm #

          Open up your ~/.bashrc file in your favorite text editor, scroll to the bottom, and append the relevant commands.

      • akash July 2, 2016 at 5:15 am #

        the above problem got solved but fot the command:
        sudo apt-get install python3.4-dev
        it says
        E: Unable to locate package python3.4-dev
        E: Couldn’t find any package by glob ‘python3.4-dev’
        E: Couldn’t find any package by regex ‘python3.4-dev’

        • Adrian Rosebrock July 5, 2016 at 1:54 pm #

          Can you check which Python version you are using I’m willing to bet you are using Python 3.5, in which case the command would be:

          $ sudo apt-get install python3.5-dev

  57. akash July 2, 2016 at 9:36 pm #

    Is there anyway i can use Opencv2 without disturbing the environment for opencv3 ?

    • Adrian Rosebrock July 5, 2016 at 1:55 pm #

      Yes — please see my reply to “Hacklavya” above.

  58. Arasch Lagies July 6, 2016 at 12:52 am #

    Hi Adrian,
    I want to run a python script using OpenCV in the virtual environment, but then have it continue running (in the background), after I log out. It seems like the script gets terminated when I log out. I tried the “&” at the end of the execution line…
    Am I missing something?
    Thanks for your help.

  59. Arasch U Lagies July 7, 2016 at 2:04 pm #

    Hi Adrian,
    Thanks, I guess I was not using the subshell right in ubuntu. This thread helped:

    • Adrian Rosebrock July 8, 2016 at 9:51 am #

      Thanks for sharing!

  60. Wiktor Drewniak July 16, 2016 at 9:20 am #

    It didn’t create python library. Found the reason. It’s required to have CMake with CPack, so from CMake 3.1.

    Best regards!

    • Yohan July 21, 2016 at 12:58 pm #

      I had the same problem, took me a while to find it. Updated CMake to 3.6.0 and it fixed the problem. Some changes to Python/OpenCV/Ubuntu might have made it necessary. Maybe the page could be updated to reflect this?

      Thanks for the tutorial!

      • Adrian Rosebrock July 22, 2016 at 11:05 am #

        Thanks for sharing Yohan, I’ll look into this.

  61. Arnoldo July 28, 2016 at 2:07 am #

    Good tutorial +10, i’d tried everything and it worked fine, sorry for my English, but is a good tutorial.

    • Adrian Rosebrock July 29, 2016 at 8:37 am #

      I’m happy to hear it worked for you Arnoldo! 🙂

  62. Rajeev Kumar Verma August 1, 2016 at 12:09 am #

    Hello Adrian,

    Thank a lot for this tutorial. Instructions were crisp and clear. I was able to install opencv with python3.4 without any issues.


    • Adrian Rosebrock August 1, 2016 at 1:59 pm #

      Congrats on getting OpenCV installed on your system, Rajeev! 🙂

  63. Mona Jalal August 5, 2016 at 10:50 am #

    Thank you so much 🙂 It worked for me on Ubuntu 16.04 64bit with Python3.5.2 and opencv checkout.

    • Adrian Rosebrock August 7, 2016 at 8:19 am #

      Congrats, good job!

    • James C August 10, 2016 at 5:30 pm #

      I just installed OpenCV 3.1.0 into an Ubuntu 16.04 virtual machine. I had a weird issue in that after I ran the make install command, the appropriate file hadn’t appeared in the /usr/local/lib site packages folder. After struggling to work out what the issue was (I checked the relevant make file, and I couldn’t see a problem), I just ran the cmake and make install commands again. After this second time, the file appeared, and I was able to follow the rest of the process fine.

      No idea what the problem was, nor really what resolved it (I don’t know much about the cmake system), but if anyone else is having this trouble, maybe they should try running the commands a second time. Hopefully this helps!

  64. amirul August 6, 2016 at 8:45 am #

    Hello sir! Thanks for your great work! Everything on your site works like charms for me but have a little inquiry. I have recently taken interest in firewire devices while working with opencv-python. From what i’ve found, libdc1394 is the one handling firewire in ubuntu and that i have to include that while building opencv from source. As I’m just started learning programming early this year and not yet familiar with cmake, so could you give me some guidance or hint? And please pardon my bad english.

    • Adrian Rosebrock August 7, 2016 at 8:15 am #

      Hey Amirul — thanks for the comment, although to be honest, I’m not sure what your specific question is?

  65. Shravan Kumar August 6, 2016 at 10:04 am #

    Hey Adrian,

    Thank you, it successfully installed on my pc. But, how coud I enter into virtual environment from the next time onwards to use workon cv.

    I have tried:

    source ~/.bashrc
    mkvirtualenv cv

    but it is throwing an error that command could not found.

    Could you please help me to fix this issue

    Thank you

    • Adrian Rosebrock August 7, 2016 at 8:14 am #

      Hey Shravan — if the mkvirtualenv command is not being found, then you’ll want to double-check your .bashrc file. It seems that it might not have been updated properly.

  66. Jeffrey August 10, 2016 at 12:22 pm #

    I have a smooth pass following your installation steps. I am super excited! Thanks for sharing and I am going to share this with my friends on fb!

    • Adrian Rosebrock August 11, 2016 at 10:39 am #

      Great job getting OpenCV installed on your system Jeffrey — and thanks for sharing the post with your friends 🙂

  67. Mona Jalal August 12, 2016 at 7:07 pm #

    Hi Adrian, what do you suggest to do?

    EDIT: the answer to my question: libtiff5-dev would work just as fine 🙂

    • Adrian Rosebrock August 14, 2016 at 9:27 am #

      Nice job resolving the issue Mona Jalal! 🙂

  68. Anupam August 18, 2016 at 12:33 am #

    Thanks for the thorough tutorial!
    I wanted the install for both python3 and python2.7. Python3 works fine now, could you please guide me how to get it working for python 2.7?

    • Adrian Rosebrock August 18, 2016 at 9:27 am #

      Please see my reply to Hacklavya above where I discuss the general procedure on how to do this.

  69. Isman siete September 7, 2016 at 6:40 am #

    I don’t know if someone has post it yet or not, but I had some problems with pyenv and cmake, instead of virtualenv.

    Some answers:

    Thank you for the amazing tutorial!

  70. Manu BN October 17, 2016 at 6:55 am #

    When I follow the instruction then in Step 4 you hv said that the file location will be /usr/local/lib/python3.4/site-packages/. but in my installation it shows python3.5/dist-packages.

    So if I change all the next steps to Python3.5 & dist-packages it dosent work

    Any hints to solve the problem plz???

    • Adrian Rosebrock October 17, 2016 at 3:59 pm #

      You can manually copy the file from dist-packages to dist-packages if you need to. I will be posting an updated tutorial covering Ubuntu 16.04 and Python 3.5 within the next 2 weeks so I would also suggest waiting for that tutorial.

  71. Kelevra Robert November 16, 2016 at 8:34 am #

    Hi, I reached the cmake step but after that when i ran the make-j4, i get fatal errors like math.h not found or stdlib.h not found, does any one have any ideas to what might be happening?

    I’m on an ubuntu 14.04 system and ran all this with the git chekout 3.0.0

    NOTE: Edited to remove terminal output that was destroying comment formatting.

    • Adrian Rosebrock November 16, 2016 at 1:35 pm #

      Hey Kelevra — try adding the following switch to your CMake command: -D ENABLE_PRECOMPILED_HEADERS=OFF

      I’m willing to be that should resolve the issue.

  72. Atilla December 2, 2016 at 5:09 am #

    Hi Adrian,

    Thank you so much for your installation description. I am almost through, gone through all the steps, but when I want to open cv2 in python it does not find the module.

    The question is, I am working with python 3.5, so have replaced that in part of the steps where it said python 3.4, else to install the devices in step 2 (part 2) was not possible *the devices are not available since I have python 3.5). Then there seem to be two problems, no /usr/local/lib/python3.5/site-packages/ file is created for python 3.5, there is only a dist-packages file. So when I eventually want to open the cv2 file, it cannot be found. Although I can open openCV and use the workon cv command in

    cd ~/.virtualenvs/cv/lib/python3.5/site-packages/

    so the change for python 3.5 is made there, but not in the usr file.

    Any suggestions on how I can solve this problem?

    Kind regards,


    Thanks a lot!

    • Adrian Rosebrock December 5, 2016 at 1:40 pm #

      Hi Atilla — if you are using Python 3.5 I would suggest using this tutorial instead.

  73. Mehta Jaldhi December 14, 2016 at 1:40 am #

    Hey i have a query regarding this..

    See I have installed this opencv and python in my local user and it is working correctly..

    My question is “What if I want to make it work in hduser? How can I set a path in that user?”

    • Adrian Rosebrock December 14, 2016 at 8:24 am #

      If you have a separate user you can share a virtual environment. This isn’t recommended, but it can be done. Otherwise, I would suggest creating a separate virtual environment for your other user and sym-linking in the bindings.

      • Mehta Jaldhi December 16, 2016 at 2:06 am #

        Okay but i tried to installed in another user but the error is saying that you have already installed it.

        Now how i need to uninstall the opencv from my local user?

        • Adrian Rosebrock December 18, 2016 at 8:47 am #

          Python, the associated virtual environments, and OpenCV should already be installed on your machine (if you successfully followed this tutorial). To create a virtual environment for your new user, you would have to:

          1. Switch to that user.
          2. Ensure their .bashrc file is updated correctly.
          3. Create a new virtual environment.
          4. Install NumPy.
          5. Sym-link in the OpenCV bindings.

          Again, another option would be to use the already existing virtual environment for user A for user B as well (this might involve changing permissions though, I’ve never directly tried this).

  74. Dominik December 20, 2016 at 7:46 am #

    Hello Adrian!
    Thanks a lot for this Tutorial!
    But i’ve got a problem.
    In the shell after workon cv etc.. all works fine like in this tutorial.
    But if i set the Python SDK on usr/bin/python3.4 I`m not able to do “import cv2”.
    It works fine in shell but my IDE (IntelliJ -Python Plugin) marks it red with “no module named cv2”.
    Can u help?


    • Adrian Rosebrock December 21, 2016 at 10:25 am #

      I’m not an IntelliJ user, but it sounds like you need to set the Python interpreter for your project to be the virtual environment. I demonstrate how to do this with PyCharm here. I imagine a similar solution exists for IntelliJ.

  75. Saul Spatz January 7, 2017 at 12:52 pm #

    Hi Adrian,

    I followed your instructions, and successfully installed open CV 3.2.0 on Yosemite 10.10.5. However, I then learned that I need ffmpeg support, so I installed ffmpeg with Homebrew, and then tried to rebuild openCV, but it failed because of undefined symbols in freetype. I then tried to build openCV again without ffmpeg, but it failed with the same errors [This gist] ( shows the cmake command and the error messages. Perhaps I should note that I always ran make clean between build attempts.

    I ran nm freetype.cpp.o and those symbols are undefined all right, but I was able to install openCV 3.2.0 on Ubuntu 16.04 (with ffmpeg) by following your instructions, and I see that the same symbols are undefined in freetype.cpp.o so the linker must be finding them in another file.

    Before installing ffmpeg, I ran brew upgrade and I noticed that it upgraded freetype, so this may be the source of the problem, but I have have no clue what the problem is. Unfortunately, I don’t have much knowledge of makefiles this complex, so I don’t know what to look at next. How can I get more verbose output from the make process? I’d like to see what the ld command is. I’m guessing that when Homebrew upgraded freetype, it downloaded a prebuilt binary instead of building from source, so that some object file needed for the build is missing. If that’s the case, I still wouldn’t know what to do about it, but at least, I’d know what to google.

    I really need openCV on the Mac. The Ubuntu installation is just a temporary stopgap. I will appreciate any advice you can give me.

    P.S. I hope I’ve addressed the formatting problems this time. It would help klutzes like me if the site displayed previews of replies, but I don’t know how to accomplish that.

    • Adrian Rosebrock January 9, 2017 at 9:18 am #

      Hey Saul — unfortunately without direct access to your machine I’m not sure what the exact error may be. I personally don’t use FFMPEG so I’m a bit unfamiliar with the intricacies it may introduce to the FFMPEG compile. While less than ideal, I would suggest re-installing Yosemite and then following the instructions from the very beginning. This will ensure you have a fresh machine to work on with no previous libraries causing problems.

  76. nicoteiz January 26, 2017 at 9:23 am #

    Hi Adrian! Can I use the same installation of openCV 3.1 for both python2 and python3? Y am using it with no problem, after installing the 3.1 version of opencv using your tutorial for python 2.7.

    I tried just doing the last steps of symlinking to the other installation, but it seems not to work.

    Do I have to do the whole installation again to be able to use it with python3?

    • Adrian Rosebrock January 28, 2017 at 7:01 am #

      No, Python 2.7 and Python 3 are separate Python versions and therefore have separate interpreters and libraries. You will need to compile OpenCV twice — once for Python 2.7 and then for Python 3, taking care to copy the file to the respective site-packages directory for your Python versions.

  77. Kati February 16, 2017 at 6:22 pm #

    Hi Adrian! I followed your instruction but I had a problem at line:
    ~$ sudo python3
    because it gives me:
    sudo: python3: command not found

    I’m using Ubuntu 12.04 LTS with python 2.7.3. Do I have to install python3.4 before starting to install OpenCv?

    • Adrian Rosebrock February 20, 2017 at 8:03 am #

      This tutorial is intended for Ubuntu 14.04, so if you’re using a prior version of Ubuntu you will need to install Python 3 on your system.

  78. Kati February 17, 2017 at 10:48 am #

    Hi Adrian! I solved the problem found yesterday by installing Ubuntu 14.04.
    Now I have followed your great tutorial and i reached the end! 🙂
    But I have a little problem… when I import cv2, it gives me:

    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named ‘cv2’

    Even another guy had a similar error (Chetan on July 31, 2015) but I followed your suggestion to him and I can’t resolve the problem.

    But I try to look in the directory, as you’ve done, with
    ~/.virtualenvs/cv/lib/python3.4/site-packages ls
    and, while for you is sky blue, for me it is red!
    Why? Could it be this the problem?
    Thanks a lot!

    • Adrian Rosebrock February 20, 2017 at 8:01 am #

      If the file is red, then your sym-link is pointing to a file that doesn’t exist. Locate your file on disk, then re-create the sym-link so that it points to the valid file path.

      • Abdul Rahman Dabbour April 25, 2017 at 5:45 pm #

        Hello! I encountered the same problem, then I found myself to have made the mistake of not running this step earlier:

        sudo make install
        in the ~/opencv/build folder

        I hope this helps!

        BTW, thank you very much Mr. Rosebrock!

  79. Joe March 2, 2017 at 6:39 am #

    Thank you very much for this extensive tutorial. Works perfectly fine with current versions. OpenCV 3.2.0 installed fine with Python 3.5 on Ubuntu 16.04.2, I just modified the version numbers from your tutorial. Also I didn’t set -D INSTALL_C_EXAMPLES=OFF in step three and the compilation worked flawlessly. The bug in OpenCV 3.1.0 seems to have been fixed in 3.2.0.


    • Adrian Rosebrock March 2, 2017 at 7:02 am #

      Thanks for sharing Joe! And congrats on getting OpenCV installed!

  80. sheikha March 14, 2017 at 11:21 am #

    I already installed opencv , but now only i realised that it only works in python 2.7 . When i run it in python 3 it shows error that ” no module named cv2″ but i need it in python3 . So should i follow ur steps without uninstalling the already existing opencv ? please help

    • Adrian Rosebrock March 15, 2017 at 8:52 am #

      You don’t need to uninstall OpenCV, simply re-run CMake from within a Python 3 virtual environment. Re-compile. And then re-install. From there you’ll be good to go.

  81. Claudio March 16, 2017 at 4:48 pm #

    Hi! Thanks for everything! Little question, I’ve got the same problem as sheikha, but my virtual environment doesn’t work, how can I open the “~/.bashrc” file to change it like indicated in step 1?


    • Adrian Rosebrock March 17, 2017 at 9:24 am #

      You can use your favorite text editor to edit ~/.bashrc. For beginners, I would suggest using nano:

      $ nano ~/.bashrc

  82. Vanshergzie March 20, 2017 at 3:30 am #

    I’ve been having much problems on installation and compilation of opencv and python3 opencv on my system. But with this tutorial, I found it much easier. Danke

    • Adrian Rosebrock March 21, 2017 at 7:23 am #

      Congrats on getting OpenCV installed Vanshergzie, nice job!

  83. Faakhir Inam April 3, 2017 at 2:58 am #


    I am running into a little problem when I run the following code,
    $ make -j4

    I get a fatal error: stdlib.h: No such file or directory

    After this the program terminates. Can you please help me out?

    • Adrian Rosebrock April 3, 2017 at 1:54 pm #

      I would make sure you have installed the “build-essentials” package. Don’t forget this line at the top of the tutorial:

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

  84. Milan April 5, 2017 at 11:13 pm #

    Thank you so much . Finally I am able to set up openCV . Finally up and running 🙂

    • Adrian Rosebrock April 8, 2017 at 1:01 pm #

      Nice job Milan, congrats!

  85. Omer Faruk BULBUL April 6, 2017 at 4:25 pm #

    Thanks a lot. Saved a lot of time

    • Adrian Rosebrock April 8, 2017 at 12:53 pm #

      Congrats on getting OpenCV installed Omer.

  86. Rebecca April 7, 2017 at 7:27 pm #

    Hi Adrian,

    Fantastic tutorial, thank you so much! I am only stuck on the last bit, and I think it is because I have previously installed Anaconda on my computer. When running in python import cv2 I get the following response:

    ImportError: /home/una/anaconda3/lib/ version `GLIBCXX_3.4.21′ not found (required by /home/una/.virtualenvs/cv/lib/python3.5/site-packages/

    Can you help me fix this?

    Thanks again,


    • Adrian Rosebrock April 8, 2017 at 12:40 pm #

      Hi Rebecca — it sounds like OpenCV might have been compiled against a different OpenCV version than the one you are trying to import it into. Double-check your CMake paths or try on a fresh install without Anaconda.

  87. Claudio F May 21, 2017 at 9:17 am #

    Hi Adrian. I followed your tutorial with python3.6.1 that has been installed from source just before. Every step of your tutorial worked, except the one where python3.6-dev should be installed. I get an error saying:

    E: Unable to locate package python3.6-dev
    E: Couldn’t find any package by regex ‘python3.6-dev’

    do you have any idea what the problem is? with python 3.4 everything workes just fine…

    thanks for your help

    • Adrian Rosebrock May 25, 2017 at 4:46 am #

      Try updating your apt-get packages:

      $ sudo apt-get update

      And then continuing.

  88. Andrei June 2, 2017 at 7:55 am #

    Awesome instructions. Thanks!

    Just in case someone has similar issues like I had …

    I had issues with building OpenCV for Python because of my Anaconda install. Anaconda installation appended another Python source to the $PATH environmental variable. Because of that I was not getting the output you’ve shown in Figure 3, as the required packages were not found. After I removed the culprit from the $PATH variable, could follow the rest of the steps successfully.

    • Adrian Rosebrock June 2, 2017 at 1:58 pm #

      Thank you for sharing Andrei!

  89. Umair August 2, 2017 at 1:11 am #

    Sir provide me the source code in opencv python for fingerprint recognition ,,,,, i have lots of problems ,,,, i will be really thankfull to u for this kind act

  90. Vladislav Rishe August 2, 2017 at 8:12 pm #

    For those, who are bumping into:

    ImportError: cannot enable executable stack as shared object requires: Invalid argument

    under Bash on Ubuntu on Windows when it’s to make import cv2, I would recommend doing the following:

    $ sudo apt-get execstack
    $ sudo execstack -c /usr/local/lib/libopencv_*

    After doing this it should get back to normal. At least I was able to perform some basic operations with Open CV.

    Thanks for such a detailed tutorial, Adrian!

    • Vladislav Rishe August 2, 2017 at 8:14 pm #

      One small correction: $ sudo apt-get install execstack, sure.

    • Adrian Rosebrock August 4, 2017 at 6:57 am #

      Thank you for sharing, Vladislav!

  91. Keags August 17, 2017 at 7:41 am #

    Thanks for sharing!

    During installation, i came across issues with missing OpenEXR header files. If anyone else faces this issue please take a look in /usr/include/OpenEXR and move the missing headers to /usr/local/include/OpenEXR.

    • Adrian Rosebrock August 17, 2017 at 9:02 am #

      Thanks for sharing, Keags!

Leave a Reply