Install OpenCV 3.0 and Python 2.7+ on Ubuntu


Last week we kicked-off the OpenCV 3.0 install fest by detailing how to install OpenCV 3.0 and Python 2.7+ on the OSX platform.

Today we are going to continue the OpenCV 3.0 install instruction series by moving over to the Ubuntu operating system.

In the remainder of the post I will provide instructions on how to configure and install OpenCV 3.0 and Python 2.7+ on Ubuntu. I have personally tested these instructions on Ubuntu 14.04, but they should pretty much work on any Debian-based operating system.

A quick note before we get started: Yes, OpenCV 3.0 is indeed compatible with Python 3+. However, the install instructions are slightly different between Python 2.7+ and Python 3+. In an effort to keep each article self-contained and easy to follow, I am creating separate OpenCV 3.0 install tutorials for Python 2.7 and Python 3+. If you would like to use OpenCV 3.0 and Python 3+ on your Ubuntu system, please keep an eye on this blog — I will be posting OpenCV 3.0 and Python 3+ install instructions later this month. But for the time being, let’s stick with Python 2.7.

How to Install OpenCV 3.0 and Python 2.7+ on Ubuntu

UPDATE: The tutorial you are reading now covers how to install OpenCV 3.0 with Python 2.7+ 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 2.7 (or Python 3.5) bindings, please use this freshly updated tutorial:

This is the second article in the OpenCV 3.0 install-fest series. Last week we covered how to install OpenCV 3.0 and Python 2.7+ on OSX. Today we are going to perform the same OpenCV 3.0 and Python 2.7 installation, only on the Ubuntu operating system. In general, you should find installing OpenCV 3.0 and Python 2.7+ on Ubuntu much easier than installing on OSX.

Step 1:

Open up a terminal and update the apt-get  package manager followed by upgrading any pre-installed packages:

Step 2:

Now we need to install our developer tools:

The pkg-config  is likely already installed, but be sure to include it just in case. We’ll be using git  to pull down the OpenCV repositories from GitHub. The  cmake  package is used to configure our build.

Step 3:

OpenCV needs to be able to load various image file formats from disk, including JPEG, PNG, TIFF, etc. In order to load these image formats from disk, we’ll need our image I/O packages:

Step 4:

At this point, we have the ability to load a given image off of disk. But how do we display the actual image to our screen? The answer is the GTK development library, which the highgui  module of OpenCV depends on to guild Graphical User Interfaces (GUIs):

Step 5:

We can load images using OpenCV, but what about processing video streams and accessing individual frames? We’ve got that covered here:

Step 6:

Install libraries that are used to optimize various routines inside of OpenCV:

Step 7:

Install pip , a Python package manager:

Step 8:

Install virtualenv and virtualenvwrapper. These two packages allow us to create separate Python environments for each project we are working on. While installing virtualenv  and virtualenvwrapper  is not a requirement to get OpenCV 3.0 and Python 2.7+ up and running on your Ubuntu system, I highly recommend it and the rest of this tutorial will assume you have them installed!

Now that we have virtualenv  and virtualenvwrapper  installed, we need to update our ~/.bashrc  file:

This quick update will ensure that both virtualenv  and virtualenvwrapper  are loaded each time you login.

To make the changes to our ~/.bashrc  file take effect, you can either (1) logout and log back in, (2) close your current terminal window and open a new one, or preferably, (3) reload the contents of your ~/.bashrc  file:

Lastly, we can create our cv  virtual environment where we’ll be doing our computer vision development and OpenCV 3.0 + Python 2.7+ installation:

Step 9:

As I mentioned above, this tutorial covers how to install OpenCV 3.0 and Python 2.7+ (I’ll have a OpenCV 3.0 + Python 3 tutorial available later this month), so we’ll need to install our Python 2.7 development tools:

Since OpenCV represents images as multi-dimensional NumPy arrays, we better install NumPy into our cv  virtual environment. We also install imutils — my package of convenience functions:

Step 10:

Our environment is now all setup — we can proceed to change to our home directory, 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.

As I mentioned last week, we also need the opencv_contrib repo as well. Without this repository, we won’t have access to standard keypoint detectors and local invariant descriptors (such as SIFT, SURF, etc.) that were available in the OpenCV 2.4.X version. We’ll also be missing out on some of the newer OpenCV 3.0 features like text detection in natural images:

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.

Notice how compared to last week our CMake command is substantially less verbose and requires less manual tweaking — this is because CMake is able to better automatically tune our install parameters (at least compared to OSX).

Now we can finally compile OpenCV:

Where you can replace the 4 with the number of available cores on your processor to speedup the compilation.

Here’s an example of OpenCV 3.0 compiling on my system:

Figure 1: OpenCV 3.0 with Python 2.7+ support compiling on my Ubuntu 14.04 system.

Figure 1: OpenCV 3.0 with Python 2.7+ support compiling on my Ubuntu 14.04 system.

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

Step 11:

If you’ve reached this step without an error, OpenCV should now be installed in  /usr/local/lib/python2.7/site-packages

However, our cv  virtual environment is located in our home directory — thus to use OpenCV within our cv  environment, we first need to sym-link OpenCV into the site-packages  directory of the cv  virtual environment:

Step 12:

Congratulations! You have successfully installed OpenCV 3.0 with Python 2.7+ bindings on your Ubuntu system!

To confirm your installation, simply ensure that you are in the cv  virtual environment, followed by importing cv2 :

Here’s an example of demonstrating the OpenCV 3.0 and Python 2.7+ install on my own Ubuntu machine:

Figure 2: OpenCV 3.0 with Python 2.7+ bindings has been successfully installed on Ubuntu!

Figure 2: OpenCV 3.0 with Python 2.7+ bindings has been successfully installed on Ubuntu!

Step 13:

Now that OpenCV has been configured and installed, let’s build a quick Python script to detect the red game cartridge in the image named games.jpg  below:

Figure 2: Our goal is to detect the red game cartridge (on the right) in this image.

Figure 3: Our goal is to detect the red game cartridge (on the right) in this image.

Open up your favorite editor, create a new file, name it , and insert the following code:

You’ll also need to download the games.jpg image and place it in the same directory as your  file. Once the games.jpg  file has been downloaded, you can execute the script via:

Assuming that you have downloaded the games.jpg  image and placed it in the same directory as our  script, you should see the following output:

Figure 4: We have successfully detected the red game cartridge in the image!

Figure 4: We have successfully detected the red game cartridge in the image!

Notice how our script was able to successfully detect the red game cartridge in the right portion of the image, followed by drawing a green bounding box surrounding it.

Obviously this isn’t the most exciting example in the world — but it has demonstrated that we have OpenCV 3.0 with Python 2.7+ bindings up and running on our Ubuntu system!

So, what’s next?

Congrats! You have a brand new, fresh install of OpenCV on your Ubuntu system — and I’m sure you’re just itching to leverage your new install to build some awesome computer vision apps.

But I’m also willing to bet that you’re just getting started learning computer vision and OpenCV, and you’re probably feeling a bit confused and overwhelmed on where exactly to start.

Personally, I’m a big fan of learning by example, so a good first step would be to read this blog post on accessing your webcam with OpenCV. This tutorial details the exact steps you need to take to (1) capture photos from your webcam and (2) access the raw video stream.

And if you’re really interested in leveling-up your computer vision skills, you should definitely check out my book, Practical Python and OpenCV + Case Studies. My book not only covers the basics of computer vision and image processing, but also teaches you how to solve real world computer vision problems including face detection in images and video streams, object tracking in video, and handwriting recognition.


So let’s put your fresh install of OpenCV on your Ubuntu system to good use — just click here to learn more about the real-world projects you can solve using your new OpenCV install + Practical Python and OpenCV .


To celebrate the OpenCV 3.0 release, I am working my way through OpenCV 3.0 and Python 2.7/Python 3.4 installation instructions on OSXUbuntu, and the Raspberry Pi.

Last week I covered how to install OpenCV 3.0 and Python 2.7+ on OSX.

And today we covered how to install OpenCV 3.0 with Python 2.7 bindings on Ubuntu. I have personally tested these instructions on my own Ubuntu 14.04 machine, but they should work on any Debian-based system.

Next week we’ll continue the install-fest and hop back to OSX — this time installing OpenCV 3.0 and Python 3!

This will be the first time we’ve used Python 3 on the PyImageSearch blog, so you won’t want to miss it!

And please consider subscribing to the PyImageSearch Newsletter by entering your email address in the form below. As we work through the OpenCV install-fest, I’ll be sending out updates as each new OpenCV 3.0 + Python install tutorial is released!

, , ,

376 Responses to Install OpenCV 3.0 and Python 2.7+ on Ubuntu

  1. Mitch Berkson June 23, 2015 at 3:59 pm #

    So where is games.jpg?

    • Adrian Rosebrock June 23, 2015 at 6:34 pm #

      Hey Mitch, I saw your other comment, but for further clarification I’ll add the games.jpg link here to help other readers out:

    • Albert September 24, 2015 at 6:31 am #

      Right click -> Save image as …

      • Adrian Rosebrock September 24, 2015 at 6:57 am #

        I’ll also add that the code + images is available in the source code download of the post.

  2. Mitch Berkson June 23, 2015 at 4:04 pm #

    Doh. Can just download games.jpg from the browser.

  3. Frank Frederic June 23, 2015 at 10:01 pm #

    Thank you Adrian for the guide.
    Are you able to install the combination OpenCV 3 + Python 3 on any platform?
    I’m long for this.

    • Adrian Rosebrock June 24, 2015 at 6:10 am #

      I’ve personally installed OpenCV 3.0 + Python 3 on OSX, Linux (Debian based systems), and the Raspberry Pi. I do not have a Windows system, otherwise I would probably give that a shot as well.

      • Red May 29, 2016 at 10:11 am #

        I’m constantly struggling with “Missing PythonLibraries” for both the 2.7 and 3.x using an Xubuntu 14.04 LTS. Development packages for both Python versions are installed. Funny thing is that every second time I run cmake .. in the build directory of OpenCV 3’s source directory I get that the libraries for Python 2.7 are found. When I get that I’m also able to build the Python libs for 2.7. For Python 3.x not the same luck sadly. After two months still unable to compile the damn libs hence I’m unable to properly use OpenCV 3 in Python 3.

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

          I’m sure this is a dumb question on my part, but after you looked at my OpenCV 3 + Python 3 installation tutorial?

  4. shixudongleo June 23, 2015 at 11:15 pm #

    Should I set the OPENCV_EXTRA_MODULES_PATH explicitly so that cmake can include the opencv_contrib modules?

    Or the cmake can automatically locate the opencv_contrib directory because opencv and opencv_contrib directories are download side-by-side.

    • Adrian Rosebrock June 24, 2015 at 6:12 am #

      You need to explicitly set the OPENCV_EXTRA_MODULES_PATH like this: -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules

      Also, just realized that I didn’t mention that in the original post. I have updated it, thanks for pointing it out Xudong!

  5. Sébastien Vincent June 24, 2015 at 4:33 pm #

    It seems to me that there’s a small mistake in the build setup instruction. The two dots should not be before the OPENCV_EXTRA_MODULES_PATH option but at the very end:
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules .. <== there!

    • Adrian Rosebrock June 25, 2015 at 6:21 am #

      Thanks for pointing this out Sébastien! I have fixed the error 🙂

      • afshan July 5, 2017 at 9:17 am #

        i can’t enter backslash from my keyboard. please help me fix this problem.

      • Anne November 17, 2017 at 11:32 am #

        Dear Adrian,

        At the ending of Step #10, is there a chance of getting errors if you type the command ‘make install’ instead of ‘sudo make install.’? Is there a way of doing it without sudo?

        Thanks! 🙂

        • Adrian Rosebrock November 18, 2017 at 8:11 am #

          Unless you set the install directory to something other than /usr/local you will need sudo permissions to install.

  6. Chris Viehoff June 25, 2015 at 1:10 am #

    I did not get any error but in step11 checking for the files in /usr/local/lib/python2.7/site-packages …there were no files created.

    • Adrian Rosebrock June 25, 2015 at 6:26 am #

      Take a look at your output from cmake, like in this screenshot and look for the packages path. This will tell you where the generated file will be stored. It’s possible that CMake had generated a different packages path on your system.

      • Refik Can June 25, 2015 at 9:33 am #

        I also did not find file under /usr/local/lib/python2.7/site-packages and my cmake build python options are same with your screenshot.

        • Adrian Rosebrock June 25, 2015 at 12:42 pm #

          Hey Refik, just to confirm — you ran $ make install after compiling, correct? If so, can you check in your /usr/local/lib/python2.7/dist-packages directory to see if it ended up there?

          Finally, check in your build/lib directory as well. You should find the file there after a successful compilation. The make install command just copies the binary to the packages path.

          • Evan Zamir January 18, 2016 at 6:41 pm #

            I have a similar issue installing on a clean (Docker) version of Ubuntu 14.04. The only instructions I didn’t follow were installing the virtualenv (which doesn’t make much sense since I’m using Docker anyway).

            I can’t find anywhere, not in /lib/build and not in site-packages or dist-packages. Any idea what might have gone wrong? No errors encountered along the way. Seems odd.

          • Adrian Rosebrock January 20, 2016 at 1:55 pm #

            After the compile did you run sudo make install? Also be sure to check the lib sub-directory of build.

            You might also want to try searching the entire Pi for the file as well:

            $ find / -name ""

      • Max Gordon September 8, 2015 at 7:48 am #

        Thanks for an excellent tutorial! I’m having a samilar problem on Ubuntu 14.04. I’ve solved it by doing a “sudo cp lib/ /usr/local/lib/python2.7/dist-packages/” – it does feel like a hack and it would be nice to understand why this issue appears.

        It seems that cmake doesn’t find the python2.7 libraries. I think the problem in my case is that it fails to find the python libraries, I get the:

        Could NOT find PythonLibs (missing: PYTHON_INCLUDE_DIRS) (found suitable exact version “2.7.6”)

        The part corresponding to you screenshot is just “Libraries: No” for the Python 2 section. I’ve also noticed that instead of site-packages it uses the dist-packages. From googling I’ve found a few hints (the best one is probably but I’m not familiar enough with cmake to start changing the script and the python-dev is alreade installed.

        • Adrian Rosebrock September 9, 2015 at 6:48 am #

          It might “feel” like a hack, but in reality that’s all the sudo make install command does for the Python bindings. It just copies the file into the specified location.

          • Vincent January 24, 2016 at 12:27 pm #

            Thanks a lot Adrian! Everything worked great for me. I could even find the red cartridge.

            As I located the in the opencv/build/lib directory, I copied it in the dist-package with ” sudo cp lib/ /usr/local/lib/python2.7/dist-packages/ “, as Max said.

            Thanks again for all this, guys.

        • March 6, 2016 at 11:30 am #

          I have the same problem, and upgrading cmake 2.8->3.2 helps (

          sudo apt-get install software-properties-common
          sudo add-apt-repository ppa:george-edison55/cmake-3.x
          sudo apt-get update
          sudo apt-get install –only-upgrade cmake

          for me was also crucial to install

          sudo apt-get install qt5-default libvtk6-dev

          • Red May 29, 2016 at 10:56 am #

            Kudos for the tip! I had problems for both Python 2.7 and Python 3.4 but after upgrading cmake everything was found and configured appropriately. Thanks a lot!

          • hjjoo July 24, 2016 at 9:57 pm #

            Thank you!!! so!! much!!!!

          • John August 12, 2016 at 7:41 pm #

            Thanks, sorted also! Had to remember to delete CMakeCache.txt in the build folder before running CMake again.

  7. Chris Viehoff June 25, 2015 at 1:15 am #

    when I run

    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules

    the last command does not get executed and remains on the command line by itself:

    (cv)chris@theLab:~/opencv/build$ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules

    • Adrian Rosebrock June 25, 2015 at 6:21 am #

      Hi Chris, thanks for pointing this out, I was missing an extra “\” after the -D BUILD_EXAMPLES=ON .. part. I have updated the post and added in the extra slash, please go back up and try copying and pasting the command again.

  8. Caio Iglesias June 25, 2015 at 3:08 am #

    I installed opencv3 directly to the virtual enviroment so I can keep opencv3 on “cv” and leave opencv2 on the “gurus” enviroment as we move through the pyimagesearch gurus course. Just wanted to share the code here if someone ends up needing it.

    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    -D PYTHON_PACKAGES_PATH=$VIRTUAL_ENV/lib/python2.7/site-packages \

    Just remember to delete CMakeCache.txt if you’ve ran cmake before and to run “workon cv” before running this cmake code.

    • Adrian Rosebrock June 25, 2015 at 6:14 am #

      Very nice, thanks for sharing Caio!

    • Phil Smith September 9, 2015 at 4:29 pm #

      Thanks you – this worked perfectly on Ubuntu 14.04.

    • Manuel June 16, 2016 at 5:35 pm #

      Thank you very much, I did what you suggested and it worked.

  9. Tim Clemans June 28, 2015 at 9:57 am #

    Would be nice to have a single script version of this tutorial that one can wget and just run.

    • Adrian Rosebrock June 28, 2015 at 2:35 pm #

      Hey Tim, that’s a great suggestion. I’ll look into putting something like this together. The biggest challenge is that everyones’ system configuration is slightly different and that can lead to system/package paths being different. And if those system/packages paths are off, then you’ll get compile or install errors. I’ll look into the feasibility of this though.

  10. Sébastien Vincent June 29, 2015 at 4:12 pm #

    Hello Adrian,
    I’m not sure if it’s my install, but I performed all the steps, and it looks like cv2 can’t find the reference to the xfeatures2d package… Could you check if it’s ok on your PC? Here is what I got:

    (cv)seb@seb-VirtualBox:~/tst-opencv$ python
    Python 2.7.6 (default, Jun 22 2015, 17:58:13)
    [GCC 4.8.2] on linux2
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import cv2
    >>> cv2.__version__
    >>> detector = cv2.xfeatures2d.SIFT_create()
    Traceback (most recent call last):
    File “”, line 1, in
    AttributeError: ‘module’ object has no attribute ‘xfeatures2d’

    • Adrian Rosebrock June 29, 2015 at 4:22 pm #

      Hey Sébastien, I’m actually trying to figure out this problem myself. I’ve installed OpenCV 3 with Python 3+ bindings and the xfeatures2d module is installed. However, if I perform the same process of installing OpenCV 3 with Python 2.7 bindings, then the xfeatures2d bindings are not not installed. I think it might be a bug with OpenCV as I’ve gone through the process a bunch of times, each time ensuring that the opencv_contrib package was included in the compile, but each time it cannot import xfeatures2d

      Definitely repeat the process with OpenCV 3 and Python 3 and let me know if this resolves the issue. If so, then we’ll know it’s a bug with either the cmake script/OpenCV.

      • AlexM December 24, 2015 at 11:34 am #

        Thank you Adrian! I’ve done everything exactly as it was described here ( Ubuntu 14.04, Python 2.7), and I even have libopencv_xfeatures2d installed in site-packages, but still I can’t work with xfeatures2d 🙁

        didn’t show, that xfeatures2d exist ( despite the fact, that .so object is installed properly, as I said before)

        Could you please hint me?

        • Adrian Rosebrock December 24, 2015 at 12:09 pm #

          If you’re not seeing xfeatures2d, then go back to the CMake step and ensure that xfeatures2d is listed in the “modules to be built” list. If it’s not, then it’s likely that your path to the opencv_contrib/modules directory is incorrect.

  11. Sébastien Vincent June 29, 2015 at 4:49 pm #

    Thanks for the quick answer. I’ll try with Python3.4. Coming back to Linux after ~15 years does not help, as I’m not sure if errors come from my mistakes or from something else!

    • Sébastien Vincent July 6, 2015 at 6:07 pm #

      Hello Adrian,
      Up to now, I haven’t been able to install OpenCV3 with Python3 (in a virtualenv) on my Ubuntu install. I think I’ll have to wait for your tutorial. I guess something is wrong with my cmake options.

      • Adrian Rosebrock July 6, 2015 at 6:09 pm #

        Hey Sébastien — I was planning on having the OpenCV 3 + Python 3 tutorial online today, but the bat-country post ended up taking priority. I’ll try to have it online by next Monday.

  12. Vanderson July 3, 2015 at 12:37 am #

    Thanks a lot man! God bless you.

    • Adrian Rosebrock July 3, 2015 at 6:18 am #

      No problem, I’m glad the tutorial worked for you!

  13. Larry Low July 3, 2015 at 8:04 pm #

    Thanks Adrian! This works on Linux Mint 17.

    • Adrian Rosebrock July 4, 2015 at 7:38 am #

      Awesome, glad to hear it Larry!

  14. Nick July 8, 2015 at 4:31 pm #

    Works like a charm, did not even get one error message. Your page really do look very interesting. Now I have so many ideas about what to extract.


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

      Fantastsic — I’m glad it worked for you Nick!

  15. tej kamal August 8, 2015 at 6:49 am #

    i am working with ubuntu 14.04. i am getting an error at step11. it is stating that

    (bash: cd: /home/kolukuluri/.virtualenvs/cv/lib/python2.7/site-pakages/: No such file or directory)

    and in the next step while import cv2 file from opencv to python it is showing an error

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

    please guide me a way to overcome this error

    • Adrian Rosebrock August 9, 2015 at 7:06 am #

      If you are getting an error that the site-packages directory does not exist for the cv virtual environment, then I would go back to Step 8 and install virtualenv, virtualenvwrapper, and ensure that you have created the cv virtual environment — that is a critical step in getting this tutorial to work.

  16. Sulav August 8, 2015 at 1:39 pm #


    When I import the cv2 module from python interpreter it is imported, but when I do the same in python file, it says “importError : No cv2 module” ?
    Can you help on this?

    Thank you.

    • Adrian Rosebrock August 9, 2015 at 7:08 am #

      There are many reasons why the cv2 module could fail to import. The most common reasons are that your virtual environment was not setup correctly, the OpenCV compile failed, or the OpenCV bindings were not sym-linked into the site-packages directory of the virtual environment.

  17. Grant August 11, 2015 at 6:36 pm #

    I got through most of this tutorial fine, but I’m having a problem on the make step. I get about 40% compiled before getting this error.

    make[2]: *** No rule to make target ‘/usr/include/../lib/libavcodec.a’, needed by ‘lib/’. Stop.
    make[2]: *** Waiting for unfinished jobs….
    [ 42%] Building CXX object modules/videoio/CMakeFiles/opencv_videoio.dir/src/cap_ffmpeg.cpp.o
    CMakeFiles/Makefile2:5408: recipe for target ‘modules/videoio/CMakeFiles/opencv_videoio.dir/all’ failed
    make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
    make[1]: *** Waiting for unfinished jobs….

    It’s having problems making the libavcodec, and I’ve tried several things, including install ffmpeg and other dev packages, but none of them seem to fix the missing rule. I’ve also re-run the cmake command to see if it makes any difference, but no dice. When going through the tutorial, I did notice that libtiff4-dev is no longer available and has been replaced with libtiff5-dev. I’ve seen some boards suggesting one of the other packages removed features that libavcodec depends on, but I haven’t had any luck fixing it. Any suggestions for how to modify the make command or other packages to install to get over this hump?

    • Adrian Rosebrock August 11, 2015 at 6:47 pm #

      Based on the compile error, it seems like FFMPEG is causing some problems. I would suggest running ccmake .. (you might have to install the ccmake terminal GUI) and going through the options. Look for an option that looks like WITH_FFMPEG or something similar and make sure it’s set to OFF. Then do a make clean and recompile.

      • Grant May 26, 2016 at 6:01 pm #

        Back again almost 10 months later. I recently got back into the idea of using OpenCV, after being unsuccessful building it back in August. I’ve been able to build OpenCV 3.1.0 and use it with FFMPEG turned off, but I’m trying to do the 10-day crash course now, which requires video features. The issue is the same one as before, and despite three solid days of trying different solutions, the problem is still libavcodec.a not having a rule.

        I’ve posted the problem to the OpenCV board, but at this point, my options are to hope someone gives an answer that apparently doesn’t exist anywhere on the internet, or I find a way to compile OpenCV’s video features without FFMPEG.

        • Adrian Rosebrock May 27, 2016 at 1:30 pm #

          Hey Grant — just to confirm, did you try adding the -D WITH-FFMPEG=OFF to your CMake command?

  18. Luke August 11, 2015 at 10:22 pm #

    I tried these instructions with Anaconda. Initially wasn’t generated, but I changed the cmake command as specified here and then it generated correctly:

    then I followed the rest of this tutorial, and finally used the command
    find / -name “”
    to locate the file.

  19. Joey August 13, 2015 at 1:04 pm #


    I work on Ubuntu 14.04.3. After I followed your tutorial I got no error until step 12. When I imported cv2 it gives me:
    >>> import cv2
    Traceback (most recent call last):
    File “”, line 1, in
    ImportError: No module named cv2

    that error. Can you guess what should I do to make it work?

    • Adrian Rosebrock August 14, 2015 at 7:19 am #

      Hey Joey — unfortunately, there are a number of reasons why importing OpenCV could fail. The main reasons are not being in the cv virtual environment before compiling, not being in the virtual environment when trying to import OpenCV, and not setting up the proper sym-links into the environment. My suggestion is to go back to Step 11 and see where the file is stored, it seems likely that the sym-link is incorrect.

  20. Andrey August 14, 2015 at 9:44 am #

    Hello Adrian,

    I wanted to update opencv version from 2.4.9 to 3.0.0 so I find your guidelines very useful, however I’ve stacked to some problem.

    I’ve followed your instructions, except for step 8 with virtualenv.
    Everything goes fine until step 11 – I am unable to find

    I mean, there is which belongs to older version of CV.
    Bth I’m using python 2.7 under anaconda.

    • Adrian Rosebrock August 15, 2015 at 6:29 am #

      I’m not a fan of anaconda so I don’t have any personal experience trying to install using it. Be sure to check the lib directory of your build.

  21. Bulat Suleymanov August 17, 2015 at 4:50 pm #

    Hello Adrian!

    After installing Python 3.4 + OpenCV 3.0 on Ubuntu using virtual environment, I tried to create another one with Python 2.7 according to your tutorial. When I create new virtual environment it already consists lib/Python3.4. What should I do?

    • Adrian Rosebrock August 18, 2015 at 6:43 am #

      Hey Bulat, you’ll need to specify your Python version when using the mkvirtualenv command:

      $ mkvirtualenv foo -p python2.7

  22. Nathan McCorkle August 20, 2015 at 1:39 am #

    Thanks! I just used these to compile the MASTER branches of both, since there have been tons of bugfixes since the 3.0.0 release… so far things are great!

    • Adrian Rosebrock August 20, 2015 at 6:58 am #

      Nice — very nice Nathan. I’m sure there will be a ton more bug fixes to come. The 3.0.0 release is nice, but I don’t think things will start to settle down and become more stable until the 3.1 or even 3.2 release.

  23. MJ August 26, 2015 at 9:18 am #

    I install it on Ubuntu
    I get the below error several time. plz any one help me.
    ln: failed to create symbolic link ‘’: File exists.

    • Adrian Rosebrock August 27, 2015 at 6:22 am #

      If the file already exists, remove it:

      $ rm

      And then re-create the sym-link.

    • sohmod August 29, 2015 at 8:30 pm #

      The might had gotten installed in your dist-packages folder, if so the sym-link should be ;

      cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
      $ ln -s /usr/local/lib/python2.7/dist-packages/

  24. Arun Rajagopalan August 26, 2015 at 5:36 pm #

    Hi all,

    If you encounter the error “use of enum ‘AVCodecID’ without previous declaration”during make, then the following link will help you.

    Hello Adrian Sir,

    You deserve massive respect for making such an useful, lucid tutorial! Take a bow Sir! Continue being this awesome!

    • Adrian Rosebrock August 27, 2015 at 6:22 am #

      Nice, thanks for passing this along Arun!

  25. Ernest Chen September 6, 2015 at 10:58 pm #

    Hi Adrian,

    I have been following your instructions without any issues. I noticed that I can only vidocapture the video stream at the recorded fps, and not faster. I found a similar issue here:

    in which it mentions ffmpeg is required rather than the libavcodec library used here. How to do this with your instructions here? Thanks very much.


    • Adrian Rosebrock September 7, 2015 at 8:22 am #

      Hi Ernest — to get FFMPEG support for OpenCV, you’ll need to (1) install FFMPEG on your Ubuntu system, (2) compile OpenCV with the -D WITH_ffmpeg=ON flag set (you should also make sure that’s the correct flag, I’m doing this from memory since it’s been a long time since I’ve had to use FFMPEG), and then (3) re-install OpenCV once the compile has finished.

  26. Lawrence Moore September 8, 2015 at 7:52 pm #

    Hi Adrian,

    Thanks for the super clear tutorial. The normal libraries of openCV work fine, but whenever I try to use something from the opencv_contrib repository like SIFT, it says “AttributeError: ‘module’ object has no attribute ‘SIFT'”. Doing a quick dir(cv2) affirms that the functions aren’t there. Do you know what could have gone wrong? I followed all the steps as best I could. I did notice that when I ran the cmake command it couldn’t find a few packages, but I ran all the apt-get installs as instructed. Thanks!


    • Adrian Rosebrock September 9, 2015 at 6:44 am #

      Hey Lawrence, to read my post on Where did SIFT and SURF go in OpenCV 3?. If you followed my install instructions exactly, then you’ll be able to find SIFT and SURF in the xfeatures2d sub-module of cv2.

  27. Adrian September 9, 2015 at 8:18 am #

    Hi Adrian, I was able to install OpenCV and Python with contrib modules but it seems that some contrib modules are missing.
    For example “text” module seems to be incomplete.

    This is the output of the command help(“cv2.text”). I was expected to find methods like “loadClassifierNM1”, etc.

    Any help is appreciated.

    Help on module cv2.text in cv2:





    • Adrian Rosebrock September 10, 2015 at 6:37 am #

      Some modules may not be compiled and installed if your system does not have the appropriate dependencies installed. I haven’t explored all of the opencv_contrib modules, so I’m not sure which dependencies each of them has. However, after you run CMake, run ccmake .. and you’ll be able to easily navigate and see which modules will be compiled and which ones won’t.

  28. John Wandeto September 12, 2015 at 6:38 am #

    May I update my earlier question. I get this at step 10:

    (cv)john@john-Precision-T1700:~/opencv_contrib/build$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    > D CMAKE_INSTALL_PREFIX=/usr/local \
    > D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
    CMake Error: The source directory “/home/john/opencv_contrib” does not appear to contain CMakeLists.txt.
    Specify –help for usage, or press the help button on the CMake GUI.

    Any idea what could be ailing me?

    • Adrian Rosebrock September 12, 2015 at 6:45 am #

      Hey John — you’re trying to compile OpenCV from the opencv_contrib directory, not the opencv directory, that’s what your problem is. Simply change directory to cd ~/opencv, create your build directory, and then run CMake.

  29. selvam September 14, 2015 at 3:16 am #

    Hi Adrian

    In step 8, after entering “source ~/.bashrc”, I am getting the following response:

    bash: /usr/share/virtualenvwrapper/ No such file or directory

    Could you please guide me

    Thanks in advance

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

      It sounds like you may have forgotten to install virtualenv and virtualenvwrapper in Step 8.

  30. vasista September 16, 2015 at 8:48 am #

    ImportError: cannot open shared object file: No such file or directory

    I got this error while compiling

    Can you please tell how to rectify this issue @Adrian

    • Adrian Rosebrock September 16, 2015 at 11:04 am #

      It sounds like you might have missed Step 3 where you install the required packages to read various image formats from disk:

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

      I would suggest ensuring the packages above are installed, then re-compiling and re-installing OpenCV.

  31. Tanya September 16, 2015 at 10:52 pm #

    Please tell me why am I having this errors?

    tanichka@tanichka-K56CM:~/Sublime$ ./
    ./ line 2: import: command not found

    I dont know why, because I installed numpy as well as opencv. Now I have to link them with python again?

    Please tell me how

    • Adrian Rosebrock September 17, 2015 at 8:12 am #

      Try executing the script with python:

      $ python

  32. Vascom September 17, 2015 at 7:06 am #

    How can I get the list of installed modules in cv2 interface. Exactly, I want to know if cuda based modules are installed in cv2.pyd

    • Adrian Rosebrock September 17, 2015 at 7:33 am #

      The easiest way is to run ccmake .. (that’s not a typo, there are two c’s in the program). This will give you a graphical version of CMake, allowing you to see what is going to be compiled and installed.

  33. Chris September 29, 2015 at 12:01 am #

    Outstanding step-by-step. Rarely do I ever find a guide that I can execute line-for-line with 0 modifications.

    • Adrian Rosebrock September 29, 2015 at 6:01 am #

      Awesome, I’m glad it worked for you Chris! And thanks so much for the kind words. 😀

  34. Paul September 29, 2015 at 8:20 pm #

    Hello, I have done this instruction yesterday. However, Im getting this error after rebooting the ubuntu server. Could you let me know which configurations Im missing? Thanks.

    ~/opencv/build$ workon cv
    workon: command not found

    Self answering to the question.. I went back to step 8 to set up virtual environment..

    • Adrian Rosebrock September 30, 2015 at 6:29 am #

      Nice, I’m glad it’s working for you now Paul 🙂

      • chaima September 28, 2017 at 11:06 am #

        workon: command not found and i didn’t found cv2

  35. Shia September 30, 2015 at 12:19 pm #

    I am new to python,opencv. I followed these instructions and installed opencv,python on my machine(unix). It created two main folders opencv and opencv-contrib with many files and sub dirs.
    I started saving my source files in opencv folder. Initially simple programs were executing perfectly but now i see for imports like
    import matplotlib.pyplot as plt
    import numpy as np
    import scipy.signal as signal etc, it keeps giving ImportError, “eg:ImportError: No module named matplotlib.pyplot” for many such imports. I then have to google and find the solutions.
    I then install the missing module again in the current dir. Exactly which directory should I work into, to avoid this problem?

    P.S- I understand that this might be a very basic question, but i will greatly appreciate your help. I am just a beginner student here and for now I am getting so loaded with my assignments that I am not getting time to figure out the problem and its solution by myself, trying to implement algorithms from my assignments is eating up all my time.

    • Adrian Rosebrock October 1, 2015 at 6:13 am #

      Did you follow the instructions exactly as detailed in this post? You’ll need to make sure you’re in the cv virtual environment, but afterwards, installing the extra packages is easy:

      I would suggest reading up on pip and how it allows you to install packages into Python. Also, there is no need to save your own Python scripts in the opencv folder. Provided you followed the instructions on this post, you can execute your scripts from anywhere on your machine.

  36. Michel October 4, 2015 at 10:25 pm #

    According to docs,

    findContours returns 2 values – contours, hierarchy not 3. And when changed it to
    (_, cnts) = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    it gives error
    c = max(cnts, key=cv2.contourArea)

    • Adrian Rosebrock October 5, 2015 at 6:33 am #

      You can read more about the change in cv2.findContours return signature in OpenCV 3 here — what you linked to is the OpenCV 2.4 docs, not the OpenCV 3 docs.

      A quick fix to get cv2.findContours to work in both OpenCV 2.4 and OpenCV 3, just modify the code slightly:

      And that will take care of everything.

  37. Michel October 5, 2015 at 11:11 am #

    Thanks, Adrian. The fix worked like a charm!

    • Kouki April 24, 2016 at 4:59 pm #

      I am grateful for this great tutorial and for your response I had the same error and that fixed it.

      Thanks a lot!

  38. Toby October 7, 2015 at 1:18 am #

    When I execute make -j4 it says – the source directory “home/toby/opencv/build/ -j4 does not exsist”. Any help thanks

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

      You need to preface that with a “make”:

      $ make -j4

  39. Saurabh Bisht October 11, 2015 at 7:55 am #

    Everything went right before this at the last step

    • Adrian Rosebrock October 11, 2015 at 8:14 am #

      Make sure the file is properly sym-linked into your site-packages directory.

      • Saurabh Bisht October 11, 2015 at 8:29 pm #

        can you please tell me the procedure to do that.

        • Adrian Rosebrock October 12, 2015 at 6:54 am #

          Please see Step 7 of this tutorial. The trick is to make sure the path to the site-packages directory is correct for your installation of Python.

  40. hacklavya October 17, 2015 at 3:09 am #

    I installed succ. fully with this tutorial, thanks.

    and in the installation terminal I ended up with (CV) before myname@computername and I can do import cv2
    when I open a new terminal, I can not see (CV) on command prompt and also I can’t do import cv2.
    is it some environment thing?

    • Adrian Rosebrock October 17, 2015 at 6:43 am #

      Make sure to run workon cv in your new terminal and that will drop you down into the virtual environment, allowing you to import cv2 successfully.

      • hacklavya October 17, 2015 at 3:41 pm #

        Thanks for the answer
        how can I go into that environment (which command to use)?
        I found following commands do it:
        source /usr/local/bin/
        source ~/.bashrc
        mkvirtualenv cv
        am I right? or there is short and easy way?

        • Adrian Rosebrock October 18, 2015 at 7:05 am #

          If you want to access that virtual environment again, all you need is:

          The mkvirtualenv command is only used to actually create the virtual environment.

          • Betty November 9, 2015 at 6:03 am #

            Hi. I also want to go into that environment but I found that it is “workon: command not found” after I followed your step.
            Thank you!

          • Adrian Rosebrock November 9, 2015 at 6:26 am #

            Take a look at the “Troubleshooting” section of this post. While it’s for the Raspberry Pi, it can help diagnose why the workon command is not working for you.

  41. Mohamed Ragab October 19, 2015 at 11:26 am #

    Hi Adrain,

    I have a problem with >>> import cv2,that not modules with this name and I am sure is exited in package modules. Thanks for your efforts.

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

      If you are sure that the file is in the site-packages directory of the cv virtual environment, then make sure you’re typing it:

      $ workon cv

      To drop down into the cv virtual environment prior to running Python or trying to import OpenCV.

  42. Arpit Joshi October 19, 2015 at 12:42 pm #

    I followed all the steps you told above.
    After installing, in am in the cv environment and it successfully imports cv2.

    I exit the terminal and then open it again and type import cv2
    It gives me an error saying cv2 not defined

    also if i type workon cv it gives me an error saying workon not defined.

    I followed each and every step. I hope you can help me out on this one! Thanks

    Also can this be installed without the virtual env? I really dont want to type workon cv again and again. Can’t we directly install it on the ubuntu system?

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

      The virtual environment is certainly optional, it’s just a best practice for Python development.

      If you are getting an error that the workon command is not found, go back and make sure your ~/.bashrc was updated correctly. If you’re still getting an error, try directly sourcing it via: source ~/.bashrc

  43. Krystof October 26, 2015 at 1:13 am #

    Nice tutorial, Adrian.

    If you’re using virtual environments (very reasonable), why do you install the library globally (‘sudo make install’)? Wouldn’t it be more sensible to keep it in the build directory and point the virtual environment there?

    Ultimately the reason I ask is because I’d like to have OpenCV 2.x side-by-side with OpenCV 3.x, and it seems to me that virtual environments are the right way to do it, but it also seems to me that installing the lib globally is not conducive to this effort.

    • Adrian Rosebrock October 26, 2015 at 6:13 am #

      If you want to avoid having OpenCV itself installed globally, just be sure to keep the build directory after the compilation has finished (everything else can be deleted). Then you can sym-link in the file into your virtual environment and it will work just fine.

      It involves a few more steps and I didn’t want to include it since this tutorial is meant for those who are just getting started building OpenCV and installing it.

  44. Hacklavya November 8, 2015 at 2:52 pm #

    I CAN install scikit-image outside the virtual env. but I CAN’T install and use scikit-image in virtual env, and because of that I am unable to make any progress, please include the step(s) for scikit-image installation inside virtual env, so that I can use skimage on following command prompt.

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

      Installing scikit-image inside the virtual environment is just as easy as installing it outside the virtual environment, just let pip do the work for you:

  45. Ben Lansdell November 22, 2015 at 1:49 am #

    Thanks a lot for this, very useful.

    I found this guide worked perfectly on Ubuntu 14.04 on a machine without CUDA installed.

    On an Ubuntu machine with CUDA installed I needed to follow the advice here:

    and edit some of the source code to replace AVCodecID with CV_CODEC_ID.

    • Adrian Rosebrock November 22, 2015 at 7:15 am #

      Thanks for sharing Ben!

    • Ben Lansdell November 22, 2015 at 2:39 pm #

      With a little extra thought, it’s unlikely the AVCodecID is actually related to CUDA. Nonetheless, it may still be useful for some to know that this correction is needed for some setups.

  46. Michael December 23, 2015 at 4:21 pm #

    and I solved the problem, but now I have another not imported into the terminal well opencv
    Now open my subleme_text editor and executor code directly from there I get this error
      import cv2
    ImportError: No module named cv2
    is not it should
    thank you for your help

    • Adrian Rosebrock December 23, 2015 at 6:40 pm #

      Make sure you are in your Python virtual environment before executing the script:

      workon cv

      And then execute the Python script.

  47. Beat Tödtli December 25, 2015 at 6:59 am #

    In step 10 I needed a “cd ..” before
    make -j4
    Kind regards,

  48. Aishwarya December 25, 2015 at 12:54 pm #

    Thanks a ton! Works for me!

    • Adrian Rosebrock December 25, 2015 at 1:44 pm #

      Glad to hear it Aishwarya! 🙂

  49. Srikar December 26, 2015 at 11:27 am #

    Thank You for the post!
    I tried a lot to install on windows first but it wasn’t coming, so I did it on Ubuntu now and it works! 🙂

    • Srikar December 27, 2015 at 2:33 am #

      Also, am having this problem:
      Once it worked and I closed my terminal window and opened a new one and I type “workon cv”,”python”,”import cv2″ I get errors such as not found or no command, I realised I may not be in the correct directory, can you help me how to open it ?
      As am using Ubuntu for the first time I don’t have much idea…


      I have found the solution to my problem, I have seen that many people are facing the same problem and thought I should share…

      Once you open the terminal window start from step 8 with “$ sudo rm -rf ~/.cache/pip”
      and follow all the commands upto ” mkvirtualenv cv”
      Then you enter cv envirnoment and you can open python and import cv.

      Thank You!

      • Adrian Rosebrock December 27, 2015 at 7:56 am #

        After you have already created the mkvirtualenv cv command once, all you need is the workon cv command to drop down into the virtual environment. From there, you’ll be able to import OpenCV.

    • Adrian Rosebrock December 27, 2015 at 7:55 am #

      Glad to hear it Srikar! 🙂

  50. Mahmoud Tolba December 31, 2015 at 9:07 am #

    Hi Adrian,

    Thanks for the great tutorials.

    I installed OpenCV 3.0 and python 2.7 on a virtual environment cv and it works well.

    Now I want to install OpenCV 2.4.11 on another virtual environment to run the old codes instead of modifying them to work with OpenCV 3.0, Should I start with creating a new virtual environment called cv2 for example and continue with the rest of steps?

    • Adrian Rosebrock December 31, 2015 at 1:47 pm #

      Yes, the first step would be to create a new virtual environment. Then, follow the same steps using this tutorial

      However, do not run make install! Simply run make and then sym-link in the into your new virtual environment. All other files and directories besides build/lib can then be deleted.

  51. karun January 4, 2016 at 11:08 am #

    hello first of all thanks for a great tutorial
    am new to this kind of stuffs and while executing one of the programs i get an error saying that

    orb = cv2.ORB()
    AttributeError: ‘module’ object has no attribute ‘ORB’


    also some errors in
    xfeatures2d while using sift

    • Adrian Rosebrock January 4, 2016 at 12:00 pm #

      I would (1) check your OpenCV 3 version and (2) read this post on accessing SIFT/SURF in OpenCV 3.

  52. Moataz January 7, 2016 at 12:30 am #


    I followed every step on a clean VBox machine.
    Everything is run without errors. but there is no file on the machine at all.
    There exist a bunch of so files related to cv (i.e. libopencv_*.so.*) in ‘/usr/local/lib/’ just setting there next to the python folder!

    Any help is much appreciated.

    • Adrian Rosebrock January 7, 2016 at 6:36 am #

      The file should be in /usr/local/lib/python2.7/site-packages/. You should also check build/lib to see if it was compiled. If there is not a file in your build/lib directory, I would suggest double checking the output of your CMake command to ensure Python 2.7 was properly picked up for the build.

  53. Pewe January 25, 2016 at 9:03 am #

    hi Adrian, nice tutorial btw,
    when i followed your tutorial, i already have installed OpenCV2.4.11, and your tutorial works great, there’s no error on OpenCV3. But, is there any way to use OpenCV2.4.11 back instead of OpenCV3? when i first install OpenCV3, i made no changes at the Cmakelists but it still works. thanks

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

      Running a multi-version install of OpenCV on the same system is non-trivial, but the easiest way is to install OpenCV 3 first. Then, create a new build for OpenCV 2.4. After OpenCV 2.4 successfully compiles, you can delete everything but the build/lib directory. Then create a new Python virtual environment and sym-link in the file from the build/lib OpenCV 2.4 directory.

  54. Sridhar January 25, 2016 at 1:21 pm #

    fatal: unable to access ‘’: Could not resolve host:
    What do i do??

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

      Make sure your internet connection is working properly and can load

  55. Dhivya January 27, 2016 at 1:17 am #

    Hi Adrian,
    Nice tutorial! I followed this tutorial and reached till step 12. I am able to see the file in the site-packages. But when I import cv2 in python from the virtual environment, it says “no module named cv2”.
    I am working on Ubuntu 14.04. Can you help with troubleshooting this?

    • Adrian Rosebrock January 27, 2016 at 3:06 pm #

      I would suggest reading through the “Troubleshooting” section on this post.

  56. Ajay Yadav January 28, 2016 at 5:42 am #

    Hi Adrian,
    Very nice tutorial, I followed all the steps. It works good.

    • Adrian Rosebrock January 28, 2016 at 3:39 pm #

      Awesome, I’m glad it worked for you Ajay! 🙂

  57. Todd Brick January 28, 2016 at 4:18 pm #

    Hi Adrian, I got your package with the VM; I am learning a lot and having fun. I was installing the SITL environment from diydrones on it that has some OpenCV code to do a map and icon display. It was obviously written for 2.4.X since it can’t find functions like CreateMatHeader() and SetData(). Do you have any suggestions for replacements for these that will work with 3.0? I can’t seem to find any good docs on how to transition 2.4.X code to 3.X OpenCV code. Thanks, Todd

    • Adrian Rosebrock January 28, 2016 at 6:07 pm #

      Actually, those old bindings pre-date OpenCV 2.4! They are seriously old.

      I personally have tried this, but you should be able to compile OpenCV with the old functionality as well. Go back to the CMake command and try adding --BUILD_opencv_world=ON to he command. The “world” references the old legeacy bindings. If that switch doesn’t work, try running ccmake .. from the build directory and enabling anything related to the “world”.

      Otherwise, you should consider getting an OpenCV 2.4.X installation going. OpenCV 2.4 still has support for the super old bindings.

  58. Ram January 29, 2016 at 1:28 am #

    Hi Adrian,
    The build completed without errors..However when i type workon cv it says command not found..i also did the previous step involving creation of symbolic linkage

    • Adrian Rosebrock January 29, 2016 at 7:32 am #

      If the workon command is giving you trouble, it sounds like your .bashrc file was not updated correctly. Take a look at “Step 8” of this tutorial and also give the “Troubleshooting” section of this post.

  59. Christian February 1, 2016 at 8:28 am #

    Small typo in step 8: “To make the changes to our ~/bashrc file take affect,…” Dot missing in ~/.bashrc. Also it should be “effect” not “affect”.

    • Adrian Rosebrock February 2, 2016 at 10:37 am #

      Thanks for letting me know Christian!

  60. Rig February 8, 2016 at 8:36 pm #

    Hey Adrian, will the cv2.VideoCapture feature work straight away with this install or do the uv4l drivers need to be installed during this setup to allow webcam access? Thanks for another great tutorial btw!

    • Adrian Rosebrock February 9, 2016 at 4:01 pm #

      Are you accessing a USB webcam or the Raspberry Pi camera module? For the USB webcam, you don’t need the UV4L drivers and cv2.VideoCapture should work out of the box.

  61. Joshua February 19, 2016 at 7:13 am #

    Hi Adrian!

    I followed all the steps you indicate and everything has been installed without errors . On completion, check the version of your cv and example has worked. But after reboot I get the error ” no module named numpy “, and “no module named CV2 ” . I do not know what could have happened . thanks

    • Adrian Rosebrock February 19, 2016 at 7:24 am #

      Make sure your ~/.bashrc file has been successfully updated and the changes to include virtualenv and virtualenvwrapper are still there. If so, be sure to enter the cv virtual environment before executing your Python script:

      • joshua February 22, 2016 at 6:49 am #

        thanks, i update my ~/.bashrc and now works.

        • Adrian Rosebrock February 22, 2016 at 4:19 pm #

          Congrats on resolving the issue, Joshua!

  62. suresh February 24, 2016 at 3:00 pm #

    I just followed your steps and in the first try everything went smooth and it is installed successfully. By the way I am using Linux Mint 17.3 Mate. Your installation steps are just WOW. Thanks a lot. 🙂

  63. Sandeep February 25, 2016 at 2:48 pm #

    This is a great resource! Thanks for these installation instructions. Coming from someone who has ran into all sorts of issues while compiling OpenCV + Python on Ubuntu and Mac.

  64. panini February 26, 2016 at 4:31 am #

    Thanks a million!
    Very clear, and step by step. Needed to rope in the contrib repo and thanks for getting me to do the virtual env… been dragging my feet on that 🙂

    • Adrian Rosebrock February 26, 2016 at 1:50 pm #

      No problem, happy to help 🙂

  65. Ravi Naik March 5, 2016 at 8:27 am #

    Thanks a lot Adrian…Finally I could get Opencv working on my ubuntu…Thanks for your precise instructions…

    • Adrian Rosebrock March 6, 2016 at 9:17 am #

      No problem Ravi, congrats on getting OpenCV installed!

  66. Cédric March 5, 2016 at 5:36 pm #

    So I am just wondering what the point of the virtual environment is. How do I get to use OpenCV when I open up a new terminal. I have some python scripts, that are scattered all around my computer, and they usually tell me that they do not know the module cv2. Can you tell me how to make the module recognised everywhere?

    • Adrian Rosebrock March 6, 2016 at 9:17 am #

      The point of using Python virtual environments is to have an isolated environment for each project you are working on. For example, suppose you are working on a project that requires package X v1.0. But another project you are working on requires package X v2.0. What do you do? Buy another machine? Install a VM? Nope. Instead, you use virtual environments. Each Python virtual environment is entirely isolated from the system install of Python, therefore you can install different versions of Python packages without having to worry about conflicts.

      In either case, you need to execute the workon command before running your Python script to drop you into the cv virtual environment. Inside this virtual environment, the OpenCV bindings can be found and recognized:

  67. Greg S March 9, 2016 at 3:49 pm #

    I meant this worked great 🙂 Haha sorry

    • Adrian Rosebrock March 9, 2016 at 4:36 pm #

      I’m glad it worked for you Greg! 🙂

  68. Mathys March 19, 2016 at 8:46 am #

    Hi man thanks for the tutorial(and all the other).

    I have followed all the steps and redone them as well but no luck.

    when I import cv2 it says no such file.

    so I checked with sudo find / -name “cv2.s0” and it says its in my home/mathys/.virtualenvs/cv/lib/python2.7/site-packages/

    but when I go into that file it is empty, theres no
    Another thing as you mentioned earlier to check my cmake output under python2 everything looks the same as the sreenshot you gave a link too but where it says libraries it says no instead of something like usr/lib/

    Many thanks

    • Adrian Rosebrock March 19, 2016 at 9:07 am #

      If you ran find and it returned a file, then there is absolutely in the /home/mathys/.virtualenvs/cv/lib/python2.7/site-packages/ directory, so I would double check there again.

      • Mathys March 19, 2016 at 9:47 am #

        Man why must life be so hard haha

        I just double checked it again I go into computer/home/mathys/ and then there is no folder called virtualenvs

        • Adrian Rosebrock March 20, 2016 at 10:45 am #

          It’s a hidden directory. Note the “.” before the directory path “.virtualenvs”

  69. Felipe March 29, 2016 at 12:19 pm #

    Excellent guide! Thanks a lot.

    • Adrian Rosebrock March 29, 2016 at 3:37 pm #

      No problem Felipe, I’m happy the guide helped!

  70. mikele March 31, 2016 at 10:49 am #

    Awesome! Everything works perfectly. Thank you.

    • Adrian Rosebrock March 31, 2016 at 2:53 pm #

      Glad to hear it! 🙂

  71. Jacob S April 1, 2016 at 12:55 pm #


    I got everything set up alright, but when running the example script (copied into a python file), I get the error “The lower bounary is neither an array of the same size and same type as src, nor a scalar in function inRange”

    I’m using python 2.7 and openCV 3.1

    Any idea what’s causing this?

    • Adrian Rosebrock April 1, 2016 at 3:11 pm #

      Double check your path passed to cv2.imread. Typically you’ll see cv2.inRange throw that error message when the image path is invalid and thus the image is not properly loaded from disk.

  72. Paul April 4, 2016 at 4:07 am #

    Excellent installation guide! I successfully installed OpenCV 3.1.0 in Ubuntu with the help of this article.

    • Adrian Rosebrock April 4, 2016 at 9:25 am #

      Awesome job Paul! 🙂

  73. Mo Badawy April 8, 2016 at 6:10 am #

    Hi Adrian, Awesome tutorial but I ran into a snag. Regardless of what I do, querying the opencv version yields 2.4.8!!! After some digging it turned out that Linux Mint 17.3 Cinnamon have a dependency on opencv, so opencv 2.4.8 was already installed by system. Uninstalling opencv (2.4.8) => remove Cinnamon (among others as well, e.g. Blender) as well. Cinnamon is Mint’s Desktop Environment, so removing it is not an option.

    So, here is what I did and it seems to be working so far 🙂
    1. In step 10, in the cmake command, change the installation directory to a local folder, e.g. use “-D CMAKE_INSTALL_PREFIX=/home//local \”
    2. Then use “make install”, i.e. drop “sudo”.
    3. cd to the installation folder and search for “” via:
    $ find ./ -iname
    4. Copy the resulting folder’s path (e.g. /home//local/lib/python2.7/dist-packages) and add it to $PYTHONPATH by doing the following:
    – Use your favorite text editor to open/create ~/.profile
    – Add the line: export PYTHONPATH=$PYTHONPATH:/home//local/lib/python2.7/dist-packages
    – Save & exit, then $ source ~/.profile
    5. Loading the cv2 module in Python & checking version now yields 3.1.0, while system dependence on opencv 2.4.8 is uninterrupted.

    • Adrian Rosebrock April 8, 2016 at 12:51 pm #

      This is fantastic, thanks so much for sharing! 🙂

  74. Anagh April 10, 2016 at 1:25 am #

    Hello Adrian ,

    When i used git checkout 3.0.0 in opencv_contrib
    i m getting this error

    HEAD is now at 6123e89… Merge pull request #247 from cbalint13/daisy

    • Adrian Rosebrock April 13, 2016 at 7:09 pm #

      That’s not an error message at all — it’s just the output of running git checkout.

  75. Emre April 18, 2016 at 7:21 am #

    Hey Adrian. After installing and uninstalling for 5 times finally i installed the opencv 3.1.0 successfully. Thanks for the great tutorial .

    • Adrian Rosebrock April 18, 2016 at 4:45 pm #

      Congrats Emre!

  76. ed April 26, 2016 at 12:00 pm #

    Hi, Great tutorial! Hopefully a simple issue, for some reason the game.jpg is rendered smaller that is should be e.g I can’t see the red game. It’s not shrunk compared to the original in ratio but it looks more cropped with the top left hand corner be OK extending out to 50% of the original image.



  77. Ronald April 29, 2016 at 6:28 pm #

    Thanks a lot for taking time to write this amazing tutorial

    • Adrian Rosebrock April 30, 2016 at 3:52 pm #

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

  78. ruben May 11, 2016 at 9:49 am #

    hello adrian!!! i have this problem when compiling program!!!

    can you help me?

    • Adrian Rosebrock May 12, 2016 at 3:43 pm #

      Hi Ruben — please read the comments before posting. Michel and I have discussed the problem and solution in a comment above.

  79. ruben May 13, 2016 at 11:19 am #

    thanks a lot!!!!!

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

      No problem Ruben!

  80. Swapnil May 18, 2016 at 2:19 am #

    Hey Adrian after installing opencv, writing the code and downloading the image i am continuously getting assertion failed error i.e. its not reading the image but it works fine for videos. How can I fix this ??

    I have saved the image in the same directory of the python code and tried several path name apart from “games.jpg” or “games.jpeg” but its showing the same error every time.

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

      It sounds like OpenCV is having issues loading images from disk, but it able to process videos just fine. In that case, you might have skipped over Step 3 where the image I/O libraries are installed.

  81. Vania May 18, 2016 at 8:34 pm #

    Hi Adrian!

    Could we install both OpenCV 2.4 and 3 on MacOS?

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

      Absolutely! The process is a bit more complicated, but you need to compile both of them — but don’t run make install. Instead, you’ll want to delete everything but your build directory and then sym-link the files into their corresponding virtual environments.

  82. mayank May 20, 2016 at 4:54 pm #

    I think the order of executing these commands must be reversed.

    First sourse then export

    1: source /usr/local/bin/
    2: export WORKON_HOME=$HOME/.virtualenvs

    If i followed the the order mentioned in post and it could not recognize mkvirtualenv command.

    BTW great post adrian.

    • Adrian Rosebrock May 21, 2016 at 8:13 am #

      According to the install instructions, the export comes before the source. I’m not sure why it would need to be reversed in your case.

  83. Douglas May 25, 2016 at 3:35 pm #

    Hello Adrian, is there any good tutorials like your that you know that does not use cv virtual envs ?

    i ask this because i need to call my python scripts from another aplication (unity) but i can’t find a way to do that using opencv virtual envs, basicaly i need the command “python” to run my script using opencv without any virtualenv

    • Adrian Rosebrock May 25, 2016 at 5:38 pm #

      You can still use these same instructions to install OpenCV with Python bindings without the virtual environment. Just skip Step #8 and Step #11. Also, take note of which site-packages directory the output bindings will be stored. That will help debug any issues when it comes to import cv2.

      Alternatively, if you need to call a Python script via a separate application, you can just as easily call a shell script form a separate application. The shell script can them access the Python virtual environment and then call the Python script, just like I do in this post.

      • douglas May 29, 2016 at 3:50 pm #

        Thank you for your answer, i am just having trouble with the “source /home/pi/.profile” path, i don’t know what path should i use in my machine, whats default ?

        note: i followed the tutorial exactly.

        • Adrian Rosebrock May 29, 2016 at 4:34 pm #

          Are you trying to install on Ubuntu or a Raspberry Pi? The tutorial you’re currently following is for Ubuntu. On Ubuntu, the path should be /home//.bashrc. On the Pi it should be /home/pi/.profile

      • douglas May 29, 2016 at 4:30 pm #

        Thank you Adrian !

  84. Yash K May 30, 2016 at 3:02 am #

    I proceeded as mentioned in the tutorial and everything went as per the tut till step 10 .
    While using the ln command I got this error:
    ln: failed to create symbolic link ‘’: File exists

    and file is empty and when i start working cv2 is not found

    • Adrian Rosebrock May 31, 2016 at 4:04 pm #

      Take a look at your output of CMake, specifically the “Site packages configuration for Python. This is where your bindings will be stored. Then, sym-link this file into the site-packages of your virtual environment.

  85. y2k May 31, 2016 at 12:22 am #

    Hi Adrian
    Amazing tutorial but I am having a problem executing the last step , it is saying

    ln: failed to access ‘’: Too many levels of symbolic links

    So can you please suggest a work around to this or a way to resolve this , thanks in advance

    • Adrian Rosebrock May 31, 2016 at 3:43 pm #

      That’s quite strange, I’ve never encountered that error message before. It sounds like your sym-links are not valid. Can you double-check that the file exists?

  86. Michael June 7, 2016 at 11:40 am #

    Hi Adrian,

    Thanks for providing the tutorials for this and installing openCV with python 3 I was wondering what you gain (or loose) from using python 3 over python 2 with OpenCV3 in particular and what config would you recommend?

    Also any advice for installing OpenCV3 as part of a cluster requirement (having OpenCV3 available on every machine in the cluster) I see the benefit of the virtual environments but I expect my issue will be getting 1 consistent environment and deploying it to a cluster

    • Adrian Rosebrock June 7, 2016 at 2:48 pm #

      There aren’t any real benefits to using OpenCV 3 with Python 2 or Python 3 — simply go with the Python install that you feel most comfortable with.

      As for installing OpenCV in a cluster environment, I would suggest creating a “master image” that has OpenCV installed and then replicating it across each of the systems in the cluster.

      • Michael June 7, 2016 at 3:46 pm #

        Thanks, python version would be 1 less job to worry about. The master image route have been my preference but beggars can’t be choosers.

        Is it possible to run the “make install” command from outside the build directory and should I run cmake -> make -> make install on every system in the cluster or can I get away with just “make install”

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

          Is your hardware and OS the same? Because if so, you can just compile OpenCV once, copy the build directory to each machine, and run make install.

          If the environment is different for each system, then yes, you’ll need to run cmake, make, and make install on every system.

  87. XuanToa June 9, 2016 at 9:25 pm #

    Hi Adrian,
    Thanks for your tutorials.
    To enter the cv enviroment, I must follow 4 steps
    1. export WORKON_HOME=$HOME/.virtualenvs
    2. source /usr/local/bin/
    3. source ~/.bashrc
    4. workon cv

    If i just use ” workon cv”, I always get an error: command not found.

    Can you solve this problem?

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

      Hey XuanTao — it looks like your ~/.bashrc file has not been updated correctly. Make sure you open it:

      $ nano ~/.bashrc

      And ensure you have included:

      At the bottom of the file.

  88. Sagar June 13, 2016 at 11:38 am #

    Can I just delete the opencv and opencv_contrib folders once I finish installing?


    • Adrian Rosebrock June 15, 2016 at 12:46 pm #

      Yes, once you have finished installing OpenCV, feel free to delete both the opencv and opencv_contrib directories.

  89. Akshat Jain June 15, 2016 at 1:39 pm #

    I need to get opencv working on Apache Storm and everytime I install opencv and import it inside I keep getting an error that the requirements for cv2 were not met. Any ideas on how I should go around this error?

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

      Apache Storm is a distributed system for computation — which means that all nodes on your system need to have OpenCV installed on them. This includes all the dependency libraries, NumPy, etc. I haven’t tried to install OpenCV specifically on a Storm cluster before, so it’s pretty hard for me to diagnose this error.

  90. Dinesh June 17, 2016 at 5:05 am #

    Hey Adrian! Excellent Tutorial. But I have a small query in Step 10. I have read the comment and haven’t found anyone asking this. Should we exit the virtual environment ‘cv’ when you say ‘Our environment is now all setup — we can proceed to change to our home directory’.
    I did cd ~ but that still keeps me in the virtual environment ‘cv’.. So am I supposed to continue there or exit the virtual environment before I clone the git?

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

      Once you enter the cv virtual environment, you should remain in that virtual environment for the remainder of the tutorial. Do not exit the cv virtual environment.

  91. Youssef June 18, 2016 at 8:56 pm #

    Hello Adrian!
    So I have run this whole tutorial around 5 times now and I am almost certain I did not miss any steps. I watched the other video from your tutorial with the Pi and viewed the troubleshooting section of that page. However, I cant seem to make the error go away.
    ImportError: No module named cv2
    when i go to ~/.virtualenvs/cv/lib/python2.7/site-packages/ there is a, when I double click it it says
    “This link cannot be used because its target “/usr/local/lib/python2.7/dist-packages/” doesn’t exist.”
    The /usr/local/lib/python2.7/site-packages directory is always empty. I think it should have the file there or something, but it doesn’t have it.
    I am not sure what I may b doing wrong.

    I am fairly new to linux so my lingo is still new, I will try to understand your explanation.
    Thank you for your time.

    • Adrian Rosebrock June 20, 2016 at 5:32 pm #

      Hey Youssef — unfortunately, there are many reasons why you might not be able to import OpenCV. I detail the main reasons in the “Troubleshooting” section of this post.

  92. Raul June 28, 2016 at 8:41 am #

    Hello Adrian,

    Thanks for the great tutorial. I noticed the upper threshold used to retrieve the red game is practically the blue color. Could you explain the reasoning behind this? What changes should I make to retrieve the red instead? I tried messing around with the range values but was not successful.

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

      RGB colors are actually stored in reverse order in OpenCV, so we actually write them in BGR order. To detect red, you’ll want to change the range values. Given the new knowledge regarding RGB versus BGR order, this should help 🙂

      • Raul July 1, 2016 at 2:46 pm #

        Hi Adrian!

        Thanks for getting back at me. In my last comment I meant to say blue instead of red!

        Taking into account your comments about the color ordering I fiddled around with the thresholds but I did not get the results I expected.

        These are the values I attempted:

        upper = np.array([255, 65, 65])
        lower = np.array([150, 0, 0])

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

          It’s a game of trial and error, and based on your lighting conditions, colors can appear differently. I would suggest using the range-detector script inside the imutils library to help you tune the threshold.

  93. Aris July 4, 2016 at 3:40 pm #

    Thanks for this easy to follow tutorial. Just to add a little on it if I could.

    After following all with no Error whatsoever I started having the following error while trying to follow the simple example at the end: (pyhton

    ImportError: No module named cv2

    It happened for the simple reason that, as a beginner, I failed to understand how to update the ~/.bashrc file

    The line at Step 8 are supposed to be added to the ~/.bashrc file. Not to be copy and paste in the terminal which I did in my first attempt.

    You should copy those lines

    use the following line to open the ~/.bashrc file

    $ sudo nano ~/.bashrc

    Copy and paste the code there and source the ~/.bashrc file.

    This is how I solved the “ImportError: No module named cv2”

    Thanks Adrian

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

      Hmmm, it’s strange that you would need to be sudo for this — that shouldn’t be necessary at all. Were you trying to install OpenCV as the “root” user? Or a standard, normal user?

      • Aris July 6, 2016 at 7:08 am #

        I was just trying to access the .bashrc file. May be I did not need to be sudo.

  94. San July 8, 2016 at 1:37 pm #

    Works like a charm, thanks Adrian.

    • Adrian Rosebrock July 9, 2016 at 7:24 am #

      Fantastic, glad to hear it San! 🙂

  95. Gray July 26, 2016 at 12:51 pm #

    Thank you so much for this tutorial! I used it to install OpenCV 3.1 and Python 2.7 on an Ubuntu VM on VirtualBox. It worked very smoothly and I was able to try out the demo program by the end of the day. After tearing my hair out trying to work out how to install OpenCV for Python on my Windows 8.1 machine this was so much easier. Thank you, sir.

    • Adrian Rosebrock July 27, 2016 at 2:00 pm #

      Nice job getting OpenCV installed on your Ubuntu machine, congrats!

  96. Antoine August 9, 2016 at 5:12 am #

    Many thanks Adrian for you tutorial !
    Hard time to get it working on Linux Mint 17.2 but (after many tries) using the opencv zip file its done !
    Gotta catch the red !

    • Adrian Rosebrock August 10, 2016 at 9:30 am #

      Congrats on getting OpenCV installed Antoine! 🙂

  97. Alex August 17, 2016 at 9:01 am #

    In my cmake report I have not the “packages path” neither for python 2.7 nor for python 3.4. I’ve followed all the instruction unless the use of the virtual environment. As result, i can see all the opencv modules compiled (i.e., all the .so files) in the “/usr/local/lib” directory but the file. And I still not find such a file.

    • Adrian Rosebrock August 17, 2016 at 11:58 am #

      It sounds like your site-packages directory is not being automatically determined for some reason. Try updating your cmake command to use the following switch:

      -D PYTHON_PACKAGES_PATH=$VIRTUAL_ENV/lib/python2.7/site-packages

  98. Anushree August 28, 2016 at 8:01 am #

    Looks like i forgot to install numpy. How to go about it now?

    • Adrian Rosebrock August 29, 2016 at 1:57 pm #

      You can install NumPy by accessing your virtual environment and using pip. Go back to Step #9 and continue with the tutorial.

  99. Abder-Rahman August 28, 2016 at 7:45 pm #

    Very nice and detailed tutorial, keep it up!

    Thanks for your kind efforts.

    • Adrian Rosebrock August 29, 2016 at 1:55 pm #

      Thank you Abder-Rahman, I appreciate that 🙂

  100. Luis September 1, 2016 at 3:04 pm #

    Hi Adrian,

    When I run :
    make -j4

    CUDA causes problems because my default gcc version is 5.4, and CUDA only works with gcc version 4.9 & below. I have followed the guidance at

    to solve the problem, but it is not working. So I have three questions:

    1. Is it possible to eliminate the dependence on CUDA?

    2. If so, how?

    3. What functionality in openCV might I lose if I were to do so?

    • Adrian Rosebrock September 2, 2016 at 7:03 am #

      You can compile OpenCV without CUDA support. If your CMake command is automatically picking up CUDA support (likely due to NVIDIA/CUDA drivers already being installed), then just disable them within your CMake command by adding the following switch:


      You won’t lose much support, just any GPU optimizations that would have otherwise been installed.

  101. Gaurav September 5, 2016 at 6:53 am #

    After following all the steps my cv2 version is 2.4.8
    How to create virtual environment for cv2??
    Unable to run sift

    • Adrian Rosebrock September 5, 2016 at 7:58 am #

      It sounds like you may not be in the cv virtual environment. Make sure you are in the correct Python virtual environment using mkvirtualenv and the workon commands when compiling and installing OpenCV.

  102. Zack Mitkin September 13, 2016 at 6:49 pm #

    Hello, I did all the steps correctly, but python cannot find cv2 or numpy. I swear I did all the steps correctly, and I got no errors. Please tell me a way to check what the problem is

    • Adrian Rosebrock September 15, 2016 at 9:37 am #

      If you cannot find cv2 or Numpy, then you are likely not in the cv virtual environment when trying to import your libraries:

      • jai September 21, 2016 at 2:44 am #

        I am also facing similar problem n i am quite sure that i am working withing cv virtualenv.

  103. Moni Delgado September 19, 2016 at 10:02 pm #

    I can’t even run the “workon cv” command. I think it’s because I’ve already closed the terminal and log in again, so I’m not running stuff in the right place…what are the easy steps to access cv on python and run again the example you showed, for example.

    • Adrian Rosebrock September 21, 2016 at 2:17 pm #

      If you can’t run the “workon” command then it’s likely your ~/.bashrc file wasn’t updated properly. Go back to Step 8 and check the contents of the file.

  104. Nicolas October 4, 2016 at 4:21 pm #

    Thank you very much for this tutorial 😀

    • Adrian Rosebrock October 6, 2016 at 7:01 am #

      No problem Nicolas, I’m glad I could help 🙂

  105. Sai October 26, 2016 at 3:36 pm #


    I already have opencv 2.4.8 and tried to do the above process and when i ran:

    import cv2

    print cv2.__version__

    it printed 2.4.8

    How can i upgrade it to 3.1.0?


    • Adrian Rosebrock November 1, 2016 at 9:31 am #

      It sounds like you’re not in the proper virtual environment. Make sure you run:

      $ workon cv

      To access your Python virtual environment. You should also check the site-packages directory for the “cv” virtual environment to ensure the bindings are there.

  106. Pedro Lorente November 4, 2016 at 5:19 pm #

    Every time I close the terminal window, I have to repeat the whole process of Chapter 8, to be in the environment (cv).
    Is there anyone who can help me?
    Thank you all!

    • Adrian Rosebrock November 7, 2016 at 2:56 pm #

      Every time you open a new terminal all you need to do is:

      $ workon cv

      No other command except for the one above needs to be executed to access the “cv” Python virtual environment.

  107. neztorig November 8, 2016 at 10:52 am #

    Great I following this step to install OpenCV and its work on ubuntu.
    many thank’s

    • Adrian Rosebrock November 10, 2016 at 8:44 am #

      Great job getting OpenCV installed! 🙂

  108. Melki November 23, 2016 at 11:49 am #


    I am trying to install OpenCV 3.1.0 with python 2.7 on linux. All the installation process went ok (I have some problems that I solved) but finally I see that the is installed in /usr/local/python/2.7/ rather than /usr/local/lib/python2.7/site-packages. Do you have any idea why? I solced the problem by copying the to /usr/local/lib/python2.7/site-packages manually but I guess it is not the way it should be done..


    • Adrian Rosebrock November 23, 2016 at 3:17 pm #

      I’m not sure why the file would have ended up in /usr/local/python2.7, but yes, copying it into /usr/local/lib/python2.7/site-packages is the correct solution.

  109. Namrah November 23, 2016 at 3:08 pm #

    This was aweosme, I had been struggling with opencv and opencv_contrib installation. Thank you so much, this really helped.

    • Adrian Rosebrock November 23, 2016 at 3:16 pm #

      Congrats on getting OpenCV installed on your system Namrah, nice job!

  110. DanDan December 3, 2016 at 3:12 am #

    Thanks very much for the great post. I wonder how much time I spent to try out and compile into this comprehensive post.
    Thanks again.

  111. ibrahim December 5, 2016 at 7:16 am #

    hi thanks for the great tutorial you give here,
    i have installed the opencv3.1(with the instruction above) and python 2.7 successfully, but when come to run the program example you give, it give an error that said, “OpenCV Error: Sizes of input arguments do not match (The lower boundary is neither an array of the same size and same type as src, nor a scalar) in inRange”
    i still new at this , perhaps you can give me some direction to solve this error


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

      Hey Ibrahim — it sounds like you might have passed an invalid path to cv2.imread. Double check the path to your input image is correct.

  112. Mehta Jaldhi December 16, 2016 at 1:42 am #

    Actually I have installed opencv in different login.. now I need to login in hduser.. how can i remove from my local login?

  113. Hakan December 19, 2016 at 5:18 pm #

    Hi Adrian,

    Great tutorial. I followed all the steps installed OpenCV on Ubuntu successfully. Thanks !

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

      Fantastic, nice job getting OpenCV installed Hakan!

  114. Madhu Oruganti December 20, 2016 at 5:14 am #

    Dear Sir, I am using the Ubuntu 14.04 and I installed the Opencv3 as per given steps, but I am getting two problems as follows
    1. If I close the terminal I could not open the CV directly until process the step8 and 9.
    2.ImportError: No module named matplotlib
    Please respond to my problem.

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

      1. If you close your terminal you can re-access the “cv” virtual environment via the workon command:

      $ workon cv

      2. You need to install matplotlib into the “cv” virtual environment:

      • Madhu Oruganti December 22, 2016 at 11:21 pm #

        it’s working sir.Thank you so much, sir.

  115. Madhu Oruganti December 27, 2016 at 6:00 am #

    Sir, Can you please tell me the code/command to run the video in python and opencv3

    • Adrian Rosebrock December 31, 2016 at 1:35 pm #

      Are you trying to access a video stream with OpenCV? If so, please see this post.

  116. Artur December 29, 2016 at 2:11 pm #

    Using your post I’ve just installed OpenCV 3.2 with Python 3.5 on Ubuntu 16.04.
    Thanks Adrian 🙂

    • Adrian Rosebrock December 31, 2016 at 1:23 pm #

      Congrats on getting OpenCV installed Artur, nice job! 🙂

  117. Yuvraj January 8, 2017 at 2:59 am #

    Thank you Adrian!
    up to step 11 everything works perfect but i got error in step12.

    $workon cv
    workon: command not found

    can you please help me?

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

      Please see the “Troubleshooting” section of this blog post for common reasons why the “workon” command may not be found.

      • yuvraj January 15, 2017 at 5:47 am #

        Getting error while importing “cv2”
        “ImportError: No module name cv2”

        even “” present in site-packages.
        so whats going wrong?

        • Adrian Rosebrock January 15, 2017 at 12:02 pm #

          It’s hard to say without having physical access to your machine, but I think the issue is that you aren’t in the “cv” virtual environment:

          $ workon cv

  118. Alex January 19, 2017 at 10:00 pm #

    One key package which I needed to install to get this working was python-dev…without it, everything runs successfully but is never built and therefore you cannot use the library.

    • Adrian Rosebrock January 20, 2017 at 10:56 am #

      Correct, you need the Python development headers installed before trying to compile OpenCV.

  119. Mehta Jaldhi January 21, 2017 at 4:01 am #

    Thankx Adrian.

    Loved your article.

    But can you tell me how to uninstall the whole method what we just did?

    Actually I need to again install it.

    • Adrian Rosebrock January 22, 2017 at 10:17 am #

      You can delete OpenCV by removing all files that contain “opencv” in the filename from /usr/local/lib. Alternatively you can simply re-run make and make install and you will overwrite all the old files.

  120. Tony January 21, 2017 at 9:20 pm #

    I get to step 10 and type make -j4, but it doesn’t work, instead printing this error:

    make: *** No targets specified and no makefile found. Stop.

    It seems that cmake didn’t entirely work. There is a file in opencv/build/CMakeFiles/ called CMakeError.log. Scanning through that, I see a number of errors, but I don’t know how to resolve them.

    I should note I’m doing this using MinGW/msys on a Windows 10 machine.

    • Saikanam January 29, 2017 at 9:39 pm #

      I had the same issue,try fixing the -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ thing,try to fix it to wherever you have cloned opencv_contrib.. hope this helps 🙂

  121. Deepan Raj January 22, 2017 at 12:16 pm #

    Hi !!
    Love this blog. But i dont have good computer specs that can support vmware. So i started to install Opencv for python in windows, also installed numpy, scikit, matplotlib too! Can you Please tell me is there any way to install dlib in windows.. installed all other packages like a charm in windows..Only thing i cant do is how to install dlib..I have downloaded dlib package. installed cmake….
    Note: do i need visual studio to install dlib ?

  122. Huck January 24, 2017 at 1:27 pm #

    I have used these steps many times without error but this time I’ve had problems. First, I had errors with opencv_perf_stitching so I just disabled building that module so I could get through the make step. I did the install and ldconfig but there is NO on my machine anywhere! What in tarnation am I doing wrong?

    • Adrian Rosebrock January 24, 2017 at 2:15 pm #

      Hi Huck — I’m sorry to hear about the issues with compiling OpenCV. Can you check the build/lib directory and see if the file is there? If it’s not, there was likely a misconfiguration within CMake.

    • Huck January 24, 2017 at 2:23 pm #

      I realized that I could not import numpy either….I had to re pip install numpy but this time with sudo…

      sudo pip install numpy

      I completely wiped my build directory and started over….this time showed up as planned.

      Note: I do NOT use the virtual environments so perhaps this is why I needed to sudo pip install?

      • Adrian Rosebrock January 24, 2017 at 2:33 pm #

        If you did not use the virtual environment then yes, you would need “sudo” to have the proper permissions to install any Python packages into your system’s site-packages directory.

      • Huck January 24, 2017 at 3:27 pm #

        Adrian…thanks for your great instructions. Is it possible to build opencv 3.1.0 for both Python 2.7 (already done and working happily) *AND* Python 3.4 (not instead of)? Can I just follow your instructions here ( or is there something else that needs to be done?

        • Adrian Rosebrock January 26, 2017 at 8:28 am #

          To install OpenCV 3.1 for Python 2.7 and Python 3.4 you will need to run CMake and make twice. Once for Python 2.7. And then again for Python 3.4. Copy the file created each respective Python version into their respective site-packages directory.

  123. Tonmoy Roy January 29, 2017 at 12:19 am #

    Hi, I didn’t read through all the comments, but someone might have asked this question already. What if OpenCV_Contrib is not the same version as OpenCV?

    • Adrian Rosebrock January 29, 2017 at 2:42 pm #

      You should be using the same opencv_contrib version as your opencv version, otherwise you’ll likely run into errors.

  124. Deepak Sharma February 2, 2017 at 10:55 am #

    in case you get an error numpy not found after pip install during make

    export PYTHON_INCLUDE=$PYTHON_INCLUDE:/home/USERNAME/.virtualenvs/cv/lib/python2.7/site-packages/numpy

  125. Siddhesh Acharekar February 12, 2017 at 1:32 pm #

    Hey I followed this step by step, everything worked great but in the last step i wasnt able to create the link. I got this error:

    ln: failed to create symbolic link ‘’: File exists

    Not able to understand what this means but mainly how do I go about this? Also when I enter the python environment it shows version 2.4.8 which was preinstalled. I want to move onto 3.0.0 and based on your tutorial thats all but done due to this one error. Please help.

    • Adrian Rosebrock February 13, 2017 at 1:41 pm #

      If the sym-link already exists (I’m not sure why it would already) you can simply delete it and re-create it:

      rm /path/to/sym-link

  126. Zerui March 1, 2017 at 6:00 pm #

    Hi Adrian,

    I am current setting up multiple virtual environments for different configuration combinations (i.e., OpenCV 3 with Python 2.7 and OpenCV 3 with Python 3.6). I followed the tutorial; everything was good until I built the OpenCV library. The problem is that CMake was not able to pick up the correct Python interpreter in my virtual environment. I force to set it to /home/username/.virtualenvs/cv_py2/bin/python, and this time it was able to find the correct interpreter, but the wrong Numpy include path. I am curious about the Cmake working mechanism and why it didn’t find the correct path and interpreter for me automatically. Is that a problem of my PATH and PYTHONPATY?


    • Adrian Rosebrock March 2, 2017 at 6:43 am #

      If you are in the correct Python virtual environment for your respective version, CMake should be able to automatically determine your interpreter, NumPy path, etc. Are you in each of the respective virtual environments when running CMake?

  127. Bharathi March 2, 2017 at 11:45 pm #

    Hi, I am trying to run opencv on my linux, following this tutorial step by step. But i could not compile. till last step it is fine but when i run “$ make -j4” the processing is done till 31% then system is getting stuck and shutting down. sometimes it runs in that particular step hours together. Can anyone please help me?

    • Adrian Rosebrock March 4, 2017 at 9:40 am #

      This sounds like a race condition using multiple cores. Clean the build and then retry the compile using only a single core:

  128. Bharathi March 6, 2017 at 1:46 am #

    Hi Adrian, i tried compiling using single core even though the same story repeats…
    Does the remaining space in the disk or the type of processor matters?

    • Adrian Rosebrock March 6, 2017 at 3:38 pm #

      The processor shouldn’t matter, but you should ensure you have a few gigabytes of space on your machine just to be safe.

  129. Rodrigo Cesar da Silva March 13, 2017 at 6:16 pm #

    My installation of opencv is working fine, except for the fact that the modules of opencv-contrib were not installed.

    I am not sure why this is happening since i am pointing to opencv-contrib folder.

    Can anyone help with that? How can i check after Cmake if those modules are going to be installed?



    • Adrian Rosebrock March 15, 2017 at 9:03 am #

      Check the “Modules to be compiled” section of CMake and ensure the contrib libraries are included. Double and triple-check your paths to the opencv_contrib directory. You might also want to try deleting your “build” directory, re-creating it, and re-running CMake.

  130. ezlikespie March 16, 2017 at 2:04 pm #

    Everything worked fine except I don’t think virtualenv installed because there is not a .virtualenv folder to put in. The only folders I have are the opencv and the opencv_contrib folders. I dont understand anything about the virtual env or how to install it.

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

      If you’re new to using Python virtual environments I would suggest reading through this tutorial before continuing.

  131. Huang tzuen March 30, 2017 at 6:37 pm #

    Hi Adrian,

    Here is installation come with a problem on STEP 3.
    It show :package ‘libpng12-dev’ has no installation candidate

    And it show some message on ubuntu16.10 : this may mean that the package is missing, has been obsoleted , or is only available from another source.

    Really thanks for providing such a great tutorial to beginner.


    • Adrian Rosebrock March 31, 2017 at 1:50 pm #

      Try running sudo apt-get update to refresh your package definitions. I also have an updated guide for Ubuntu 16.04 that you should take a look at.

  132. Miguel P Xochicale April 19, 2017 at 12:29 pm #

    Dear Adrian,

    Following your instructions. I got OpenCV ‘3.2.0’ working with Python 2.7.13 on Ubuntu 14.04 x64. Now, I am ready to devour your tutorial of facial landmarks with dlib, OpenCV, and Python. Thank you very much!

    • Adrian Rosebrock April 19, 2017 at 12:41 pm #

      Congrats on getting OpenCV installed Miguel, nice job!

  133. Sid May 2, 2017 at 2:27 am #

    Hi Adrian! Thanks a lot for all the tutorials. I am unable to import cv2 as numpy isn’t installed in the environment. However I cannot pip install due to non-availability of an internet connection due to some reasons. Is there any other way to install it? I can access internet via some other laptop but not this one (OS: Ubuntu). For example if I can download the required whl file and then transfer it to this laptop in the required directory. But how to do that, I don’t have any idea.

    • Adrian Rosebrock May 3, 2017 at 5:55 pm #

      Can you clone the GitHub repo for NumPy from your laptop, save it to a thumb drive, and then copy the repo on to your system? From there you can run python install to install NumPy.

  134. Charushila May 3, 2017 at 6:29 am #

    I am getting following error for python

    python: can’t open file ‘’: [Errno 2] No such file or directory

    • Adrian Rosebrock May 3, 2017 at 5:36 pm #

      Make sure you use the “Downloads” section of this blog post to download the code. Then unzip the file, change directory, and execute the script.

  135. Vaibhav May 14, 2017 at 10:56 am #

    Whenever i write “make -j4” it shows make: *** No targets specified and no makefile found. Stop.

    Please help!

    • Adrian Rosebrock May 15, 2017 at 8:38 am #

      If you are getting an error related to no Makefile being found, then CMake must have excited with an error. Go back to the CMake step and ensure there are no errors during the configuration.

  136. Sandy May 17, 2017 at 1:58 am #

    Hi Adrin,

    Its a very nice tutorial for biggners ike me…. it helped me alot foe installing and configuring opencv and python on my ubuntu 12.04 pc….

    Now the issue is when i execute a program i am getting a part of output (window size is small) insted of detecting the right portion of the image, followed by drawing a green bounding box surrounding it. The output window is covering only the1/4 of input image instaed of total image.

    Even for video related programs I am getting a small window instead of getting full window.

    so, how to rectify this issue…



    • Adrian Rosebrock May 17, 2017 at 9:48 am #

      Hi Sandy — that sounds like a very strange error indeed. Do you have a screenshot of the error that you can share? I’m not sure I fully understand the exact issue.

      • Sandy May 22, 2017 at 3:42 am #

        Hi Adrin, please check your mail for the screenshot of the error what i am getting when i execute



  137. mohammad May 26, 2017 at 10:38 am #

    I am a new gay in opencv and learn very helpful training from your site.
    I’m read before you suggest better to start with virtual box and download ubuntu with configured opencv and python on it.
    please send me the link of this image file.
    Best regards

    • Adrian Rosebrock May 28, 2017 at 1:08 am #

      Hi Mohammad — the Ubuntu VirtualBox VM you are referring to is part of the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV.

  138. matheus macedo May 30, 2017 at 12:34 am #

    you save my life, ty

  139. Yanxiang June 15, 2017 at 4:01 am #

    Hi Adrian,

    Thanks for your guidance. It’s very useful. One thing I’d like to know, why not using “pip install opencv-python” directly? It seems to be much easier since you don’t have to “make”. What’s the difference between your steps and the way I mentioned?
    I am quite new to area so hope to get your answer. Thanks.

    • Adrian Rosebrock June 16, 2017 at 11:21 am #

      There are two reasons why I don’t recommend using the opencv-python package directly:

      1. The opencv_contrib modules are not included.
      2. There is no GUI support with opencv-python, so you can’t use functions such as cv2.imshow

  140. Lee June 22, 2017 at 3:31 pm #

    Thank you.

  141. Lucas Alves July 5, 2017 at 6:07 pm #

    The installation came to a happy end, but as I copied the find_game script and run, got this message:

    “The lower bounary is neither an array of the same size and same type as src, nor a scalar function inRange.”

    • Adrian Rosebrock July 7, 2017 at 10:03 am #

      Instead of copy and pasting the code, use the “Downloads” section of this blog post to download the source code + example image.

  142. Ali July 15, 2017 at 9:31 am #

    Hi Adrian. I need your help. When I run the command ‘workon cv’, it says that : Error: deactivate must be sourced. Run ‘source deactivate’ instead of ‘deactivate’.

    But I followed the instructions. Lastly, I can work import cv2 in the terminal, but when I try it in Idle it does not work. Any help would be appreciated. Thank you..

    • Adrian Rosebrock July 18, 2017 at 10:09 am #

      Regarding your first error message, I’m not sure what the exact problem is. It sounds like you may have (somehow) introduced strange aliases to the deactivate and workon commands. I would suggest re-installing from scratch on a fresh Ubuntu system if you can.

      Secondly, I would also recommend that you take a look at the Quickstart Bundle and Hardcopy Bundle of my book, Practical Python and OpenCV which includes a pre-configured Ubuntu VM with OpenCV pre-installed. This is by far the fastest way to get up and running with OpenCV + Python.

  143. Eric C. Gustav July 15, 2017 at 10:31 pm #

    Thankss Adrian for a great tutorial!!!

    I have one comment to add. The latest version of OpenCV is 3.2.0 which is great and all, but it has some CMAKE issues with openBLAS, and LAPACK. The current work-around is to add “-D WITH_LAPACK=OFF”, which did the trick for me.

  144. EKELE July 26, 2017 at 5:34 am #

    I have installed matplotlib however, I am not able to work in the virtual environment with matplotlib and opencv python. Please can you help me

    • Adrian Rosebrock July 28, 2017 at 10:03 am #

      It sounds like you installed matplotlib into your global Python site-packages and not the site-packages for your “cv” virtual environment. Make sure you install and Python packages you wish to interact with OpenCV into the “cv” virtual environment:

  145. Kodur Krishna Chaitanya August 3, 2017 at 2:07 am #

    Hey Adrian! great tutorial here! you have included -D CMAKE_INSTALL_PREFIX=/usr/local in the cmake command. But by default opencv is installed /usr/local so there is no need of mentioning it.
    # Set the install prefix
    SET(CMAKE_INSTALL_PREFIX “/usr/local”)

  146. Madhu Oruganti August 23, 2017 at 6:18 am #

    Dear Adrain,
    How to use run length coding in python opencv.
    ImportError: No module named runlength
    I am getting Error

    • Adrian Rosebrock August 23, 2017 at 3:40 pm #

      Hi Madhu — I have not personally encountered that error before. How are you trying to import the OpenCV library?

  147. Herleeyandi September 7, 2017 at 8:22 am #

    Hello, I am trying to install opencv 3.3.0 with opencv contrib in my linux 15.04 in both python 2.7 and 3.4. It has been successfully installed. However when I am doing “import cv2” it return as “segmentation fault(core dumped)”. I think it can because conflict in python 2 and 3. So how can I fix it?, suppose that I just want opencv in my python 2.

    • Adrian Rosebrock September 11, 2017 at 9:33 am #

      This sounds like you compiled OpenCV + Python bindings for a different Python version than the one you are trying to import it into. Double-check your output from CMake and ensure the Python bindings to be compiled match the Python version you are trying to import them into.

  148. itzco September 8, 2017 at 5:01 am #

    Anaconda + Linux

    If you already have anaconda, use it instead to create / activate workspaces:

    Step 8:

    – Create: conda create –name cv python=2.7
    – Activate: source activate cv

    Step 11 will not work, 2 possible causes.
    1) The is not where it should. (this wasn’t my case) but you can search the file with:
    sudo find /usr -name “”

    2) Find the location of conda environment
    conda info –envs

    cd (result of 2)
    # This should work now:
    ln -s /usr/local/lib/python2.7/dist-packages/

    ^ If the file is not in there then replace:
    ln -s (result of 1)

    After that everything was ok

  149. Andrea October 4, 2017 at 5:54 am #

    Hi, just did it! I think i didnt execute perfectly all the steps and found the same error as other people when i was to import in python! Searching in the folders i noticed that wasnt in ‘site-packages’ folder but in the ‘dist-packages’ one. Basically i think i messed up using the virtual environment due tu i was following the ubuntu 16.04 guide at first then switched to this. I was able to import and run correctly the example by avoiding the virtual environment.

    I am new to linux, hope i wont find any problems in future cause of my imperfect installation procedure.

    Thank you!

    • Adrian Rosebrock October 4, 2017 at 12:33 pm #

      Congrats on getting OpenCV installed, Andrea! Nice job. Getting OpenCV installed is not an easy task, especially for someone new to Linux. You should be very proud of yourself!

  150. Harshit November 6, 2017 at 11:10 am #

    Hi, Adrian
    Very good article on OpenCV installation. I have followed all of your steps and it’s working perfectly fine in my virtual environment. But, I wanted to use Jupyter Notebook for writing my python codes for my research work and I am facing issues to import cv2 inside Jupyter notebook.

    Can you please help me how to import cv2 inside Jupyter notebook. As far as I know, I am facing problem because of sudo access given in your blog for my virtual environment.

    I am trying this since October but couldn’t figure out. Any help would be really appreciated.
    I have tried this page for setting up my Jupyter notebook and I am able to make new kernel for opencv but it doesn’t import opencv.

    • Adrian Rosebrock November 6, 2017 at 4:23 pm #

      I assume you are using Python virtual environments for your OpenCV install? If so, just install Jupyter Notebooks into your Python virtual environment (the same one that has OpenCV installed):

      And then launch your notebook:

      While you are in the “cv” virtual environment. You’ll be good to go from there.

  151. Salvador Blanco November 23, 2017 at 1:57 am #

    When I execute the $ sudo ldconfig command I get the following error:

    /sbin/ldconfig.real: /usr/lib/nvidia-384/ is not a symbolic link

    /sbin/ldconfig.real: /usr/lib32/nvidia-384/ is not a symbolic link

  152. Salvador Blanco November 23, 2017 at 2:31 am #

    Now I have another problem

    I am finally able to run open-CV, but only inside the environment.
    How can I run it outside the environment ?

    • Adrian Rosebrock November 25, 2017 at 12:37 pm #

      Hi Salvador — is there a particular reason you would like to not use the Python virtual environment? Some additional context here would absolutely help.

  153. Jacqueline November 24, 2017 at 5:27 pm #

    I also have another question. So when I turn off my VirtualBox I have to re-run these lines to be able to run ‘workon’:

    export WORKON_HOME=$HOME/.virtualenvs
    source /usr/local/bin/

    If I don’t re-run this, I get an error and my terminal does not recognize ‘workon’

    • Adrian Rosebrock November 25, 2017 at 12:18 pm #

      Hi Jacqueline — I would suggest taking a look at the ~/.bashrc file in the home directory of your Ubuntu VM. It sounds like you may have accidentally forgotten to include:

      In the .bashrc file which would resolve the issue. Definitely check the file!

      • Jacqueline Garcia November 28, 2017 at 8:05 pm #


        I did that line when I was trying to get everything to work. It works the first time, but when I turn it off it almost forgets that I have done that. Does that command not write that to the .bashrc file?


        • Adrian Rosebrock November 30, 2017 at 3:42 pm #

          Indeed, it sounds like your .bashrc file was not updated properly. Open the file again and confirm that those lines have been added.

  154. Jacqueline Garcia November 28, 2017 at 8:02 pm #

    Hello Adrian,

    I am working on virtual box (from my macbook pro), trying to do your Real time Object detection. For some reason, I cannot get to work because it is not detecting my Mac camera. I’ve searched the web tirelessly with no solution to this. Do you happen to know how to fix this issue?


    • Adrian Rosebrock November 30, 2017 at 3:44 pm #

      By definition, a virtual machine is not allowed to access peripherals on your host machine (i.e., your MacBook Pro). A VM abstracts away hardware and enables you to virtualize an operating system. The downside is that you cannot access your webcam or other peripherals. Some people on the Ubuntu forums have gotten this to work but again, I cannot say whether or not the solution will work in your case.

  155. ajay bolloju December 15, 2017 at 6:30 am #

    Hi Adrian,
    I followed all your mentioned steps of installation. but when I enter command “workon” its saying that command is not found. and in phython when entered “import cv2″ its saying that

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

    can you please help me to fix this.

    • ajay bolloju December 15, 2017 at 7:25 am #

      now I can run “workon” command. but “import cv2” in python is showing error.
      in the path “site-packages” I found with a symbol “” even if I removed it and reinstalled the same thing is happening.

      help me to fix this.

      thank you for the help.

    • ajay bolloju December 15, 2017 at 7:28 am #

      now I can run “workon” command but cv2 error is still exits.
      I found “” in “site-packages” even if I removed and reinstalled it, the same thing is happening.
      can you help me to fix this..
      thank you for the help.

      • Adrian Rosebrock December 15, 2017 at 8:16 am #

        Hi Ajay — please see the “Troubleshooting and FAQ” section of this blog post. The likely issue here is that you have OpenCV installed, but it’s not properly sym-linked into the “cv” Python virtual environment. Please see the post I linked to for more details.

  156. Zhenglin Li February 6, 2018 at 12:02 pm #

    Hi Adrian, thanks a lot for your brilliant tutorial. I have successfully it installed on my ubuntu. But now I’d like to use opencv together with C as well. Is it possible? I set the search directories in codeblocks as ” /usr/local/lib/python2.7/site-packages” and “/usr/local/include” but it still doesn’t work. I think it may be related to the virtual envirionment but I have no idea how to fix it. Could you please help to give some advice? Many thanks.

    • Adrian Rosebrock February 8, 2018 at 8:48 am #

      The OpenCV C/C++ libraries should be installed on your system now. The Python virtual environment has nothing to do with the C/C++ libraries, they are installed independently. Take a look at this post in the official OpenCV forums to help you get started.

      • Zhenglin Li February 11, 2018 at 3:27 pm #

        Hi Adrian, thank you so much for your helpful advice. It works well with codeblocks now 😀

        • Adrian Rosebrock February 12, 2018 at 6:19 pm #

          Awesome, I’m glad to hear it 🙂

  157. pooja June 9, 2018 at 12:07 pm #

    Hey thankyou for great tutorial
    In workon cv step i m getting this error
    workon:command not found
    please help

    • Adrian Rosebrock June 13, 2018 at 6:02 am #

      Try reading the “Trouble shooting your install (FAQ)” section of this blog post as it should help you resolve the issue.

  158. Arezoo June 17, 2018 at 9:50 am #

    I installed opencv 3.3.1 on python3.5 and ubuntu 16.04. I did not install OpenCV through the repositories and I compiled it myself. how can I update opencv to the last version?

    • Adrian Rosebrock June 19, 2018 at 8:53 am #

      You will need to compile and install OpenCV from source for whatever version you would like to update to.

  159. Akshay Chaturvedi September 20, 2018 at 2:23 am #

    Hi Adrian, thanks for the tutorial,
    i am already using opencv3.2 on ubuntu 16.04,is there a way so that i can upgrade opencv ver 3.2 to 3.4.1 or better.

  160. Krishna Thali December 20, 2018 at 12:16 am #

    Hey Adrian Rosebrock, I followed the same procedure and I am not able to find anywhere in the machine. Command I used to find is sudo find / -name, as a result, I didnt get anything. Please help me fixing this issue.

  161. Vishnucharan March 20, 2019 at 9:36 am #

    First of all nice tutorial, I have made everything as you mentioned. But when i run import cv it shows error no module name cv

    • Adrian Rosebrock March 22, 2019 at 8:58 am #

      I would instead suggest you follow Updated OpenCV + Ubuntu install guide. That tutorial should help you more.

      • Vishnucharan April 4, 2019 at 7:30 am #

        I got that on day after i messaged.Thank you for your kind reply. I have a doubt now I want to use opencv in C++, can i use this same cv environment to run that? I had run the image stitching code and got error like follow . What should i do now? or else please suggest me any tutorial.

        • Adrian Rosebrock April 4, 2019 at 1:02 pm #

          The “cv” virtual environment is for Python only. You can use gcc/g++ to build and link your C++ code.


  1. Install OpenCV 3.0 and Python 3 on Ubuntu - PyImageSearch - July 20, 2015

    […] A couple weeks ago I provided step-by-step install instructions to setup OpenCV 3.0 and Python 2.7+ on your Ubuntu machine. […]

  2. Ubuntu 16.04: How to install OpenCV - PyImageSearch - October 24, 2016

    […] Install OpenCV 3.0 and Python 2.7+ on Ubuntu 14.04 […]

Before you leave a comment...

Hey, Adrian here, author of the PyImageSearch blog. I'd love to hear from you, but before you submit a comment, please follow these guidelines:

  1. If you have a question, read the comments first. You should also search this page (i.e., ctrl + f) for keywords related to your question. It's likely that I have already addressed your question in the comments.
  2. If you are copying and pasting code/terminal output, please don't. Reviewing another programmers’ code is a very time consuming and tedious task, and due to the volume of emails and contact requests I receive, I simply cannot do it.
  3. Be respectful of the space. I put a lot of my own personal time into creating these free weekly tutorials. On average, each tutorial takes me 15-20 hours to put together. I love offering these guides to you and I take pride in the content I create. Therefore, I will not approve comments that include large code blocks/terminal output as it destroys the formatting of the page. Kindly be respectful of this space.
  4. Be patient. I receive 200+ comments and emails per day. Due to spam, and my desire to personally answer as many questions as I can, I hand moderate all new comments (typically once per week). I try to answer as many questions as I can, but I'm only one person. Please don't be offended if I cannot get to your question
  5. Do you need priority support? Consider purchasing one of my books and courses. I place customer questions and emails in a separate, special priority queue and answer them first. If you are a customer of mine you will receive a guaranteed response from me. If there's any time left over, I focus on the community at large and attempt to answer as many of those questions as I possibly can.

Thank you for keeping these guidelines in mind before submitting your comment.

Leave a Reply