Ubuntu 16.04: How to install OpenCV


Over the past two years running the PyImageSearch blog, I’ve authored two tutorials detailing the required steps to install OpenCV (with Python bindings) on Ubuntu. You can find the two tutorials here:

However, with support of Ubuntu 14.04 winding down and Ubuntu 16.04 set as the next LTS (with support until April 2021), I thought it would be appropriate to create a new, updated Ubuntu + OpenCV install tutorial.

Inside this tutorial, I will document, demonstrate, and provide detailed steps to install OpenCV 3 on Ubuntu 16.04 with either Python 2.7 or Python 3.5 bindings.

Furthermore, this document has been fully updated from my previous Ubuntu 14.04 tutorials to use the latest, updated packages from the apt-get  repository.

To learn how to install OpenCV on your Ubuntu 16.04 system, keep reading.

Note: Don’t care about Python bindings and simply want OpenCV installed on your system (likely for C++ coding)? No worries, this tutorial will still work for you. Follow along with the instructions and perform the steps — by the end of this article you’ll have OpenCV installed on your system. From there, just ignore the Python bindings and proceed as usual.

Ubuntu 16.04: How to install OpenCV

Before we get into this tutorial, I want to mention that Ubuntu 16.04 actually ships out-of-the-box with both Python 2.7 and Python 3.5 installed. The actual versions (as of 24 October 2016) are:

  • Python 2.7.12 (used by default when you type python  in your terminal).
  • Python 3.5.2 (can be accessed via the python3  command).

Again, it’s worth repeating that Python 2.7 is still the default Python version used by Ubuntu. There are plans to migrate to Python 3 and use Python 3 by default; however, as far as I can tell, we are still a long way from that actually becoming a reality.

In either case, this tutorial will support both Python 2.7 and Python 3. I’ve highlighted the steps that require you to make a decision regarding which version of Python you would like to use. Make sure you are consistent with your decision, otherwise you will inevitably run into compile, linking, and import errors.

Regarding which Python version you should use…I’m not getting into that argument. I’ll simply say that you should use whichever version of Python you are comfortable with and use on a daily basis. Keep in mind that Python 3 is the future — but also keep in mind that porting Python 2.7 code to Python 3 isn’t terribly challenging either once you understand the differences between the Python versions. And as far as OpenCV goes, OpenCV 3 doesn’t care which version of Python you’re using: the bindings will work just the same.

All that said, let’s get started installing OpenCV with Python bindings on Ubuntu 16.04.

Step #1: Install OpenCV dependencies on Ubuntu 16.04

Most (in fact, all) steps in this tutorial will be accomplished by using your terminal. To start, open up your command line and update the apt-get  package manager to refresh and upgrade and pre-installed packages/libraries:

Next, let’s install some developer tools:

The pkg-config  package is (very likely) already installed on your system, but be sure to include it in the above apt-get  command just in case. The cmake  program is used to automatically configure our OpenCV build.

OpenCV is an image processing and computer vision library. Therefore, OpenCV needs to be able to load various image file formats from disk such as JPEG, PNG, TIFF, etc. In order to load these images from disk, OpenCV actually calls other image I/O libraries that actually facilitate the loading and decoding process. We install the necessary ones below:

Okay, so now we have libraries to load images from disk — but what about video? Use the following commands to install packages used to process video streams and access frames from cameras:

OpenCV ships out-of-the-box with a very limited set of GUI tools. These GUI tools allow us to display an image to our screen ( cv2.imshow ), wait for/record keypresses ( cv2.waitKey ), track mouse events ( cv2.setMouseCallback ), and create simple GUI elements such as sliders and trackbars. Again, you shouldn’t expect to be building full-fledged GUI applications with OpenCV — these are just simple tools that allow you to debug your code and build very simple applications.

Internally, the name of the module that handles OpenCV GUI operations is highgui . The highgui  module relies on the GTK library, which you should install using the following command:

Next, we install libraries that are used to optimize various functionalities inside OpenCV, such as matrix operations:

We’ll wrap up Step #1 by installing the Python development headers and libraries for both Python 2.7 and Python 3.5 (that way you have both):

Note: If you do not install the Python development headers and static library, you’ll run into issues during Step #4 where we run cmake  to configure our build. If these headers are not installed, then the cmake  command will be unable to automatically determine the proper values of the Python interpreter and Python libraries. In short, the output of this section will look “empty” and you will not be able to build the Python bindings. When you get to Step #4, take the time to compare your output of the command to mine.

Step #2: Download the OpenCV source

At the time of this article’s publication, the most recent version of OpenCV is 3.1.0 , which we download a .zip  of and unarchive using the following commands:

When new versions of OpenCV are released you can check the official OpenCV GitHub and downloaded the latest release by changing the version number of the .zip .

However, we’re not done downloading source code yet — we also need the opencv_contrib repository as well:

Why are we bothering to download the contrib repo as well?

Well, we want the full install of OpenCV 3 to have access to features (no pun intended) such as SIFT and SURF. In OpenCV 2.4, SIFT and SURF were included in the default installation of OpenCV. However, with the release of OpenCV 3+, these packages have been moved to contrib, which houses either (1) modules that are currently in development or (2) modules that are marked as “non-free” (i.e., patented). You can learn more about the reasoning behind the SIFT/SURF restructuring in this blog post.

Note: You might need to expand the commands above using the “<=>” button during your copy and paste. The .zip  in the 3.1.0.zip  may be cutoff in smaller browser windows. For convenience, I have included the full URL of both the opencv  archive as well as the opencv_contrib  archive below:

I also want to mention that both your opencv  and opencv_contrib  versions should be the same (in this case, 3.1.0 ). If the versions numbers do not matchup, you could very easily run into compile time errors (or worse, runtime errors that are near impossible to debug).

Step #3: Setup your Python environment — Python 2.7 or Python 3

We are now ready to start configuring our Python development environment for the build. The first step is to install pip , a Python package manager:

I’ve mentioned this in every single OpenCV + Python install tutorial I’ve ever done, but I’ll say it again here today: I’m a huge fan of both virtualenv and virtualenvwrapper. These Python packages allow you to create separate, independent Python environments for each project that you are working on.

In short, using these packages allows you to solve the “Project X depends on version 1.x, but Project Y needs 4.x dilemma. A fantastic side effect of using Python virtual environments is that you can keep your system Python neat, tidy, and free from clutter.

While you can certainly install OpenCV with Python bindings without Python virtual environments, I highly recommend you use them as other PyImageSearch tutorials leverage Python virtual environments. I’ll also be assuming that you have both virtualenv  and virtualenvwrapper  installed throughout the remainder of this guide.

If you would like a full, detailed explanation on why Python virtual environments are a best practice, you should absolutely give this excellent blog post on RealPython a read. I also provide some commentary on why I personally prefer Python virtual environments in the first half of this tutorial.

Again, let me reiterate that it’s standard practice in the Python community to be leveraging virtual environments of some sort, so I suggest you do the same:

Once we have virtualenv  and virtualenvwrapper  installed, we need to update our ~/.bashrc  file to include the following lines at the bottom of the file:

The ~/.bashrc  file is simply a shell script that Bash runs whenever you launch a new terminal. You normally use this file to set various configurations. In this case, we are setting an environment variable called WORKON_HOME  to point to the directory where our Python virtual environments live. We then load any necessary configurations from virtualenvwrapper .

To update your ~/.bashrc  file simply use a standard text editor. I would recommend using nano , vim , or emacs . You can also use graphical editors as well, but if you’re just getting started, nano  is likely the easiest to operate.

A more simple solution is to use the cat  command and avoid editors entirely:

After editing our ~/.bashrc  file, we need to reload the changes:

Note: Calling source  on .bashrc  only has to be done once for our current shell session. Anytime we open up a new terminal, the contents of .bashrc  will be automatically executed (including our updates).

Now that we have installed virtualenv  and virtualenvwrapper , the next step is to actually create the Python virtual environment — we do this using the mkvirtualenv  command.

But before executing this command, you need to make a choice: Do you want to use Python 2.7 or Python 3?

The outcome of your choice will determine which command you run in the following section.

Creating your Python virtual environment

If you decide to use Python 2.7, use the following command to create a Python 2.7 virtual environment:

Otherwise, use this command to create a Python 3 virtual environment:

Regardless of which Python command you decide to use, the end result is that we have created a Python virtual environment named cv  (short for “computer vision”).

You can name this virtual environment whatever you like (and create as many Python virtual environments as you want), but for the time bing, I would suggest sticking with the cv  name as that is what I’ll be using throughout the rest of this tutorial.

Verifying that you are in the “cv” virtual environment

If you ever reboot your Ubuntu system; log out and log back in; or open up a new terminal, you’ll need to use the workon  command to re-access your cv  virtual environment. An example of the workon  command follows:

To validate that you are in the cv  virtual environment, simply examine your command line — if you see the text (cv)  preceding your prompt, then you are in the cv  virtual environment:

Figure 1: Make sure you see the "(cv)" text on your prompt, indicating that you are in the cv virtual environment.

Figure 1: Make sure you see the “(cv)” text on your prompt, indicating that you are in the cv virtual environment.

Otherwise, if you do not see the cv  text, then you are not in the cv  virtual environment:

Figure 2: If you do not see the "(cv)" text on your prompt, then you are not in the cv virtual environment and need to run the "workon" command to resolve this issue. 

Figure 2: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment and need to run the “workon” command to resolve this issue.

To access the cv  virtual environment simply use the workon  command mentioned above.

Install NumPy into your Python virtual environment

The final step before we compile OpenCV is to install NumPy, a Python package used for numerical processing. To install NumPy, ensure you are in the cv  virtual environment (otherwise NumPy will be installed into the system version of Python rather than the cv  environment). From there execute the following command:

Step #4: Configuring and compiling OpenCV on Ubuntu 16.04

At this point, all of our necessary prerequisites have been installed — we are now ready to compile and OpenCV!

But before we do that, double-check that you are in the cv  virtual environment by examining your prompt (you should see the (cv)  text preceding it), and if not, use the workon  command:

After ensuring you are in the cv  virtual environment, we can setup and configure our build using CMake:

The above commands change directory to ~/opencv-3.1.0 , which if you have been following this tutorial is where you downloaded and unarchived the .zip  files.

Note: If you are getting an error related to stdlib.h: No such file or directory during either the cmake  or make  phase of this tutorial you’ll also need to include the following option to CMake: -D ENABLE_PRECOMPILED_HEADERS=OFF . In this case I would suggest deleting your build  directory, re-creating it, and then re-running CMake with the above option included. This will resolve the stdlib.h  error. Thank you to Carter Cherry and Marcin for pointing this out in the comments section!

Inside this directory we create a sub-directory named build  and change into it. The build  directory is where the actual compile is going to take place.

Finally, we execute cmake  to configure our build.

Before we move on to the actual compilation of OpenCV, make sure you examine the output of CMake!

To do this, scroll down the section titled Python 2  and Python 3 .

If you are compiling OpenCV on Ubuntu 16.04 with Python 2.7 support, make sure the Python 2  section includes valid paths to the Interpreter , Libraries , numpy , and packages path . Your output should be similar to mine below:

Figure 3: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for Ubuntu 16.04.

Figure 3: Ensuring that Python 2.7 will be used when compiling OpenCV 3 for Ubuntu 16.04.

Examining this output, you can see that:

  1. The Interpreter  points to the Python 2.7 binary in the cv  virtual environment.
  2. Libraries  points to the Python 2.7 library (which we installed during the final step of Step #1).
  3. The numpy  value points to our NumPy installation in the cv  virtual environment.
  4. And finally, the packages path  points to lib/python2.7/site-packages . When combined with the CMAKE_INSTALL_PREFIX , this means that after compiling OpenCV, we’ll find our cv2.so  bindings in /usr/local/lib/python2.7/site-packages/ .

Similarly, if you’re compiling OpenCV 16.04 with Python 3 support, you’ll want to ensure your Python 3  section looks similar to mine below:

Figure 4: Validating that Python 3 will be used when compiling OpenCV 3 for Ubuntu 16.04.

Figure 4: Validating that Python 3 will be used when compiling OpenCV 3 for Ubuntu 16.04.

Again, notice how my Interpreter , Libraries , numpy  and packages path  have all been correctly set.

If you do not see the cv  virtual environments in these variable paths, it’s almost certainly because you are NOT in the cv  virtual environment prior to running CMake!

If that is indeed the case, simply access the cv  virtual environment by calling workon cv  and re-run the CMake command mentioned above.

Assuming your CMake command exited without any errors, you can now compile OpenCV:

The -j  switch controls the number of processes to be used when compiling OpenCV — you’ll want to set this value to the number of processors/cores on your machine. In my case, I have a quad-core processor, so I set -j4 .

Using multiple processes allows OpenCV to compile faster; however, there are times where race conditions are hit and the compile bombs out. While you can’t really tell if this is the case without a lot of previous experience compiling OpenCV, if you do run into an error, my first suggestion would be to run make clean  to flush the build, followed by compiling using only a single core:

Below you can find a screenshot of a successful OpenCV + Python compile on Ubuntu 16.04:

Figure 5: Successfully compiling OpenCV 3 for Ubuntu 16.04.

Figure 5: Successfully compiling OpenCV 3 for Ubuntu 16.04.

The last step is to actually install OpenCV 3 on Ubuntu 16.04:

Step #5: Finish your OpenCV install

You’re coming down the home stretch, just a few more steps to go and your Ubuntu 16.04 system will be all setup with OpenCV 3.

For Python 2.7:

After running sudo make install , your Python 2.7 bindings for OpenCV 3 should now be located in /usr/local/lib/python-2.7/site-packages/ . You can verify this using the ls  command:

Note: In some cases, you may find that OpenCV was installed in /usr/local/lib/python2.7/dist-packages  rather than /usr/local/lib/python2.7/site-packages  (note dist-packages  versus site-packages ). If your cv2.so  bindings are not in the site-packages  directory, be sure to check dist-pakages .

The final step is to sym-link our OpenCV cv2.so  bindings into our cv  virtual environment for Python 2.7:

For Python 3.5:

After running sudo make install , your OpenCV + Python 3 bindings should be located in /usr/local/lib/python3.5/site-packages/ . Again, you can verify this using the ls  command:

I’ve been puzzled regarding this behavior ever since OpenCV 3 was released, but for some reason, when compiling OpenCV with Python 3 support, the output cv2.so  filename is different. The actual filename might vary for you, but it should look something similar to cv2.cpython-35m-x86_64-linux-gnu.so .

Again, I have no idea exactly why this happens, but it’s a very easy fix. All we need to do is rename the file:

After renaming cv2.cpython-35m-x86_64-linux-gnu.so  to simply cv2.so , we can sym-link our OpenCV bindings into the cv  virtual environment for Python 3.5:

Step #6: Testing your OpenCV install

Congratulations, you now have OpenCV 3 installed on your Ubuntu 16.04 system!

To verify that your installation is working:

  1. Open up a new terminal.
  2. Execute the workon  command to access the cv  Python virtual environment.
  3. Attempt to import the Python + OpenCV bindings.

I have demonstrated how to perform these steps below:

As you can see, I can import my OpenCV bindings into my Python 3.5 shell.

Below follows a screenshot of me utilizing the same steps outlined in this tutorial and importing OpenCV bindings into a Python 2.7 shell:

Figure 6: Ensuring that I can successfully import my Python + OpenCV bindings on Ubuntu 16.04.

Figure 6: Ensuring that I can successfully import my Python + OpenCV bindings on Ubuntu 16.04.

Thus, regardless of which Python version you decide to use, simply follow the steps detailed in this tutorial and you’ll be able to install OpenCV + Python on your Ubuntu 16.04 system.

Once OpenCV has been installed, you can delete both the opencv-3.1.0  and opencv_contrib-3.1.0  directories (along with their associated .zip  files):

But again, be careful when running this command! You’ll want to make sure you have properly installed OpenCV on your system prior to blowing along these directories. Otherwise, you’ll need to restart the entire compile process!

Troubleshooting and FAQ

In this section, I address some of the common questions, problems, and issues that arise when installing OpenCV on Ubuntu 16.04.

Q. When I execute mkvirtualenv  or workon , I get a “command not found error”.

A. There are three primary reasons why you would be getting this error message, all of which are related to Step #3:

  1. First, make sure you have installed virtualenv  and virtualenvwrapper  using the pip  package manager. You can verify this by running pip freeze , examining the output, and ensuring that you see both virtualenv  and virtualenvwrapper  in the list of installed packages.
  2. Your ~/.bashrc  file may not be updated correctly. To diagnose this, use a text editor such as nano  and view the contents of your ~/.bashrc  file. At the bottom of the file, you should see the proper export  and source  commands are present (again, check Step #3 for the commands that should be appended to ~/.bashrc ).
  3. After editing your ~/.bashrc  file, you may have forgotten to source  it and reload its contents. Make sure you run source ~/.bashrc  after editing it to ensure the contents are reloaded — this will give you access to the mkvirtualenv  and workon  commands.

Q. Whenever I open a new terminal, logout, or reboot my Ubuntu system, I cannot execute the mkvirtualenv  or workon  commands.

A. See reason #2 from the previous question.

Q. When I (1) open up a Python shell that imports OpenCV or (2) execute a Python script that calls OpenCV, I get an error: Import Error: No module named cv2 .

A. Unfortunately, the exact cause of this error message is extremely hard to diagnose as there are multiple reasons this could be happening. In general, I recommend the following suggestions to help diagnose and resolve the error:

  1. Make sure you are in the cv  virtual environment by using the workon cv  command. If this command gives you an error, then see the first question in this FAQ.
  2. If after you’ve ensured your ~/.bashrc  file has been updated properly and source ‘d, then try investigating the contents of the site-packages  directory in your cv  virtual environment. You can find the site-packages  directory in ~/.virtualenvs/cv/lib/python2.7/site-packages/  or ~/.virtualenvs/cv/lib/python3.5/site-packages/  depending on your Python version. Make sure that (1) there is a cv2.so  file in this site-packages  directory and (2) that it’s properly sym-linked to a valid, existing file.
  3. Be sure to check the site-packages  (and even dist-packages ) directory for the system install of Python located in /usr/local/lib/python2.7/site-packages/  and /usr/local/lib/python3.5/site-packages/ , respectively. Ideally, you should have a cv2.so  file there.
  4. If all else fails, check in your build/lib  directory of your OpenCV build. There should be a cv2.so  file there (provided that both cmake  and make  executed without error). If the cv2.so  file is present, manually copy it into both the system site-packages  directory as well as the site-packages  directory for the cv  virtual environment.

So, what’s next?

Congrats! You now have a brand new, fresh install of OpenCV on your Ubuntu 16.04 system — and I’m sure you’re just itching to leverage your 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 probably feeling a bit confused and overwhelmed on exactly where to start.

Personally, I’m a big fan of learning by example, so a good first step would be to have some fun and read this blog post on detecting cats in images/videos. This tutorial is meant to be very hands-on and demonstrate how you can (quickly) build a Python + OpenCV application to detect the presence of cats in images.

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 streamsobject tracking in video, and handwriting recognition.


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


In today’s blog post, I demonstrated how to install OpenCV 3 with either Python 2.7 or Python 3 bindings on your Ubuntu 16.04 system.

For more OpenCV install tutorials on other operating systems (such as OSX, Raspbian, etc.), please refer to this page where I provide additional links and resources.

But before you go…

If you’re interested in learning more about OpenCV, computer vision, and image processing, be sure to enter your email address in the form below to be notified when new blog posts are published!

, , , ,

616 Responses to Ubuntu 16.04: How to install OpenCV

  1. nico October 24, 2016 at 10:30 am #

    Thanks Adrian! This is my to-go site for installing openCV.

    I ran into a “404 not found” when trying to download opencv_contrib using the link indicated in the tutorial. How can I fix this?

    I was missing the .zip at the end, my bad!

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

      Congrats on resolving the issue Nico!

      • sulthana February 15, 2017 at 8:17 pm #

        hi adrian.. wil this work for ubuntu 14.04? mine is 14.04. thank you

        • Adrian Rosebrock February 16, 2017 at 9:50 am #

          For Ubuntu 14.04, I would recommend this tutorial.

    • Alberto Castillo December 5, 2017 at 9:27 am #

      Adrian, thanks a lot. It was really helpful

  2. Jaime Lopez October 24, 2016 at 11:28 am #

    Hi Adrian,

    Thank you very much for share this steps. I am using openCV 3.0 on windows 10 and python 2.7, but some times I have had some problems when I want to re-installing these.

    So, it is really good to know you share this, as a source in case of need.

    Keep doing that way Adrian, I enjoy following your steps.


  3. Luis October 24, 2016 at 1:15 pm #

    Hey Adrian!, i have installed opencv and python in like 3 or 4 times on a raspberry and odroid and it has worked really good, however i have read about TBB enabling which allow us to run some opencv tools using all the cores in the device. Have you considered to add such feature during the install process? Regards.

    Specially with HAAR cascade training, which have reducing the training time in a very noticeable ratio.

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

      TBB (Threading Building Blocks) can certainly speedup OpenCV. I haven’t included it in this tutorial mainly because these tutorials are aimed at readers just getting started with OpenCV, not those who are trying to milk every last bit of performance out of OpenCV (and are therefore used to reading command line output and can diagnose compile errors). I’ll consider this for a more “advanced build” tutorial.

      • Gabriel January 21, 2019 at 8:03 am #

        Hello Luis!

        What is your OS? Ubuntu Mate?

        I would like to install OpenCV on odroid to make some projects with USB webcam. Can you show me how to install the OS and OpenCV (the links or tutorials that you follow)?

        Thanks a lot!!!

  4. Anselal October 24, 2016 at 4:51 pm #

    Great tutorial Adrian. Maybe I have asked this before but what is the difference between compiling opencv and instaling it via apt-get install python-opencv ?

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

      The python-opencv repository is older, outdated, and doesn’t include the opencv_contrib package. I cannot say this enough — I do not recommend installing OpenCV via apt-get. You’ll be missing features and be running an older, outdated version. Simply put: don’t do it.

      • Steve July 26, 2018 at 11:13 am #

        There’s actually a python-contrib-opencv available through pip that contains both main and contrib modules. https://pypi.org/project/opencv-contrib-python/

        As of right now on Ubuntu 16.04, this gives me cv2.__version__ = 3.4.2, which was only released a few weeks ago as of this writing. So seems pretty up-to-date, and much easier to install than your instructions. Maybe for true beginners this is the way to go.

  5. Cesar October 24, 2016 at 9:39 pm #

    I jump the following error: Configuring incomplete, errors occurred! has something to do that is doing this tutorials are in a virtual machine?

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

      I assume you are referring to the “cmake” command? If so, check the output of cmake — it will tell you what the configuration error is.

    • Bibigun January 28, 2018 at 12:03 pm #

      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \

      Change it to
      -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.4.0/modules \

  6. Jose C October 25, 2016 at 5:47 am #

    Thank you!! working properly with python 3.5.

    Only I have changed this part:

    $ sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so

    By this other:

    $ sudo mv cv2.cpython-35m-i386-linux-gnu.so cv2.so

    Thank you,

  7. Francois Koutchouk October 25, 2016 at 12:30 pm #

    Everything worked up to:
    pip install numpy
    that fails with errors like
    Failed to establish a new connection: [Errno -2] Name or service not known’,)’: /simple/numpy/
    This occurs in both a Python2 or Python3 environment.
    So I did
    pip install scipy and then the pip install numpy worked. Strange, perhaps an improvement on your excellent documentation?

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

      This sounds like a problem with your internet connection. Double-check that you have a strong connection before continuing with the tutorial.

  8. Francois Koutchouk October 25, 2016 at 6:38 pm #

    I think in Step #4 above for cmake it should read

    -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python3 \

    note the 3. After that change all worked just fine.

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

      The “3” is not necessary. When you create the Python virtual environment the virtualenv/virtualenvwrapper commands will automatically sym-link the correct version of Python you chose using mkvirtualenv.

  9. jonathan marcelino October 26, 2016 at 7:07 am #

    Thanks! I have been looking for this. 🙂

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

      No problem Jonathan, happy I could help 🙂

  10. Linus October 26, 2016 at 10:34 am #

    Hey Adrian,
    you’re too late, I’ve managed this just some weeks ago. However, well explained! Do you think it’s worth uninstalling OpenCV and reinstalling it with virtualenvs? Do I have to install OpenCV for every project new?
    Thanks in advance!

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

      I highly recommend using Python virtual environments but they are not a strict requirement. You do not have to install OpenCV for every new project. The only thing you would need to do is sym-link in the cv2.so bindings into the site-packages directory. It’s literally one command that you would have to execute. I would encourage you to use virtual environments but at the end of the day it’s your choice.

      • jd June 20, 2017 at 3:21 am #

        Hi Adrian,

        How do you exactly do this sym-link? I have followed the tutorial and everything was installed succesfully, however, I am trying to use Qt creator to program an OpenCV app and getting the error: “/usr/bin/ld: cannot find -lGL”.

        So, my question is, how do you link the opencv installation to each project?

        By the way, thanks for the tutorial, it is easy to follow and very friendly..

        • Adrian Rosebrock June 20, 2017 at 10:46 am #

          You can read up on sym-links here.

  11. CanAmHack October 26, 2016 at 8:45 pm #

    Not sure if I missed a step but I couldn’t get Python 3.5 installed on Ubuntu 16.04. Python 3.4 installs fine but running:

    sudo apt-get install python2.7-dev python3.5-dev

    returns this error:

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

    • CanAmHack October 28, 2016 at 8:02 am #

      So apparently I got mixed up in my Virtual Box images and I wasn’t actually running Ubuntu 16.04. My bad. I reran on 16.04 and it worked like a charm. Thanks for a fantastic and nicely detailed step-by-step!

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

        Fantastic, I’m happy to hear it worked! 🙂

  12. h3cr0jas October 27, 2016 at 1:29 pm #

    Hi Adrian, I finally have install opencv in a VM.

    Thanks!! and great job!!

    Thanks for sharing your knowledge and experience!!

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

      Congrats on getting OpenCV installed, I’m happy I could help! 🙂

  13. Ehsan Eshaghi October 28, 2016 at 10:50 am #

    Dear Adrian,

    I was wondering how i can uninstall this version of opencv and delete its virtual environment completely. i am using octave to run a code which needs opencv. But octave cant find this virtual environment…

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

      You can delete a virtual environment using rmvirtaulenv. To delete OpenCV from your system simply delete any libopencv* files from /usr/local/lib

  14. nidhi October 28, 2016 at 5:25 pm #


    I installed opencv and opencv_contrib Ubuntu 15.04 using your method and it worked fine.
    But after I restart my system, I couldn’t import cv2 again.

    Do you know what can be the reason?

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

      It sounds like you are not accessing your virtual environment. Don’t forget to use the workon command each time you open up a new terminal:

  15. Marcin Budny October 30, 2016 at 7:54 am #

    Hi Adrian,
    Great article. I followed the instructions in Ubuntu 16.10 and had some issues during the compilation phase – some header files were not found. I turned out that including -DENABLE_PRECOMPILED_HEADERS=OFF in the cmake command helped.

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

      Thanks for sharing Marcin. I personally haven’t ran into this issue but I’ll absolutely make note of it.

      • Dominik L November 3, 2016 at 6:07 am #

        Hey Adrian,
        I think I have a similar problem like Marcin. When i run the make I get a fatal error: hdf5.h: no such file or directory.
        Do you know what the problem could be?

        • Adrian Rosebrock November 3, 2016 at 9:34 am #

          If you are getting an error related to HDF5 then you do not have HDF5 installed on your machine but for some reason the CMake command thinks you do. You can either install HDF5 using:

          $ sudo apt-get install libhdf5-serial-dev

          And then re-run CMake and recompile.

          Another option is to instruct CMake to explicitly compile without HDF5. I can’t remember off the top of my head but I think the switch is -D WITH_HDF5=OFF. You could verify this by running ccmake .. and looking for the HDF5 option.

          • Adrian Montero November 27, 2016 at 8:12 am #

            I got a solution here

            Message from https://github.com/kleinash
            #6016 @avtomatons solution worked for me, add:


            into modules/python/common.cmake

            (I just put it at the bottom)

          • Yogesh Lather December 15, 2016 at 2:29 pm #

            Adrian Montero’s Comment did work!
            I also got the same library error.


          • Dani January 30, 2017 at 5:01 am #

            Yes Adrian Montero’s solution worked also for me with opencv 2.7.

            Thanks all!

          • Riya February 6, 2018 at 10:36 am #

            I can’t openCV on my laptop. it says “hdf5.h: no such file or directory.”. I tried the above solution i.e. $ sudo apt-get install libhdf5-serial-dev, without any luck. I also tried,

            include_directories(${HDF5_INCLUDE_DIRS})” (Although, I am not sure if I have correctly used the code)

            still no luck.

            Please help me

          • Adrian Rosebrock February 6, 2018 at 10:38 am #

            Try deleting your “build” directory, re-creating it, and re-running cmake, this time adding the following flag:

            -D WITH_HDF5=OFF

            Additionally, you may want to consider deleting your “build” directory and re-running cmake now that you have the libhdf5-serial-dev library installed. Sometimes cmake does not pick up on these initial configuration differences if you do not re-run it.

          • Riya February 6, 2018 at 12:28 pm #

            So, I deleted the “build” directory using the command:

            $ rm -rf build

            Re-created it using the command:

            $ mkdir build:

            and re-run the cmake using the command:

            cmake -D CMAKE_BUILD_TYPE=RELEASE

            -D CMAKE_INSTALL_PREFIX=/usr/local
            -D INSTALL_C_EXAMPLES=OFF -DOPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python
            -D BUILD_EXAMPLES=ON ..
            -D WITH_HDF5=OFF

            The error says: “CMake Error: The source directory “/home/raov/opencv-3.1.0/build/WITH_HDF5=OFF” does not exist.”

            So, I moved the following files in build directory:



            and still showing the same error. Is there something I am doing wrong?

          • Adrian Rosebrock February 8, 2018 at 8:47 am #

            You forgot the “..” at the end of the cmake command. You correctly added the “-D WITH_HDF5=OFF” but you need to move the “..” to the end, implying that cmake will run on the next level up directory (i.e., where the source code is).

        • Maciej March 26, 2018 at 3:25 pm #

          For me the answer here: https://stackoverflow.comhttps://stackoverflow.com/a/44912342 worked perfectly.

          • Adrian Rosebrock March 27, 2018 at 6:14 am #

            Awesome, thanks for sharing Maciej 🙂

      • Jonas K September 11, 2017 at 11:08 am #

        Hi Adrian,

        Thanks for the Tutorial! is has worked well until I ran into the same error regarding the stdlib.h: No such file or directory.

        I’m on Ubuntu 17.04 and tried to compile, but unfortunately the solution of Marcin Budny did not work.
        I removed the whole build directory first and rebuild it, also tried both python (2 & 3) environments but did not change anything.

        Do you have any suggestion what I could do?

        • Adrian Rosebrock September 11, 2017 at 2:34 pm #

          You might have missed this line:

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

          The build-essential package is important. Make sure you have it installed, delete your “build” directory, and then re-run CMake and make.

          • Jonas K September 13, 2017 at 3:28 am #

            Thanks for the answer, but the build-essential package was already installed (i double checked). 🙁
            Do you have any other ideas?

            I am trying to compile opencv-3.2.0 if this is of any importance.

          • Adrian Rosebrock September 13, 2017 at 7:04 am #

            The standard development headers should be installed via build-essential. Take a look at this thread on the Ubuntu forums for more information and other possible suggestions.

        • Riya January 17, 2018 at 8:57 pm #

          what is the command to remove the build directory and to rebuild it?

          • Adrian Rosebrock January 18, 2018 at 8:52 am #

            You can remove your “build” directory via:

            $ rm -rf build

            And then re-run the “cmake” command we detailed in the blog post.

    • Carter Cherry November 4, 2016 at 12:39 am #

      Marcin and Adrian,

      cmake compile error In Ubuntu 16.10 installing opencv 3.1.0, python 3.5 :

      /usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: No such file or directory #include_next

      Resolved as Marcin Budny stated by changing cmake command to:

      > -D CMAKE_INSTALL_PREFIX=/usr/local \
      > -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
      > -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \

      Thanks Marcin!! Great tutorial, Adrian!

      • Adrian Rosebrock November 4, 2016 at 9:59 am #

        Thanks for sharing this CMake command Carter! I know a few other PyImageSearch readers were having this same issue as well.

      • soumen February 27, 2017 at 5:50 pm #

        Hello @Adrian Montero,
        Can you please tell me how to add the commands into the common.cmake as I am also getting same error hdf5.sh not found

    • Anjan Das March 22, 2017 at 1:25 am #

      Hey buddy! Where do we include this statement in the command, I mean the position?

    • Mattia October 19, 2017 at 1:03 pm #

      Yeah it was very useful also to me.

    • Juan Torres August 26, 2019 at 5:34 pm #

      Try this
      it works for me

  16. James October 30, 2016 at 10:58 am #

    great guide, but i’ve got to step 3, where im getting the error ‘no module named virtualenvwrapper’. any idea??

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

      Hey James, be sure to check the “Troubleshooting and FAQ” section of this post — you likely forgot to install virtualenv/virtualenvwrapper and/or forgot to update your ~/.bashrc file.

  17. nidhi October 31, 2016 at 6:59 am #


    Thanks for this tutorial.
    Can you please tell how to run this in anaconda spyder. Also i have installed matplotlib but in the virtualenvs its showing an error- ImportError: No module named matplotlib

    Can you help.


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

      I have not used the Sypder IDE before but essentially all you should need to do is configure the IDE to point to the Python interpreter inside the “cv” virtual environment. Exactly which menu choices you need to select I’m not sure since I’m not a Sypder user, but it is certainly possible. As for matlotlib, make sure you install it in the “cv” virtual environment:

    • Bojan Matovski December 5, 2016 at 3:27 pm #

      Hey nidhi,

      I am a spyder user myself.

      Fixed this issue by adding the “~/.virtualenvs/cv/lib/python3.5/site-packages/” directory in the PYTHONPATH manager, which can be found in the menu section above the top Toolbar (in Tools>PYTHONPATH manager).


      • Bojan Matovski December 5, 2016 at 3:30 pm #

        I just noticed that adding the cv2 path in the PYTHONPATH manager makes the cv2 available outside the cv virtual environment and I’m not sure that it something you wish to do, since Adrian (and a lot of other developers in the Python community) have stated multiple times that virtual environments are almost a “must use”.

        Maybe Adrian will leave further thoughts on this “solution”.

        • Adrian Rosebrock December 7, 2016 at 9:53 am #

          Provided that the version of Python you are importing cv2 into is the same version OpenCV was compiled against, this will work. However, instead of updating the PYTHONPATH (which can include extra libraries along your path) I would suggest just creating a sym-link to the cv2.so file.

      • Adrian Rosebrock December 7, 2016 at 9:53 am #

        Thanks for sharing Bojan!

  18. kara November 1, 2016 at 1:38 am #

    what a very helpful tutorial is!
    but i have problem, when I did Step 4, no error appeared. But, unlike your result, mine doesnt have Libraries , numpy , and packages path in the list. I install Python 2.7 btw.
    then when i keep continue, I couldnt find any linked library when I did check ls -l /usr/local/lib/python2.7/site-packages/ (total 0) or dist-packages. but in dist-packages, it was total 364 items detected.
    how could be the problem?
    is it okay to ignore this thing?

    ive solved my problem by deleting “build” folder and do the procedure from beginning. without deleting it, the problem keeps coming
    thank you~

    • Adrian Rosebrock November 1, 2016 at 8:50 am #

      Congrats on resolving the error Kara!

      • Mike December 19, 2016 at 10:22 am #

        I seem to have the same issue, in that my CMake output in step 4 shows Python 2 with an interpreter but nothing else. No libraries, numpy or paths.

        As with Kara my Python 2.7 install seems to be in /dist-packages.

        Can you advise when you delete the build folder? Do you just mean you deleted the build, start the process over and create a new build folder?

        Thanks for the help

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

          If I need to make a change to my CMake command, I first delete the “build” directory, re-create the “build” directory, and then run my updated CMake command.

          • jordan May 26, 2017 at 9:04 am #

            Hi Adrian,
            I’m having same issue as above. Also Python 2.7 and seeing install in dist-packages.
            I delete the ‘build’ folder and still problems persist when I try the cmake process again. Any other ideas you can suggest? I’m so close to the finish line!


          • jordan May 26, 2017 at 9:11 am #

            Hi Adrian,
            Two follow-up points that may provide some insight. I don’t actually see cv2.so file in either packages folder, even though I follow instructions to the letter.

            Also when I run workon cv I get an error message, that says “Error: deactivate must be sourced. Run ‘source deactivate’ instead of ‘deactivate’.

            I believe this is due to my anaconda python install, which I read would require me to run source deactivate instead of deactivate.

            Yet when I run workon cv, I indeed am in the environment and can open Python from here, so I believe that all steps are working up until my openCV cmake.

          • jordan May 26, 2017 at 9:51 am #

            Sorry that this is coming out in 3 separate comments!

            I noticed that I do have a cv2.so file – but it’s in my virtual environment. Is this OK? Did I do something wrong?

            Can I just reverse my binding command? I ran cmake while in cv, so this outcome doesn’t seem surprising, but it’s confusing compared to instructions above, because my cv2.so is in .virtualenv instead of in /usr/local/lib

            Thanks again!

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

            It’s perfectly okay for the cv2.so file to be in your virtual environment. This might also be because you used Anaconda as well.

      • Trung Bui June 16, 2017 at 2:43 am #

        Hi Adrian,

        I have the same issue, and it cannot be solved by simply deleting build folder. I check that ~/.virtualenvs/cv/lib/python2.7/site-packages/ does include cv2.so, numpy, and pip.

        I cannot figure out what actual problem is. Please share your opinion.

        Thank you.

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

          What about build/lib? Can you check that directory and see if the Python bindings were successfully built? If they do not exist in build/lib, then the Python section of CMake was not configured properly.

          • Trung Bui June 19, 2017 at 2:09 am #

            You are right. Is there any way to configure Python section of CMake properly? I have just installed cmake by the command “sudo apt-get install cmake”.

          • Adrian Rosebrock June 20, 2017 at 11:03 am #

            The section “Step #4: Configuring and compiling OpenCV on Ubuntu 16.04” covers how to configure the CMake command. I have plans on creating a more updated blog post with video tutorials and foolproof CMake configurations which should also help with getting OpenCV installed.

            In the meantime, I would suggest you take a look at the Quickstart Bundle and Hardcopy Bundle of my book, Practical Python and OpenCV. Both of these bundles include a pre-configured Ubuntu VirtualBox virtual machine that comes with OpenCV + Python pre-installed. It’s by far the fastest way to et up and running with OpenCV.

  19. Rafael November 1, 2016 at 9:48 pm #

    Hi Adrian,

    I normally use pyenv to for my python environments, but when I setup the build it didn’t set the interpreter and libraries path correctly so I followed your tutorial and used virtualenv/virtualenvwrapper.

    But now it won’t find any interpreter at all. The vm is definitely activated and the “PYTHON_EXECUTABLE=” points to “/path/to/vm/bin/python”.

    Any idea what could cause this problem?


    PS: Really nice and detailed tutorial by the way!

    • Adrian Rosebrock November 3, 2016 at 9:50 am #

      It’s hard to say what the actual problem is without having physical access to your machine. I doubt that pyenv would conflict since you are already explicitly inside the Python virtual environment, but at the same time I’ve always used both virtualenv and virtualenvwrapper. I would suggest double checking your virtual environment path.

  20. Scott November 4, 2016 at 6:56 pm #

    Thanks for these, Adrian! They’ve taken so much of the headache out of switching from Windows to Linux. Setting up OpenCV in Visual Studio was such a nightmare, that I dreaded trying to do it on Linux. Your tutorials made it a snap in Ubuntu and Raspbian both.

    One question / comment. If you’re going to use Python3, don’t you need to use pip3 instead of pip? I had a problem with numpy when importing cv2, but reinstalling numpy with pip3 seems to have fixed the problem.

    Thanks, again.

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

      I’m happy the tutorials helped Scott! Once you make the switch over to Ubuntu/Raspbian it actually makes working with OpenCV and computer vision much easier.

      As for pip vs. pip3, once you are inside the Python virtual environment it doesn’t matter — the virtual environment correctly sets the pip version based on your Python interpreter for the project.

  21. Ostrogrox November 8, 2016 at 9:59 am #

    Thanks for this great tutorial where I’ve learned a lot!
    Following the steps, I found that opencv doesn’t support cuda-8 so ‘make’ fails (giving “nppiGraphcut8_32f8u was not declared in this scope opencv python”). There is a bugfix on opencv repository but it’s not merged with master yet.
    check out : https://github.com/opencv/opencv/issues/6677
    Using that, I successfully compiled it with cuda 8 (takes about 20 minutes to compile).
    Thanks again

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

      Thanks for sharing!

    • Dave Selinger December 13, 2016 at 12:21 pm #

      Hey there, just thought I’d share that if you run into this NPPIGraphCut8 issue, I made a fork of OpenCV just to support this specific configuration: https://github.com/daveselinger/opencv/tree/3.1.0-with-cuda8
      Given that OpenCV doesn’t have point releases, I’m maintaining this simply to pull forward bug fixes like this one into the 3.10 branch. If you try to compile master, you get daily issues 🙂

  22. Francois November 8, 2016 at 8:49 pm #

    Hi Adrian,
    first, thank you for this great tutorial.
    I got an error during cmake that says: ImportError: No module named ‘numpy’
    although in the cmake report I got exactly the same output than yours in figure 3. I also double checked that numpy was installed in the virtualenv.
    Any idea what could be the problem?
    Thank you very much.

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

      Did you receive the error message during the CMake stage or the make stage? If you already have NumPy installed into the virtual environment AND your output matches mine then I would proceed with running “make”.

  23. ValR November 10, 2016 at 11:25 am #

    Great tutorial !
    I followed it with a fresh install of Linux Mint 18 (xfce desktop). I just had to install g++, i had an error during the Cmake step.

    Just want to become an openCV ninja !

    Thanks !

    • Adrian Rosebrock November 14, 2016 at 12:21 pm #

      Great job!

  24. ronen November 12, 2016 at 5:11 pm #

    Hi, Thanks for the helpful installation guide.

    • Adrian Rosebrock November 14, 2016 at 12:08 pm #

      No problem Ronen!

  25. farlina November 14, 2016 at 4:53 pm #

    i went through every step but still unable to use opencv..please help me out.
    its showing the error whenever i am trying to import.

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

      If you are having trouble importing the cv2 module please refer to the “Troubleshooting” section of this blog post. This section will take care of 95% of the common problems you may run into.

  26. rahul pathak November 15, 2016 at 12:36 pm #

    guys please help

    after typing “make clean” and “make” i am not getting the output as shown in the step 4.
    were am I going wrong

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

      There could be many reasons why you are not seeing the correct CMake output. Please see the “Troubleshooting” section at the bottom of this post for common errors.

  27. Marie November 15, 2016 at 4:26 pm #

    Hi! Could you help me? I can’t see “Libraries , numpy and packages path” 🙁 Only interpreter.

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

      It sounds like you are not in the “cv” virtual environment. Run workon cv before you run the CMake step and make sure you have installed NumPy into the “cv” environment.

  28. Sam November 16, 2016 at 2:57 pm #

    Hi Adrian, how would I go about using the virtual environment in an IDE such as IDLE?

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

      If you want to use an IDLE-like IDE, I would suggest install Jupyter Notebooks inside the “cv” virtual environment. Jupyter Notebooks are worlds better than IDLE.

  29. Anastasios Tsiolakidis November 17, 2016 at 3:07 am #

    I came to report build problems on Ubuntu 16.10, but glad to see someone already posted the solution -DENABLE_PRECOMPILED_HEADERS=OFF , you may want to make that one standard. You should include unzip in the installs, while new Ubuntus take the view that libpng-dev should be the name of the current library (v16), fingers crossed no incompatibilities will pop up. Finally, I take the view that such a big build should be even bigger, and would recommend that java is enabled before cmake, eg by pulling “maven”, unless someone is absolutely certain they will do all their work in Python.

    • Adrian Rosebrock November 18, 2016 at 8:59 am #

      The reason I don’t include -D ENABLE_PRECOMPILED+HEADERS=OFF standard is because it slows down the compile time. It’s only specific to a number of machines though. The vast majority of Ubuntu 16.04 systems I’ve installed OpenCV on do not require this switch.

      This switch has also already been documented both inside the blog post and in the comments section.

  30. navia November 18, 2016 at 6:20 pm #

    So detailed tutorial! Thanks! And I d like to know more about how to cleanly uninstall these software, for fear that I made some problems in between and want to start again from the very beginning. 😉

    • Adrian Rosebrock November 21, 2016 at 12:40 pm #

      You can use apt-get remove to remove each of the packages installed by apt-get. As for OpenCV, delete the source code directories and any file that has “opencv” in the filename inside /usr/local/lib.

      • Rahul Kamma January 1, 2017 at 10:40 am #

        Is it compulsory to delete the directories after install??

        • Adrian Rosebrock January 4, 2017 at 11:07 am #

          You don’t have to, but it saves space. The benefit of keeping them around is that if you re-run “make” with different configurations the entire library doesn’t have to be re-compiled so it will save time.

  31. Lucas S Ferreira November 22, 2016 at 6:35 am #

    The URL for the contrib repo is not working for me

    It seems that the correct one is: https://github.com/opencv/opencv_contrib/archive/3.1.0.zip

    • Adrian Rosebrock November 22, 2016 at 12:27 pm #

      Hey Lucas — make sure you’re expanding the entire code block when copying and pasting commands by clicking the “<=>” button. You’ll notice that your URL will match mine exactly.

  32. aNuo November 22, 2016 at 8:45 am #

    Hi Adrian,

    Many thanks for your helpful tutorial! A short question, what is the difference between opencv and python-opencv? It seems easier to install python-opencv. Then why opencv is necessary? what is the extra features in opencv?

    • Adrian Rosebrock November 22, 2016 at 12:25 pm #

      I assume you are referring to the apt-get version of python-opencv? In short, don’t use it. It’s old. Outdated. And doesn’t have many of the features you’ll need when developing computer vision applications. For example, you won’t have any of the opencv_contrib functionality which any type of feature extraction algorithm will likely need.

  33. Juan November 23, 2016 at 6:12 am #

    My two cents: I would recommend splitting these guides into Installation Guide for Py2..7, and one for 3.5. It’s overloaded and unnecessary to have both in a single place.

  34. Navidzj November 26, 2016 at 2:56 am #

    If you are installing in a path different from what Adrian said above, you might get not foung ffmpeg.h, videoio.h and videodev.h error when running CMake commands. Simply correct the path in this line:

    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \

    • Adrian Rosebrock November 28, 2016 at 10:34 am #

      Hey Navidzj — installing OpenCV into a different path shouldn’t cause any issues related to the video processing libraries. If you are getting an error related to this then you likely forgot these commands in Step #1:

      • jaan December 7, 2016 at 6:54 pm #

        Hello Adrian, I got the same errors and he’s right. He means that the actual path of the opencv_contrib-3.1.0 must be YOUR path to the directory. I had installed it in ~/workspace/opencv310. I corrected my path to:

        -D OPENCV_EXTRA_MODULES_PATH=~/workspace/opencv310/opencv_contrib-3.1.0/modules \

        and no more build error. nice tutorial btw

        • Adrian Rosebrock December 10, 2016 at 7:36 am #

          Got it, that makes sense. Thank you for the clarification Jaan!

        • cat January 10, 2017 at 8:09 pm #

          You’re absolutely correct. Since I have my “opencv_contrib-3.1.0/modules” under ~/Desktop/OpenCV3/opencv_contrib-3.1.0/modules, I change this line to:

          -D OPENCV_EXTRA_MODULES_PATH=~/Desktop/OpenCV3/opencv_contrib-3.1.0/modules \

          And it worked!

  35. Agnel Vishal November 27, 2016 at 12:45 pm #

    In my output of cmake, the libraries,numpy and package path of python3 were empty though interpreter was set. I had repeated the tutorial several times and confirmed that the python libraries were installed and I was in cv virtual environment. What worked for me was changing python to python 3.5 in cmake command
    ” -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python3.5″

  36. TAM November 29, 2016 at 11:51 pm #

    am a beginner in this scoop and u really had explained lots of thing so really thanks for this blog, actually i have dozen of questions but let me start with one after i installed opencv with virtualenv i upgraded my python version , yet i can’t access neither virtualenv nor my new python version, another thing : i tried to delete all what i did but i couldn’t find how ?
    hope u find my question

    • Adrian Rosebrock December 1, 2016 at 7:41 am #

      If you cannot access virtualenv or virtualenvwrapper hen please check the “Troubleshooting” section of this post where I discuss common errors or problems.

  37. Seba December 1, 2016 at 11:32 pm #

    Great Tutorial! It worked perfect, thank a lot!

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

      Awesome! Congrats on getting OpenCV installed Seba! 🙂

  38. santhosh December 2, 2016 at 10:29 am #

    Thanks a lot for the brilliant tutorial. Works like a charm for me !!

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

      Fantastic! Congrats on getting OpenCV installed on your system Santhosh!

  39. Amr December 5, 2016 at 7:50 pm #

    Hi Adrian,

    Thank you for a great Tut,
    I have followed the previous installation steps, and now i would like to do the Reverse ( purge remove opencv )


    • Adrian Rosebrock December 7, 2016 at 9:49 am #

      To delete OpenCV simply delete any opencv* files in /usr/local/lib.

  40. Yakup December 10, 2016 at 7:52 am #

    Sorry, I guess image isn’t opening again.

    This pic is your step


    But, this check is my step, Libraries and Package PATH isn’t seeming.
    Sorry for my bad English. 🙂

  41. Yakup December 11, 2016 at 3:31 am #

    Hi, I tried again and this time I succeeded. I know that I am in dept to you a thanking, even though we were “unlink” 🙂 I appreciate your effort and your interest in people. Thanks again.

    • Adrian Rosebrock December 12, 2016 at 10:41 am #

      Congrats in getting OpenCV installed! 🙂

  42. tham December 15, 2016 at 4:24 am #

    Thanks for this post, recently setup caffe environments and other deep learning tools on ubuntu cost me lot of times.

    I think the war between py2 and py3 are due to backward compatibility, in the real world old softwares are very hard to upgrade, this need times(equal to money).

    Besides, most of the programmers and project manager(at least my experiences told me) do not like programming at all. Those people lost interest on learning new things, some of them told me they refuse to use the tools if that means they need to spend time to study. Consider the conditions of real world, It is not a wise decision to break codes when upgrading language version.

  43. Edhoari Setiyoso December 24, 2016 at 7:00 am #

    Hi Adrian, thank you for this great installation tutorial.

    I have it installed on my Ubuntu 16.10 box and made some adjustment.

    1. There is bug in Ubuntu which stopped the compilation. I solved it by following instruction -> http://askubuntu.com/questions/742180/cmake-error-when-i-try-install-opencv-3-1

    2. I modified cmake flags with the following (please modify opencv-contrib with your own):

    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
    -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \

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

      Thanks for sharing Edhoari!

  44. kris December 25, 2016 at 1:02 pm #

    Hey, after cmake, I still don’t see the libraries and numpy. I only see
    “Interpreter: /home/mnemono/.virtualenvs/cv/bin/python3 (ver 3.5.2)”

    I’m still inside cv. I checked and double checked.

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

      If you are in the “cv” environment make sure NumPy was successfully installed via pip freeze. You should see NumPy in that list. If not, install it via:

      pip install numpy

      As for the libraries section, your path might be slightly different. Try to find the path using the ls command and auto-tab complete. It’s a pain, but if you work with it, you’ll find the correct path.

      • kazoo January 8, 2017 at 3:20 am #

        +1 for this question. I’m having the same issue for python2.7. I checked pip freeze, and it showed the followings:

        I could only see Interpreter. Then, after I installed OpenCV with command sudo make install, all the .so files were placed under usr/loca/lib, not usr/local/lib/python2.7/site-packages. This may be because I missed packages path, but how can I fix this?

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

          This sounds like a CMake misconfiguration of some sort. It’s hard to say what the exact error is without having access to your machine. It’s normal for the OpenCV binaries to be placed in /usr/local/lib but the cv2.so file should be in the site-packages directory. If it’s not (and the compile succeeded) you can simply manually copy the cv2.so file into the site-packages directory.

          • klibre87 March 17, 2017 at 8:20 pm #

            I have exactly the same problem as above. I only can see Interpreter, I have been working on this for days, I have no idea how to solve it

          • redit June 22, 2017 at 1:17 am #

            i have the same pb i could’t fix it

          • Neal October 28, 2017 at 6:02 pm #

            Same issue, no idea how to solve it. When looking at the CMAKE configuration it can’t even see the other python version so I don’t know how it ends up in the system python site-packages directory.

            But I did what Adrian said to do and copied cv2.so to the virtualenv python site-packages directory but now I’m getting the import error: undefined symbol: PyCObject_Type

          • Adrian Rosebrock October 31, 2017 at 8:07 am #

            Again, it’s a bit hard to diagnose this error without access to your machine but it sounds like you compiled OpenCV for one version of Python and then are trying to import it to a different Python version. Since the cv2.so bindings were not compiled for the same version of Python you are running into an error. Definitely go back to the “CMake” step and check the Python 2/Python 3 section — my guess is that the Python versions are not matching what they bindings are being imported into.

  45. Akanksha Dwivedi January 1, 2017 at 5:53 am #

    Hi, Adrian. Thank you so much for this tutorial. I installed opencv on my ubuntu 16.04 machine using this tutorial with python 3.5. Could you also list out the steps to install matplotlib. I tried installing them following different links but they did not exactly work out.
    Thank you!

    • Adrian Rosebrock January 4, 2017 at 11:10 am #

      Where you getting an error when installing matplotlib? Or were the plots simply not showing up? If it’s the latter, you should refer to this blog post.

  46. Eric Lovejoy January 6, 2017 at 11:46 am #

    This is well written, and easy to search visually, and it reminded me to do sudo ldconfig, which took my woes of not being able to run my programs after updating to opencv version 3.2, and completely alleviated them.

    • Adrian Rosebrock January 7, 2017 at 9:30 am #

      I’m happy to hear it! Congrats on getting OpenCV installed Eric 🙂

  47. amankishan January 9, 2017 at 10:32 am #

    Hi ,

    Thank you for the tutorial , After following the whole process , I am able to setup with opencv in my ubuntu system , but it is running only on python 3.5 , since I am not able to find cv2.so in ~/usr/local/lib/python2.7/site-packages or ~/usr/local/lib/python2.7/dist-packages.

    Please provide help .

    Thank you

    • Adrian Rosebrock January 10, 2017 at 1:12 pm #

      If you would like to have the Python 2.7 bindings and Python 3.5 bindings then you’ll need to re-compile OpenCV with Python 2.7 support and copy the resulting cv2.so file into your Python 2.7 site-packages directory.

  48. Peter January 11, 2017 at 10:47 am #

    I installed opencv 3.2.0 on Ubuntu 16.04

    If the install path is /sw/opencv then setting

    export PYTHONPATH=/sw/opencv/lib/python3.5/dist-packages/

    fixes the “import cv2” problem without having to rename .so files

  49. Mzbahrainwla January 14, 2017 at 5:27 pm #


    Thanks for the tutorial. It is really very helpful.

    I ran into a problem:
    After running the cmake command, the instruction say to run make -j4. I can’t do that as I don’t see a makefile in my build folder.

    Any clue as to where I might be going wrong?


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

      If there isn’t a resulting make file after running CMake, then CMake exited with an error. Go back to the CMake step and resolve any errors with CMake.

  50. Frederik Kratzert January 16, 2017 at 7:16 am #

    First of all: Thank you very much for this tutorial.

    As a side note, if anyone under Ubuntu 16.04 with OpenCV (I now installed 3.2.0) under Python2 has problems with OpenCV connections and PyQt4 GUI’s, change
    “sudo apt-get install libgtk-3-dev” to “sudo apt-get install libgtk2.0-dev”

    The error that occured was some Glib-Object Warning. Everything i could find out was, that there are some troubles between libgtk2 and libgtk3 and libgtk2 is already installed. Changing to libgtk2.0-dev did the trick.

    libgtk2.0-dev is also the version of pyimagesearch’s guide of opencv and ubuntu 14.04 😉

    • Adrian Rosebrock January 16, 2017 at 8:04 am #

      Thanks for sharing Frederik!

  51. Vegard January 16, 2017 at 12:43 pm #

    Hey, I followed your tutorial to the point, but when i start python in order to test the install (in the cv virtual environment), i get a ImportError (ImportError: dynamic module does not define module export function (PyInit_cv2)) when i try to import cv2. I’ve run the tutorial three times now, to no avail (guess that makes me crazy, expecting different results). Any idea what might be wrong? Any help will be much appreciated!


    • Adrian Rosebrock January 16, 2017 at 1:57 pm #

      Are you trying to compile OpenCV with Python 2.7 or Python 3 support? The likely issue here is that you compiled the wrong Python bindings for the Python version you are using.

  52. nikitha January 18, 2017 at 11:42 am #

    what can i do? if it shows ” canot read from progress file?

    • Adrian Rosebrock January 18, 2017 at 12:16 pm #

      Hi Nikitha — what command is giving you that error?

  53. John January 20, 2017 at 12:42 pm #

    Hi may I know what is the difference between installing with this method and just simply typing sudo apt-get python-opencv ?

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

      Using apt-get to install OpenCV is not recommended. It will install an older version of OpenCV without the opencv_contrib modules, meaning that you’ll be missing on important functionality (specifically keypoint detectors and local invariant descriptors like SIFT/SURF). While it’s certainly easier to just issue an apt-get command, I don’t recommend this. It will cause more problems in the long run.

  54. Sidney January 23, 2017 at 7:59 pm #

    Hey I get it worked when in terminal, but I would like to use it with IDE like wingIDE, but how to set ‘workon cv’ thing inside because the shell in wingIDE is already in python.

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

      I haven’t used the Wing IDE before, but all you should have to do is set the “Project Interpreter” like in PyCharm.

  55. Joyce January 27, 2017 at 5:03 pm #

    Adrian, you rock. Thank you for sharing and thank you for your time

    • Adrian Rosebrock January 28, 2017 at 6:45 am #

      Thank you for the kind words Joyce, I’m happy the tutorial helped!

  56. Siddharth February 3, 2017 at 3:37 am #

    Thank you so very much for everything

  57. Shubhankar February 4, 2017 at 12:39 am #

    Man you are amazing!!!! Thanks for writing this.

    • Adrian Rosebrock February 4, 2017 at 9:24 am #

      No problem Shubhankar, I’m happy it helped!

  58. Murtaza Dalal February 6, 2017 at 2:07 am #

    For me import cv2 works but cv2.xfeatures2d doesn’t so I dont think that opencv-contrib install worked. I’m confused because I’m pretty sure that I followed all the steps. Any ideas as to what the issue could be?

    • Adrian Rosebrock February 7, 2017 at 9:17 am #

      If xfeatures2d isn’t found then this opencv_contrib was not compiled. Double-check the path to your opencv_contrib in your CMake command and then ensure that xfeatures2d is listed in the “Packages to build” section of CMake output.

      • Murtaza Dalal February 7, 2017 at 6:09 pm #

        Hi adrian, I checked my path it was fine, I didnt see xfeatures2d though. What do you think is going on?

        • Adrian Rosebrock February 10, 2017 at 2:15 pm #

          Without physical access to your machine I’m not sure what the exact issue is, but you should triple check your path to the contrib modules. I am 99% sure that is the problem.

  59. Mikkel Green February 7, 2017 at 8:14 pm #

    Hey Adrian,

    Thanks for the great tutorial! I had a problem with the output of my cmake not listing the libraries, numpy etc. I was most certainly on (cv) but I think the mistake I made was running pip install numpy as sudo unnecessarily. When I ran pip freeze as you recommended it revealed it was not installed properly in my virtualenv. I rm -rf ‘d my build dir and properly installed numpy and then specified my python_executable as python2.7 (as another poster suggested).

    • Adrian Rosebrock February 10, 2017 at 2:14 pm #

      I would simply go back to access your virtual environment and install NumPy there:

      And then run the remaining steps from within the “cv” virtual environment.

  60. Viktor February 8, 2017 at 9:07 am #

    Hi Adrian,

    I’m quite new to Linux and therefore have the following question:
    How do I remove OpenCV properly to start again from scratch (the common answers on the internet didn’t help me yet)?

    I need to switch from 3.1.0 to 3.0.0 because the FlannMatcher seem to be bugged in 3.1.0 ( see https://github.com/opencv/opencv/issues/5667 ) and the solution is to modify the cv2.cpp file or go back to 3.0.0. As I couldn’t find the .cpp-file I tried to remove opencv via different methods which lead to the fact that opencv is now not working at all 🙂 .

    Thanks in advance and keep up the great work.

    • Adrian Rosebrock February 10, 2017 at 2:13 pm #

      Did you successfully compile and install OpenCV? If so, remove all libopencv* files from /usr/local/lib. Otherwise, delete your “build” directory and recompile.

  61. Alex February 9, 2017 at 6:55 pm #

    I made it to the cmake step without any issues. I copied and pasted the cmake command into my terminal and ran it, and the output did not contain all of the things that you had in yours under python 3. All I had was the interpreter and nothing else. I double and triple checked to make sure i was in the cv environment so that was not my issue. I’m fairly certain I followed every step exactly, but I will run through one more time to make sure. Any idea why I’m only getting the interpreter with cmake here?

    Thanks so much!

    • Adrian Rosebrock February 10, 2017 at 2:02 pm #

      It sounds like you might be using a different version of Python 3 than the one in this tutorial. What is the output of running python --version in your terminal?

      • Fadwa August 24, 2017 at 3:13 am #

        Yes i have the same problem ! Python –version is 2.7.12

  62. Guillaume February 11, 2017 at 9:19 am #

    Very nice tutorial ! Thank you !

  63. Kevil February 14, 2017 at 3:28 am #

    It’s a little bit long, but it works.
    Thank you very much!

  64. Nikki February 14, 2017 at 6:05 am #

    In step 4, after creating directory ,Interpreter in Python2 and Python 3 is ON and not showing path.Can anyone help me to resolve this problem.

  65. Tony Holdroyd February 17, 2017 at 11:37 am #

    Had problems the first go-round, so instead of wasting too much time, and since I was working with a more-or-less fresh install of Ubuntu 10.04, but thought I may have messed it up with other installs, I re-installed the O.S. and bingo, it all worked perfectly this time and I now have my virtual environments. Great stuff, thank you Adrian, I am now ready to march on my with my pyimagesearch gurus journey.

  66. Olim February 17, 2017 at 5:55 pm #

    Hi Adrian,

    Thank you for the tutorial!
    I am encountering a problem in Step 4: “make clean” gives the following result:

    make: *** No rule to make target ‘clean’. Stop.

    My output of CMake is identical to yours.
    What could be the problem?

    Cheers, Olim

    • Adrian Rosebrock February 20, 2017 at 7:59 am #

      To start, I would:

      1. Make sure that CMake exited without error. This is the likely cause of the problem.
      2. If CMake completed successfully, make sure you are in the “build” directory when executing the “make” program.

  67. Giuseppe Scollo February 19, 2017 at 7:51 am #


    I’m just in the process of installing OpenCV 3.2.0 on Ubuntu 16.04 following your excellent tutorial. I’m finished with step #2. Before I embark on step #3, I wonder about a couple of questions:

    (1) Is it OK to install in another directory rather than $HOME, say /opt/opencv/ ?

    (2) Suppose I create _two_ distinct installation directories, say /opt/opencv2/ and /opt/opencv3/, and therein follow all the steps to install and compile OpenCV separately, one with Python 2.7 and the other with Python 3.5 (resp. binaries python and python3), could such a double installation work or would conflicts arise at some point in time?

    Thank you very much, in advance, for your help.

    • Adrian Rosebrock February 20, 2017 at 7:44 am #

      1. Yes, you can install OpenCV in any location that you would like.

      2. This is (essentially) exactly what I do to install multiple versions of OpenCV + Python on the same machine. As long as you have separate virtual environments (or update your PYTHONPATH), this method works perfectly.

  68. Presish February 21, 2017 at 3:25 pm #

    what do you do if Python 2 and Python 3 Interpreter is missing content? just says “NO” next to the Python 2 and Python 3 result of “cmake” command.

    • Adrian Rosebrock February 22, 2017 at 1:32 pm #

      Make sure you are in the “cv” virtual environment before you are executing CMake, otherwise CMake might have problems trying to automatically determine your Python paths.

  69. Anuradha Sharma February 23, 2017 at 2:07 pm #

    Didn’t get any details after cmake in the interpreter column. It says NO. I am in cv environment. and tried doing it twice also

    • Adrian Rosebrock February 24, 2017 at 11:28 am #

      Which Python version are you trying to compile OpenCV bindings for?

      • Anuradha Sharma March 17, 2017 at 10:16 am #

        tried it with both python 2.7 and python3.5.2!

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

          I am sorry to hear about the issue Anuradha, that is super frustrating. However, without physical access to your machine I’m not sure what the exact error would be.

          • Arjun April 25, 2017 at 2:00 pm #

            Same issue encountered.

  70. Asif February 24, 2017 at 2:11 pm #

    I am not being able to install the freenect(Kinnect) in “cv” virtual environment. I can use the library from outside the “cv” virtual environment but not from inside. Please help.

    • Adrian Rosebrock February 27, 2017 at 11:23 am #

      I haven’t used freenect before so I don’t know where the output modules are, but I would suggest taking a look at the site-packages directory of your Python install. If they are there, copy them into the site-packages directory of your Python virtual environment.

      • Salvador Blanco October 12, 2017 at 8:48 am #

        First of all thank you very much for the tutorial.

        The problem I have is that to use the Kinect camera with freenect I need root access, and when I run the scripts with sudo I get the following error: ” import cv2 ImportError: No module named cv2″

        The solution would be: ” This problem was caused because when I installed OpenCV I created it in a virtual env. Making a link to the OpenCV file from outside the env to the OpenCV library folder fixed it.”

        But I don’t know how to make a link to the OpenCV file, could you help me?

        Thank you very much

        • Adrian Rosebrock October 13, 2017 at 8:43 am #

          Hi Salvador — I haven’t used the Kinect and associated libraries in quite some time, but in this case, it sounds like you are trying to run a script as root using a Python virtual environment. The easiest way to resolve this issue is to supply the full path to your Python virtual environment binary:

          sudo ~/.virtualenvs/cv/bin/python your_script.py

  71. soumen February 27, 2017 at 1:00 pm #

    Hello Adrian, I want to use opencv3.0 Can you please let me know how to do

  72. Hilman February 27, 2017 at 9:46 pm #

    Hey Adrian. Nice as always.

    One question, what does

    $ sudo rm -rf ~/get-pip.py ~/.cache/pip

    line do? Why is it necessary?

    • Adrian Rosebrock February 28, 2017 at 6:59 am #

      This line deletes the cache directory for pip. If you installed virtualenv or virtualenvwrapper via sudo this directory may now belong to the root user than than the standard user. The command simply deletes the cache directory so it can be used by a normal user again (not strictly necessary — only necessary if you get a permissions issue, but good to include).

  73. Zubair March 5, 2017 at 12:25 am #

    Hi adrian
    Thanku for your every tutorials , i learned alot and still learning from your blogs thanks alot bro 🙂

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

      Thank you for the kind words Zubair, I’m happy the tutorials have helped you 🙂

  74. Prasanth March 5, 2017 at 2:52 pm #

    How to set Interpreter , Libraries , numpy and packages paths. My cmake is successful but I see no such variables after python path.

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

      Make sure you are in the “cv” virtual environment prior to running the CMake command.

  75. jsa March 5, 2017 at 3:54 pm #

    I was able to install opencv 3.2 in ubuntu 16.04.02 and xubuntu 16.04 wihtout virtualenvs. However, I had to do a fresh install, otherwise it wouldn’t build for python3. Thank you.

  76. Ramu March 10, 2017 at 4:46 am #

    Hi Adrian,
    When i run cmake I cant find paths to interpreter, libraries and numpy.
    Please help me

    • Adrian Rosebrock March 10, 2017 at 3:45 pm #

      Make sure you are in the “cv” virtual environment prior to running CMake.

  77. Wei Ren March 10, 2017 at 7:48 pm #

    Thank you for the great tutorial. In case that you have installed the ROS opencv3 package in your Ubuntu machine and include the ROS setup.bash in your .bashrc, you may hit an import error when you try using the OpenCV Python3 binding. This is because the ROS setup.bash sets the environment variable “PYTHONPATH” to its Python2.7 package path, i.e., /opt/ros/kinetic/lib/python2.7/dist-packages. To fix this, you may remove the ROS Python 2.7 package path from PYTHONPATH by adding the following line in ~/.virtualenvs/cv/bin/activate.

    export PYTHONPATH=${PYTHONPATH/"/opt/ros/kinetic/lib/python2.7/dist-packages"/}

    • Adrian Rosebrock March 13, 2017 at 12:21 pm #

      Thank you for sharing Wei Ren!

    • Robert Lee April 5, 2017 at 4:25 pm #

      Dear Wei,

      I am also trying to get ROS work with this install. I was wondering if you could elaborate a little more on how to get it to work. I did gedit on the file you mentioned and added the last line but i still cant seem to import rospy.

    • Bonafide Caskett June 7, 2017 at 6:00 am #

      Thanks Wei and Adrian, It worked perfectly when you have ROS Kinetic

    • Riya May 30, 2018 at 3:30 pm #


      I don’t really understand.

      How do I find ~/.virtualenvs/cv/bin/activate. ?

      I am a bit confused. Please guide me


    • Raghav Bansal June 25, 2018 at 3:38 pm #

      Thanks a lot Wei Ren for this trick. It worked for me with ROS kinetic.
      I also want to thank Adrian Rosebrock for providing this wonderful opencv tutorial.

  78. Twinkle March 13, 2017 at 4:23 am #

    Thank you tons.
    completing working now.

  79. sebastian March 13, 2017 at 8:49 pm #

    Thanks a lot! I apreciate the effort that you put in did this amazing step by step tutorial for openCV, have a nice day!

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

      Thank you Sebastian, congrats on getting OpenCV installed!

  80. Neeraj March 16, 2017 at 3:32 am #

    What is the difference between your guide to install opencv and “sudo pip3 install opencv-python”?


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

      The opencv-python package on PyPI doesn’t include (1) the “opencv_contrib” module which has all the extra goodies with OpenCV and (2) it doesn’t have “highgui” module support so you won’t be able to use functions like cv2.imshow to display images to your screen. I would recommend installing OpenCV from source.

      • Frank June 6, 2017 at 3:19 pm #

        Hi Adrian
        I’ve installed openCV per your tutorial. Worked great.
        However, I keep getting an error when I try to display an image on the computer screen per the example code in chapter three of your book practicalpython and open cv.
        Looks like I need the highgui, etc modules. So I guess I have to install via source? Can I use pip3 install opencv-python if I’m using python 2.7.x?

        • Adrian Rosebrock June 9, 2017 at 1:59 pm #

          Hi Frank — the issue is because you installed via pip install opencv-python. This method does not include the highgui module required to display images to your screen. I would suggest compiling OpenCV from source as I describe in this tutorial so you can display windows to your screen.

      • Jarades July 22, 2017 at 12:57 pm #

        After the installation — apparently everything has gone well –, I am not able to show images with imshow, I can use imread and work on the image properties but can not display it.

        • Jarades July 22, 2017 at 1:02 pm #

          I am sorry, it is ok and working. I just wasn’t using the waitkey()…

          • Adrian Rosebrock July 24, 2017 at 3:39 pm #

            Yes, the cv2.waitKey call is very important. Make sure you include it 🙂

  81. Manu BN March 19, 2017 at 6:30 am #

    Here are some errors I faces & their fixes:

    1. During cmake if it says cant find path to contrib_modules, then mention the complete path of the modules like -D OPENCV_EXTRA_MODULES_PATH=/home/dell/OpenCV_Installed/opencv_contrib-3.1.0/modules

    2. During cmake if errors occur due to IPPICV unable to download, then physically download it from http://www.linuxfromscratch.org/blfs/view/svn/general/opencv.html and put it in the folder /home/dell/OpenCV_Installed/opencv-3.1.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e and run cmake with -D WITH_IPP=ON

    3. If you are still facing errors due to IPPICV during compilation time such as ” cannot find -iipicv” then simply copy it to usr/local/lib with the following command sudo cp 3rdparty/ippicv/unpack/ippicv_lnx/lib/intel64/libippicv.a /usr/local/lib/

    Hope it helps others and thanks to Adrian for the awesome tutorial

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

      Hey Manu — I saw your original CMake command. Your issue for (1) was that you left an extra space before -D OPENCV_EXTRA_MODULES_PATH and the start of the path. This caused the problem. You can specify the full path or the shortcut path. Either will work.

      Thanks for sharing your findings!

    • johnson April 16, 2017 at 1:00 pm #

      Thank you very much for such detail! ippicv annoying me days!

    • Akhilesh May 8, 2017 at 12:00 pm #

      I am still having the same error as you got could u help me with it please

  82. Faris March 20, 2017 at 6:12 am #

    Hi, Adrian, could you tell me how to install opencv without virtualenv? Thanks

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

      Skip all steps involving creating a Python virtual environment. Skip installing virtualenv/virtualenvwrapper. Don’t update your .bashrc file. Skip any steps involving “workon”.

  83. Andy March 26, 2017 at 7:58 pm #

    What a fantastically useful contribution to the world! Love this article.

    Thank you,


    • Adrian Rosebrock March 28, 2017 at 1:03 pm #

      Thank you Andy!

  84. Hack_Rider March 28, 2017 at 5:08 pm #

    really great post..describe brifely each and every step with detail..what we actully doing..
    thats is great post..
    install opncv without error..really thank you Adrian Resebrock..
    great work

    • Adrian Rosebrock March 31, 2017 at 2:09 pm #

      Thank you, I appreciate the kind words 🙂

  85. Karen March 28, 2017 at 8:52 pm #

    Hi Adrian,

    I just installed the opencv3 & python2.7 on Ubuntu successfully according to your wonderful tutorial. The project I am going to do is about object tracking, and when I was calling cv2.Tracker_create(“KCF”), an error turned up: ‘module’ object has no attribute ‘Tracker_create’, also, when i try help(cv2.face), it also returns the same error.

    Thanks in advance!


    • Adrian Rosebrock March 31, 2017 at 2:06 pm #

      It sounds like you might not have installed OpenCV with opencv_contrib support. Double-check your CMake output to ensure contrib support is enabled. Also, make sure you download OpenCV 3.2 which is the latest release.

  86. Murthy March 30, 2017 at 12:33 am #

    Hi Adrian,
    I upgraded my Linux from 14.04 to 16.04 only to find that python2.7 failed to import cv2 also the upgrade rendered by /dev/sda to read only drive.

    I used:
    sudo hdparm -r0 /dev/sdb

    to set that right and rebooted my machine. that did not make any difference to python. Still gave me the same errors when I imported cv2.

    I re-installed openCV on 16.04 under different virtual environment and all seems to be working now.

    Wondering if anyone has upgraded linux to 16.04 and are able to still work on their OpenCV installed prior to upgrade.

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

      In general I do not recommend doing major operating system upgrades on your development environment. Sym-links are bound to break, which is likely what happened in your particular situation.

  87. iss April 4, 2017 at 7:43 pm #

    hi adrian

    when I want to import cv2 I get this error

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

    When I type the command ls in the directory site-packages I see that there is the file cv2.so

    • Adrian Rosebrock April 5, 2017 at 11:51 am #

      Are you inside a Python virtual environment or outside of it? If you’re inside the “cv” virtual environment you need to sym-link the cv2.so file into the site-packages directory for your “cv” environment.

      • iss April 6, 2017 at 10:24 am #

        it s done thunck you adrian

        do you have a tutorial for recognition emotions on image with opencv

  88. Chaitanya Deshpande April 5, 2017 at 12:13 pm #

    Thanks bro! works like charm!

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

      Great to hear Chaitanya, congrats on getting OpenCV installed on your Ubuntu system.

  89. Yunfei April 6, 2017 at 8:12 am #

    Hi, Adri.
    Thanks for the wonderful work you have done.
    I run into a promblem after followed the step by step tutorial couple of times.
    I run ‘workon cv “sucessfully and python, but I can’t import cv2

    However only when I cd to this directory:
    can I sucessfully import cv2?

    I really don’t understand how the virtual environment works. and can’t find an answer. Really dont want to be stuck here.

    Do you have any idea of this? Really appreciate it .

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

      Based on your error message it seems that you are not in the “cv” virtual environment. Make sure you run:

      And then try to import your cv2 bindings. From there, take a second to read up on how virtual environments work.

  90. Francesco April 6, 2017 at 6:52 pm #

    Hi Adrian,
    thank you very much for your fantastic guide! 🙂
    I am using OpenCV 3.1 and Python 3.5. I have configured properly the Enviorment and it is working fine by terminal.
    Now the question is this: how can I configure the same Enviorment that I am using with the Atom.io IDE?
    Thanks a lot.

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

      I haven’t used the Atom IDE, but I imagine it has a “Project Interpreter” setting similar to PyCharm. Update the Python interpreter to be your Python virtual environment and you’ll be all set.

      • Francesco April 10, 2017 at 4:17 am #

        Thanks. I will have a look…..

        • Dalibor April 17, 2017 at 5:42 am #

          You don’t need to configure anything. Just install platformio-ide-terminal. You will see in the down left corner + sign. Just click it, and type “workon cv”. When you want to execute your code, on the folder panel right click to py file, and choose “Copy full path”, in you terminal window, below your code, type python(or python3) and press ctr+shift+v. And your code will run. Greetings from Serbia

  91. kaan April 8, 2017 at 5:07 am #

    install opncv 3.2.0 without error..(after 3 days of try, it was my fault )
    really I feel better now
    thank you Adrian

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

      Congrats on getting OpenCV installed Kaan, nice job!

  92. Krishan April 10, 2017 at 1:05 pm #

    Hello Mr. Adrian , Firstly i want to congratulate and thank you for your excellent tutorials. I want to install use opencv with pycharm on my ubuntu 16.04. I use anaconda on my system for all python work. What steps should i follow to run opencv with pycharm on my system ? I tried this tutorial but make -4j command did not complete successfully.

    • Adrian Rosebrock April 12, 2017 at 1:15 pm #

      The first step is to install OpenCV on your system. Without knowing what your specific error is, I can’t point you in the right direction. Once you get OpenCV installed you can link to with PyCharm as I discuss here.

  93. Michael du Plessis April 13, 2017 at 7:37 am #

    Hello Adrian, I am having trouble finding

    https://github.com/Itseez/opencv/archive/3.1.0.zip ?
    Has the link changed ?

    • Adrian Rosebrock April 16, 2017 at 9:04 am #

      Double-check your internet connection. The link is working just fine for me.

  94. Sayeed April 20, 2017 at 1:17 am #

    i failed to install…cmake fails..also if i continue i dint find anaconda/keras in the virtual environment…do i have to install them again? Pls gv a tutorial on how to install opencv using conda

    • Adrian Rosebrock April 21, 2017 at 10:57 am #

      If you create a Python virtual environment, you need to install Keras (and any other libraries you want) inside the virtual environment. Keep in mind that a Python virtual environment is separate and independent from your system Python install. I would suggest reading up on Python virtual environments here.

      Additionally, if you are struggling to get OpenCV installed, be sure to take a look at the Quickstart Bundle and Hardcopy Bundle of Practical Python and OpenCV which includes a pre-configured Ubuntu VirtualBox virtual machine which has OpenCV pre-installed.

  95. Geethan April 23, 2017 at 5:45 pm #

    Great article.
    Everything explained in best possible detail
    Thanks and keep up the good work 🙂

    • Adrian Rosebrock April 24, 2017 at 9:36 am #

      Thanks Geethan!

  96. Arjun April 25, 2017 at 1:46 pm #

    Hi Adrian,
    Everything worked till the very end except i didnt find my python 3.5 folder as stated in ‘usr/local/lib/python3.5/site-packages/’ . However, i did find my ‘opencv-3.1.0’ and ‘opencv_contrib -3.1.0’ folders. How do i overcome this problem ?
    Thanks in advance. 🙂

    • Adrian Rosebrock April 25, 2017 at 9:06 pm #

      Hey Arjun — I’m confused by your comment. You were able to compile OpenCV correctly, but the cv2.so bindings were not where you expected them to be? Or you ran into an error when running CMake or make?

  97. Kunal May 2, 2017 at 2:13 pm #

    I have Anaconda already installed, so my path for python2.x is different and so that cmake command is not executing properly 🙁
    Any suggestions?

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

      You would need to determine the Python library paths for your Anaconda install. I have never tried this with Anaconda so it might take a bit of trial and error.

  98. dai May 9, 2017 at 2:24 pm #

    Hi thank you for your instructions.

    I got problems as when I imported cv2 in python 2.7.
    And I try to use cv2.MultiTracker, then it said that ”AttributeError: ‘module’ object has no attribute ‘MultiTracker’ ”

    Could you help me with this, I want to run multitracker.py.

    Thank you.

  99. Duhai May 11, 2017 at 10:41 pm #


    When working with a virtual environment based on python3, for some reason after running camke to configure the build does not detect python interpreters. I had to run camke configuration on a virtual environment based on python2.

    I am running:
    Distributor ID: Ubuntu
    Description: Ubuntu 16.04.2 LTS
    Release: 16.04
    Codename: xenial


    • Adrian Rosebrock May 15, 2017 at 9:01 am #

      That is quite strange that the CMake configuration didn’t automatically pickup the virtual environment for Python 3. I’m planning on writing an updated blog post that can automatically detect the proper CMake configurations. In the meantime, take a look at this blog post (even though it’s for macOS) as you’ll likely need to set Python 3 specific CMake parameters.

  100. Esteban May 13, 2017 at 11:45 am #

    Thank you, it works pretty good.

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

      Fantastic, I’m glad to hear it Esteban!

  101. Yashas May 17, 2017 at 2:51 am #

    Never mind, I figured it out. Thanks though.

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

      Congrats on resolving the issue Yashas!

  102. KelvinS May 22, 2017 at 1:02 am #

    Thanks for the excellent tutorial Adrian and congrats for the great work you’re doing on PyImageSearch.

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

      Thank you, I really appreciate the kind words.

  103. KelvinS May 22, 2017 at 10:47 am #

    A little tip: I had an issue with the ‘confidence’ value when trying to use the predict function from the face recognition modules (by using eigenfaces). The issue is explained here: http://answers.opencv.org/question/82294/cant-get-predict-confidence/. If someone will use the ‘confidence’ value in future I suggest to take a look at the link above before build the OpenCV, so you can change the face.hpp file to provide the ‘confidence’ value as explained by Berak as a workaround.

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

      This is great, thank you so much for sharing!

  104. Aleksei May 22, 2017 at 1:35 pm #

    Adrian, thank you a lot! Great article. I installed everything without any problems. However, I would kindly ask you several questions, just in case if you will have time to answer them.

    Let’s assume installation was made 100% the way described in the tutorial (for python 3):

    1) We installed opencv in a given virtual environment, however ‘make install’ placed .so file into system-wide site-packages directory of python 3.5.
    – Why?
    – Why not in virtual environment directory?
    – If so, why system-wide python 3.5 installation cannot import opencv even if .so file is present in its site-packages directory (/usr/local/lib/python3.5/site-packages).

    I understand that build process was specifically tuned by the virtual environment modules which may not be available on a system level, but installation places all the files into system-wide directory and it rises this question (naturally, system-wide python should not be able to run opencv installed inside virtual environment because but I cannot figure out how this isolation is guaranteed).

    2) All the story behind virtual environment is to have isolated dependencies.
    – How can I install another different instance of opencv for a different virtual environment (for example, 3.0.0 or 3.1.0 without contributions or opencv built for different numpy version)?
    – ‘make install’ will place another .so file into /usr/local/lib/python3.5/site-packages and here the conflict comes. Am I right?

    3) When we install system-wide pip – is it installed per specific python version or for any python version particular system has (I guess the latter)? How does this process work?

    4) If I update global version of pip or python. Does my virtual environment keep old versions?

    5) What do you think about python’s own implementation – venv? Is is better/worse than virtualenv+virtualenvwrapper, in your opinion?

    Appreciate your attention and help. Great site!

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

      1. You can install the cv2.so files into your Python virtual environment, that’s not an issue at all. It requires changing the site-packages flag during CMake, but that’s the only change that is required.

      2. The easiest way to do this is to create a separate Python virtual environment for each OpenCV install. Access the individual Python virtual environment. Run CMake + make. Then copy the resulting cv2.so files from your build directory into their appropriate site-packages directories for each Python virtual environment. This is exactly how I organize my installs.

      3 and 4. Pip is first installed systemwide. Once you create a new Python virtual environment a new version of pip is used. Therefore, you can upgrade or downgrade pip and other packages from within the virtual environment.

      5. I personally think virtualenv+virtualenvwrapper is easier to use, but that’s my personal opinion.

      • Aleksei May 25, 2017 at 1:07 pm #

        Thank you a lot for replies, Adrian. Have a nice day!

  105. Peter May 23, 2017 at 8:54 am #

    Great tutorial, thanks!

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

      Thanks Peter!

  106. Akash May 25, 2017 at 6:40 am #

    thank you so much. i was running into weird errors all the time and your method worked perfectly for me. very grateful to you

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

      I’m glad to hear it Akash! Congrats on getting OpenCV installed on your Ubuntu machine.

  107. progitto May 25, 2017 at 8:34 am #

    Quest a hint for who has “Missing dependencies for SOCKS support” problem on
    mkvirtualenv cv -p python2 command like me.
    Results from this post on stackoverflow https://stackoverflow.com/questions/38794015/pythons-requests-missing-dependencies-for-socks-support-when-using-socks5-fro that is related to one proxy enviroment variable (for who use a proxy for internet connection).

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

      Thanks for sharing!

  108. Jeremy Delahanty May 25, 2017 at 1:19 pm #

    Hey guys,

    I’m having trouble installing and finding the libjasper-dev and libpng-dev modules. When I try to install them with sudo apt-get install, I’m told that the package can’t be located or that the version has been obsoleted (for jasper and png, respectively).

    Any tips on how I can find and install these libraries so I can get started?

    Thanks! Good luck!

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

      Try running sudo apt-get update to refresh your package repositories.

  109. Petjan May 25, 2017 at 7:21 pm #

    why not checkinstall?

  110. Itay June 3, 2017 at 1:20 pm #

    Hey Adrian, I’ve followed you instructions and the environment works as expected – Thanks a lot.
    There is one more issue though – when I enter the virtual environment I cannot import packages that are already installed such pylab even though that it possible to import them in other python regular shells and vice versa (cannot import cv2 to regular python shells).
    What am I missing ?

    • Adrian Rosebrock June 4, 2017 at 5:30 am #

      Hi Italy — I would suggest reading up on virtual environments more. By definition, a virtual environment creates a separate, independent Python environment from your system install. To install any new packages into the “cv” virtual environment, simply access the environment via the workon and install your packages via pip.

  111. mohammed June 3, 2017 at 7:35 pm #

    total 1972
    give to me error
    1972 cannot open

    • Adrian Rosebrock June 4, 2017 at 5:29 am #

      What command is giving you that output?

      • mohammed June 4, 2017 at 7:08 am #

        at step 5
        commend total 1972

  112. ENA June 7, 2017 at 9:53 pm #

    I followed the instructions. Installation went smoothly but at the end /usr/local/lib/python2.7/site-packages is empty.

    • Adrian Rosebrock June 9, 2017 at 1:44 pm #

      I would double-check your build/lib directory and ensure the resulting cv2.so file is in there. If not, there was an error in your CMake configuration and the Python bindings were not built.

  113. Jin Kweon June 7, 2017 at 10:19 pm #

    Hi Adrian,
    I cannot download virtualenvwrapper for some reasons….

    -Jin Kweon

    • Adrian Rosebrock June 9, 2017 at 1:43 pm #

      What is the error message that you are getting when you try to install virtualenv/virtualenvwrapper?

  114. Saket Karve June 11, 2017 at 7:54 am #

    I have installed python 2.7.13.
    When I followed all the steps mentioned above skipping the virtual environment creation, I could do everything perfectly fine. But, python2 output from step 4 doesnot contain libraries, numpy and packages. Rather python3 contains it.
    Moreover, the cv2.so file is also not present anywhere under python2.7 but is present in python3.5 directory

    Not able to understand why is it happening!!

  115. PREM CHIDAMBARAM June 12, 2017 at 11:57 am #


    I did buy the book from you. I have successfully install open cv. Thank you. Now to get this to work with a python IDE….

    • Adrian Rosebrock June 13, 2017 at 10:58 am #

      Thank you for picking up a copy of Practical Python and OpenCV! As far as a Python IDE goes, have you tried PyCharm?

  116. PREM CHIDAMBARAM June 13, 2017 at 12:59 pm #


    Used pycharm . I am at Chapter 10 in a day !! Thank you . Let me finish the book with the Case Studies. I think I owe you a big recommendation some time this week!!

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

      Congrats on getting all the way to Chapter 10 in a single day, nice job!

  117. Sridhar Thiagarajan June 14, 2017 at 1:23 am #

    Thanks a bunch adrian!

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

      Glad I could help, Sridhar!

  118. Abi Shalini June 14, 2017 at 2:10 pm #

    Hey Adrian,

    Thank you for the installation guide! I can currently working on a project that requires installing scikit-learn

    I just wanted to know if I should pip install SciPy and scikit-learn before executing cmake or can I still install them after following through all the instructions above.

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

      You can install scikit-learn before or after installing OpenCV, it doesn’t make a difference (just make sure you install scikit-learn into the “cv” virtual environment).

  119. Erbic June 18, 2017 at 1:27 pm #

    One question: why do you have to rename the cv2.so file? Why doesn’t simply creating a hard link or soft link to the original long file name work?

    • Adrian Rosebrock June 20, 2017 at 11:05 am #

      You could create a sym-link for the cv2.so file, that is also perfectly acceptable. I personally prefer for the files to be (correctly) named cv2.so.

  120. Yuliang June 25, 2017 at 7:20 pm #

    Thanks for your article!
    I just confused about the virtual environment. I am only working on c++ project now, if I install the cv2 using this virtual env way, does my c++ project can find the cv2?
    Now I have an error like this:

    opencv2/opencv.hpp: No such file or directory

    So I know I have to install OpenCV, but c++ doesn’t have virtual env, right?


    • Adrian Rosebrock June 27, 2017 at 6:27 am #

      This method will install your OpenCV C++ headers, but C++ does not require the virtual environment (that is for Python only).

  121. Yuliang June 25, 2017 at 7:25 pm #

    Sorry but another question about the virtual env. If I want to install another app using virtual env, e.g., Tensorflow, in env “tensorflow”. Does the env “tensorflow” know there is an OpenCV installed in the machine?

    • Adrian Rosebrock June 27, 2017 at 6:28 am #

      Are you referring to C++ or Python? Keep in mind that this is a Python-based blog so I kindly ask that all questions are related to Python to keep comment discussions on track. If you have a question regarding C++ and OpenCV, I would suggest asking on the official OpenCV forums.

  122. Mayank June 28, 2017 at 6:32 am #

    Hi Adrian!

    I was trying the installation of opencv by your method, but my operating system is not having the required space in the root mount.So I’ll increase the size of my root, and want to try out a fresh installation, and want to remove the files and libraries download and installed.
    My build process is not getting completed due to no space in the disk.
    I want to remove all the libraries and files downloaded prior to that.
    Kindly help how to remove them.

    • Adrian Rosebrock June 30, 2017 at 8:20 am #

      All you need to do is delete your “build” directory. Then create a new “build” directory after you have increased the space of your system.

  123. Julio V June 28, 2017 at 12:21 pm #

    This is an amazing article. Many thanks for all the hard work and attention to detail that went into making it happen.

    Quick question, I performed the installation procedures on root, but is it possible to enable other user accounts to leverage the environment?

    • Adrian Rosebrock June 30, 2017 at 8:17 am #

      Congrats on getting OpenCV installed, Julio! Nice job.

      As for your question, if you installed OpenCV into /usr/local then yes, other users will have access to the OpenCV + Python bindings. However, you will need to create a Python virtual environment for each of the users.

      • Theo September 8, 2017 at 2:21 pm #

        So, to enable opencv + python on a non-root user, should I follow this tutorial again in a new virtual environment for the new user?
        Or is there a way to copy it from the root venv to a new venv?

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

          You can actually access the Python virtual environment for another user via root. Please see this blog post for an example.

  124. rastaxe June 29, 2017 at 5:00 am #

    Hi Adrian. I have a problem. I am installing opencv3.2 using virtaulenv (python3) on ubuntu.
    No problem when I import cv2 outside virtualenv. When I am inside virtualenv I have:

    ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type

    So it looks on another path. All the paths of interpreter, libraries, numpy, and packages path are like you described. I looked at the simlink and inside “.virtualenvs/cv/lib/python3.5/site-packages” folder the cv2.so file is correctly linked to:

    cv2.so -> /usr/local/lib/python3.5/site-packages/cv2.so

    So, do you know why the virtualenv looks wrong python packages (ROS installation?)

    • rastaxe June 29, 2017 at 5:39 am #

      Just an update. If a comment out the ROS stuffs from the .bashrc file it works. But I do not know how to make it works together.

    • Adrian Rosebrock June 30, 2017 at 8:12 am #

      It sounds like you compiled OpenCV with Python 2.7 support, but are trying to import it into a Python 3 shell. Keep in mind that you need to configure CMake such that it compiles OpenCV for the Python version you want to use. Python + OpenCV bindings are not compatible across different versions. You need to re-compile and re-install OpenCV for each version of Python you want to use.

    • artemii May 4, 2018 at 6:19 am #

      ROS Kinetic + OpenCV conflict

      For those, who faced with the same issue.

      It’s actually kindly explained by Mr Wei. I’m copying it with a ROS and ImportError tags.

      In case that you have installed the ROS opencv3 package in your Ubuntu machine and include the ROS setup.bash in your .bashrc, you may hit an import error when you try using the OpenCV Python3 binding. This is because the ROS setup.bash sets the environment variable “PYTHONPATH” to its Python2.7 package path, i.e., /opt/ros/kinetic/lib/python2.7/dist-packages. To fix this, you may remove the ROS Python 2.7 package path from PYTHONPATH by adding the following line in ~/.virtualenvs/cv/bin/activate.

      export PYTHONPATH=${PYTHONPATH/”/opt/ros/kinetic/lib/python2.7/dist-packages”/}

      just by putting it under the code the problem gets solved.

      • Adrian Rosebrock May 9, 2018 at 10:31 am #

        Thank you for sharing!

    • Ahmed Hassan October 24, 2019 at 2:07 pm #

      I deleted all what I have installed in this tutorial, I followed the steps for python 3.5 before.
      Then I tried to follow the python-2.7 version.
      it worked after that. Dunno, but it may be because ROS used the 2.7 version.

  125. Alexey June 29, 2017 at 2:28 pm #

    OpenCV contrib module now requires such flag at installation:


    Without it does not link to its contrib modules!

    • Adrian Rosebrock June 30, 2017 at 8:07 am #

      Hi Alexey — perhaps I’m misunderstanding your comment, but the -D OPENCV_EXTRA_MODULES_PATH flag is included in the CMake command in this blog post.

      • Erik December 16, 2017 at 3:55 am #

        No, it is just OPENCV_EXTRA_MODULES in the blog post. Took some time to find that one should append _PATH as well.

  126. Shivani July 1, 2017 at 8:08 am #

    Hello SIr !
    I followed each and every step of your tutorial.The problems I’m facing are :
    1) cmake is running fine, but I can’t see the libraries , package path or any other sub-heading in the output. There was no error during cmake, only the output is not as expected.

    2) site-package directory is empty and dist-package also has no files of opencv.

    I’ve repeated the procedure multiple times,it’s still not working. Any help would be appreciated.
    Thank you

    • Adrian Rosebrock July 5, 2017 at 6:29 am #

      Hi Shivani — make sure you are in the “cv” virtual environment before executing CMake. You should consider delete your “build” directory, re-create it, and re-run CMake.

  127. yahya_Nik July 2, 2017 at 12:15 pm #

    Dear Adrian;

    I have installed the OpenCV 3.2.0 and everything works fine (importing cv2). My problem is that when I try to use “Tracker = cv2.tracker_create(“MIL”)”. But I receive error “AttributeError: ‘module’ object has no attribute ‘tracker_create'”…
    I dont know what to do. I need to mention that I checked configuration and the extra Tracking Package was included.

    • Adrian Rosebrock July 5, 2017 at 6:23 am #

      You have a typo in your code. The “T” should be capitalized:

      tracker = cv2.Tracker_create("MIL")

      • Joe_Tro July 14, 2017 at 4:34 pm #

        I have the same issue. I made sure that I had the correct capitalization. I can also run the tracker example in opencv-3.2.0/build/bin just fine. Any ideas or things to check would be much appreciated.

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

          Can you confirm that you installed your Python + OpenCV bindings with the opencv_contrib module? That could be part of the issue.

  128. Ron July 3, 2017 at 12:02 pm #

    Adrian, the amount of work you have put into this is amazing, so I don’t want to appear whiny. I moved over to this thread after I managed to kill my SD card trying to set this up with Pi. I do have xbuntu 16.04 and I don’t know if that is the real problem. I’ve run the CMake and get an error log I’m trying to interpret. The primary issue seems to be with video files. One issue is I didn’t have ffmpeg installed. And when I did install it, I had to truncate the directory name of version numbers. I still got errors I tried to work around by coping files from a sub-directory to the ffmpeg directory. Also have errors like:
    fatal error: linux/videodev.h: No such file or directory






    • Adrian Rosebrock July 5, 2017 at 6:08 am #

      Hi Ron — my guess is that you are missing this command from Step #1 where we install video support:

      Run the above command, delete your “build” directory, re-create it, and re-compile.

      • Vivek July 16, 2017 at 7:05 am #

        Hi Adrian,

        I did install the video support, still I’m facing the same errors as mentioned by Ron.
        Can you please help in this?

        Note: I’m doing this on VM of Ubuntu 16.04 (Guest) on Windows 10 (host).

        Thanks in advance,

        • Vivek July 16, 2017 at 8:04 am #

          I’m getting following error:
          — Looking for linux/videodev.h
          — Looking for linux/videodev.h – not found
          — Looking for linux/videodev2.h
          — Looking for linux/videodev2.h – found
          — Looking for sys/videoio.h
          — Looking for sys/videoio.h – not found

          Not sure, what is missing?

          • Vivek July 16, 2017 at 8:09 am #

            I found my mistake.
            I used both opencv and contrib version 3.2.0, but cmake command had path of 3.1.0. So, when I fixed the version, it worked.

            Sorry to bother you.

            Thanks & regards,

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

            Congrats on resolving the issue, Vivek! Nice job getting OpenCV installed and working.

          • Min September 7, 2018 at 5:25 am #

            Hi Vivek, I have the same issue now, how did you check and change the path of cmake command?

  129. Samrat Sinha July 6, 2017 at 6:18 am #

    Nice blog Adrian.

    But how am i supposed to use opencv outside the “cv” virtual environment ? Suppose i needed to do it ?

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

      If you installed OpenCV globally into /usr/local you should be able to use OpenCV with Python outside the virtual environment.

  130. Rich July 6, 2017 at 5:28 pm #

    Hi, I believe I may have missed something. when I navigate to the folder Python3.5 > Dist packages, it is empty. However Python 2.7 > Dist packages has contents. reason being is I am having the issue of cv2 not found. I’m sure I went through all the steps. Is there a way to restart the process without havibg to start from step one? Iv’e not uninstalled the zip downloads.



    • Adrian Rosebrock July 7, 2017 at 9:50 am #

      The easiest way is to simply delete your “build” directory, re-create it, re-run CMake, and re-run make. There is no need to start from Step #1.

  131. Nacho July 7, 2017 at 1:35 am #

    Thank you very much for the tutorial, Adrian 🙂 I used pyenv-virtualenv so it needed a bit of tweaking but it works.

    I was wondering however: we have created a virtual environment (cv) which has numpy and which is used by opencv. However, I will potentially have different computer vision projects in my machine, and each one of them should have an independent virtual environment. However, newly created environments don’t have access to cv2, as expected.

    How do you deal with this? I hope we don’t need to recompile opencv for every new project! Thanks 🙂

    • Adrian Rosebrock July 7, 2017 at 9:49 am #

      Simply sym-link the cv2.so file into each of your newly created Python virtual environments (no need to recompile). From there you’ll be all set.

      • Nacho July 10, 2017 at 6:22 am #

        Works like a charm after installing numpy in the new virtual environment. Thanks 🙂

        • Adrian Rosebrock July 11, 2017 at 6:34 am #

          Fantastic, I’m had to hear it worked 🙂 Congrats on getting your system up and running with OpenCV.

  132. Jeremy July 11, 2017 at 9:49 pm #

    Hi, Adrian. What a thorough tutorial! My autistic brain thanks you. I am having a small problem installing OpenCV in Python 2…in Step#4 I execute cmake, and my packages path points to Python3.5, while all other paths point to Python2.7, which I’d rather use.

    I also get a couple of error messages, telling me that “no package found”, and “Configuring incomplete, errors occurred”.

    What can I do about this?

    • Adrian Rosebrock July 12, 2017 at 2:43 pm #

      If you are getting issues related to “Configuring incomplete, errors occurred” scroll back through the CMake output. It will tell you specifically where the errors were. You can also check the CMake errors log.

  133. walter July 21, 2017 at 3:30 pm #

    sorry for my English
    You could do a python and opencv tutorial for debian ..
    Does not work on debian ..
    Or if they know of a link
    Thank you and sorry for the inconvenience

    • Adrian Rosebrock July 24, 2017 at 3:50 pm #

      Hi Walter — Ubuntu is Debian-based. I’ve used this tutorial myself to install OpenCV on Debian machines. What is the issue you are running into?

  134. John July 25, 2017 at 8:08 pm #

    This is the best tutorial I’ve found for installing OpenCV in Ubuntu. Thanks!

    I installed PyCharm on the virtual environment and it all worked just fine.

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

      Thanks John, congrats on getting OpenCV installed!

  135. Muaz Usmani July 26, 2017 at 9:57 am #

    Hey Adrian, thanks for the great tutorial. I have followed this tutorial couple of times since I have found it. But recently I am trying to install it on a machine remotely where I don’t have sudo permission, which is why it is failing now. Also I am not that good with compiling using cmake etc. Can you please guide me what should I do in that case. Thanks.

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

      Hi Muaz — you will need sudo permissions to install the proper pre-reqs for OpenCV via apt-get. Otherwise, you would have to manually compile the pre-reqs by hand and install them into your local home directory. I do not recommend this as it’s a major pain.

  136. Kartik July 29, 2017 at 2:07 pm #

    Hi Adrian! First, I would like to thank you for this succinct tutorial to install OpenCV 3 on Ubuntu 16.04.

    I am a novice Linux user. I followed the steps as you have mentioned in the tutorial and OpenCV 3.2.0 has installed successfully. However, as you had said to switch to cv environment by sending ‘workon cv’ and ‘python’, I was able to access Python Shell only. For accessing scripts, I installed IDLE using the sudo command. To my surprise, when I typed out a simple program, it gave me the following errors:-

    “No module named cv2” and “No module named numpy”.

    How do I fix this? Do I need to configure my IDLE? I want to use IDLE for writing the programs in a similar fashion which we use it in Windows. Let me know if there’s a solution to this.

    Many Thanks!

    • Adrian Rosebrock August 1, 2017 at 9:48 am #

      IDLE cannot access your “cv” virtual environment, hence why you cannot import “cv2” and “numpy”. If you want to use something similar (and better) than IDLE, I would suggest using Jupyter Notebooks.

      • Kartik August 3, 2017 at 1:04 pm #

        So even the Jupyter Notebooks need to be installed on ‘cv’ environment?

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

          Correct. Please keep in mind that a Python virtual environment is totally and completely independent of your system Python install. If you are new to Python virtual environments (and why they are useful and a best development practice), please refer to this post.

  137. Kartik July 30, 2017 at 8:51 am #

    Can we install Scikit for Machine Learning? Do we need to install Scikit in the cv environment or normally in the system environment?

    • Adrian Rosebrock August 1, 2017 at 9:44 am #

      You would install scikit-learn into the “cv” virtual environment:

  138. Arek July 31, 2017 at 5:20 am #

    Hi I want to use python script in ruby on rails. How Can I run virtual env in rails controller? I have tried use system command but this isn’t work

  139. Stacy August 8, 2017 at 10:07 am #

    Thank you for the tutorial, Adrian! After some small difficulties I finally managed to do it:) And thanks for telling about virtualenv, it is a great thing and I didn’t use it before

    • Adrian Rosebrock August 10, 2017 at 8:53 am #

      Congrats on getting OpenCV installed Stacy, nice job! 🙂

  140. Zito August 10, 2017 at 3:29 am #

    I’m having trouble importing cv2. When I use python in the virtualenv, I am able to import it and use it properly. If I am not in the virtualenv, I am unable to import it. I have already manually copied the cv2.so file from my build file into my system install of python but this does not seem to fix the problem. Any help would be appreciated

    • Adrian Rosebrock August 10, 2017 at 8:42 am #

      Fire up your Python shell (outside of the virtual environment) and run:

      >>> import site; site.getsitepackages()
      From there copy the cv2.so (or sym-link it) into the site-packages directory. From there you should be able to import your OpenCV bindings.
  141. Willie Maddox August 13, 2017 at 12:17 am #

    If “workon cv” puts you in the virtual environment, what takes you out? Just plain old “deactivate”? or something special?

    • Adrian Rosebrock August 14, 2017 at 1:11 pm #

      You are correct — deactivate removes you from the virtual environment.

  142. Eric F. August 23, 2017 at 4:20 pm #

    Hi Adrian,
    I have some trouble for compiling opencv on ubuntu 16.04. I have this message:

    CMake error: The source directory “~/opencv-3.1.0/build/BUILD_EXAMPLES_ON” does not exist

    Could you help me?
    Thanks for all this work.

    • Eric F. August 23, 2017 at 4:57 pm #

      Forget my comment. I simply haven’t seen the two dots at the end of the comment CMAKE…

  143. Azmin August 25, 2017 at 7:45 am #

    Hi, I’ve tried to copy manually the cv2.so in site-packages system and cv2 environment like you said in FAQ but can’t as it is already there. but still have the error “no module named cv2” in Pycharm.
    I’ve found a new version of opencv and opencv-contrib that says can intergrate with Pycharm IDE here https://pypi.python.org/pypi/opencv-python but I don’t know the correct steps to do like in this tutorial you written. Can you enlighten me to resolve this? I’ve been stuck for days now. Thank you.

    • Adrian Rosebrock August 25, 2017 at 12:41 pm #

      So you’re using PyCharm as your IDE? If so, you just need to configure your “Python Interpreter” for the project. Please see this post for more details.

  144. edlectrico August 30, 2017 at 8:12 am #

    Thank you man, I followed your guide step-by-step and it works! Even in a Virtual Machine! I tried to do the same with an Ubuntu 17.04 but it failed. Anyways, thank you.

    • Adrian Rosebrock August 31, 2017 at 8:33 am #

      Congrats on getting OpenCV installed on Ubuntu 16.04! I haven’t tried this guide yet with Ubuntu 17.04. I would be curious to know what step the build errored out at.

  145. Jeremy Collins August 31, 2017 at 1:51 am #

    Thanks for this guide, it saved me a lot of time!

    • Adrian Rosebrock August 31, 2017 at 8:29 am #

      I’m glad to hear it Jeremy! Congrats on getting OpenCV installed.

  146. Fav September 3, 2017 at 1:50 am #

    two things , did exactly everything under linux mint

    but the virtual enviroment went to the python 2.7 folder , not the 3.5!

    what should i do?

    • Adrian Rosebrock September 5, 2017 at 9:29 am #

      Hi Fav — it sounds like you may have created your Python virtual environment via:

      $ mkvirtualenv cv

      Rather than:

      $ mkvirtualenv cv -p python3

      Definitely double-check which Python version you used to create the virtual environment.

  147. Ho Hai Dang September 4, 2017 at 9:49 am #

    Hello Adrian Rosebrock,
    I followed all your direction. When I tried to use cmake command. cmake… -> make. It doesn’t work just can go to 67% -> fail
    I realized I had to add this line to cmake -D WITH_FFMPEG=OFF\. Then it works. I don’t know when I disable ffmpeg what features I will lost when I use OpenCV?

    • Adrian Rosebrock September 5, 2017 at 9:20 am #

      FFMPEG is typically used for various video file codecs. I rarely compile OpenCV with FFMPEG implicitly enabled so unless you are looking for specific video codecs you won’t lose much.

      • Carol December 7, 2017 at 5:04 am #

        Hi Adrian,
        Thanks for the tutorials! I’m currently encountering a problem with videocapture function of cv2. The ‘ret’ value is always false when using cv2.videocapture. Just wondering, if this is the issue related to ffmpeg compilation?

        • Adrian Rosebrock December 8, 2017 at 4:51 pm #

          It’s hard to say without having access to your physical machine, but yes, it seems there is a problem with your installation. I would suggest recompiling and reinstalling OpenCV. Make sure you didn’t miss the step where we installed the video I/O libraries:

        • Wentai December 16, 2017 at 11:26 am #

          Hello Carol, I wonder whether you have solved your problem?

  148. Simon September 4, 2017 at 9:27 pm #

    Adrian, I appreciate the time you’ve spent preparing this tutorial and responding the comments you’ve received. I’ve read your tutorial line by line, trying to understand each step. I am still pretty much clueless, but its not your fault: I am utterly new at Ubuntu and Python (OpenCV is pretty much Chinese to me at this point). I have a lot of questions, but I’ll only ask the one that’s been bothering me the most: If the idea is to have OpenCV working in a virtual environment, why isn’t everything done within it? I guess another way to phrase the question is: Why isn’t the creation of the virtual environment step # 1. If I’ve understood correctly, OpenCV has been installed in the Home directory, and the build has been made inside the OpenCV file. I know my confusion stems from a lack of understanding of how virtual environments work. I read the links you referenced regarding this matter, but they aren’t clear enough for a newbie (or plain simple idiot) like me. I would GREATLY appreciate it if you could shed some light on all this, and if you could be so kind, give me a reference to understand the mechanics of python paths and dependencies in the context of Ubuntu.

    Thank you very much!

    • Adrian Rosebrock September 5, 2017 at 9:16 am #

      Hi Simon — the virtual environment only affects your Python PATH (a system variable). Installing dependency libraries via apt-get (such as those for image I/O, video I/O, optimizations, etc.) do not affect Python. Again, Python virtual environments will only affect the Python path on your system.

  149. Yasir September 16, 2017 at 5:22 am #

    im trying to build it in OrangePi-plus2,

    whatever i do cmake task won’t detect python library, numpy and package path

  150. gal September 17, 2017 at 5:37 am #

    hey Adrian, thanks for this tutorial!
    as usual, awesome work pal.

    i have worked by youre instructions,
    and yet, i get “cv2 module has no attribute ‘SIFT_create()’ ”

    same for other sift ans surf commands

    why is that?


    • Adrian Rosebrock September 18, 2017 at 2:11 pm #

      The SIFT class changed from OpenCV 2.4 to OpenCV as I discuss here.

      You can access SIFT via:


  151. Linus September 18, 2017 at 8:11 am #

    Just because I’m currently upgrading to 3.3.0, I saw you still have the old GitHub links in the post. All the OpenCV related official repos have moved to the opencv account, now located at https://github/opencv/*

    So you might update the links, even though GitHub just redirects the old one with 301 (moved permanently).


  152. Jan Debiec September 20, 2017 at 10:54 am #

    Hi Adrian,
    Thank you for the fantastic tutorial.
    I have the similar issues with the configuration of cmake.
    But I have found the fix:
    Always delete the build folder if the call to cmake was not perfect (results as in your picture).
    Then make fresh build folder, cd into it and call cmake with proper parameters.
    P.S. You earned a lot of praise for the idea with virtual environment

    • Adrian Rosebrock September 20, 2017 at 1:12 pm #

      Thanks Jan! And yes, you’re absolutely right — to restart your compile from scratch the easiest method is to delete your build directory, re-create it, and then re-run “cmake”. Thanks for chiming in!

  153. Avery September 22, 2017 at 2:47 pm #

    I’m having the same issue as the one described here: http://answers.opencv.org/question/138047/opencv-32-includes-libmirprotobuf-and-protobuf-26-which-is-conflicting-with-protobuf-31/

    Any ideas how to compile to avoid this problem?

    I’m on Ubuntu 16.04

    • Ramy Hassan October 2, 2017 at 5:24 am #

      I’ve the same issue, How did you resolve it.

  154. Nader September 27, 2017 at 6:43 pm #

    Hi adrian
    Thanks for your tutarial.
    I tried to install opencv with your guid in ubunto 16.04.
    I upgraded ubunto and now i have ubunto 17.04 on my machin.
    When i run my code from command terminal it works fine.
    But when i configure it by qt it crashe when i use namedwindow.
    Is it becase of upgrading my ubunto to 17.04 or an other reason cause crash??
    Thank you

    • Adrian Rosebrock September 28, 2017 at 9:03 am #

      Hi Nader — I haven’t tried this tutorial with Ubuntu 17.04, but in general, upgrading your OS is likely to break the development environment. If you want to use Ubuntu 17.04 I would suggest starting with a fresh install of Ubuntu 17.04.

      • Nader September 29, 2017 at 9:38 am #

        Thanks for your answer adrian.
        I changed my machin os to ubuntu 14.04 and tested it
        I use opencv 3.3 and qt 5.9.
        But my problem does’nt resolved.
        Actually when use namedwindow or imshow or waitkey, programm crashes.
        I feel that opencv 3.3 does’nt works on qt or is not cofigurable with qt.
        Does it correct??
        Thanks a lot.

        • Adrian Rosebrock October 2, 2017 at 10:18 am #

          I haven’t used OpenCV with Qt in quite some time, but you can compile OpenCV with Qt support. I typically used GTK-based GUIs so I’m unfortunately not sure what the exact error is.

  155. Marcelo September 28, 2017 at 5:15 pm #

    Guys, I had a trouble with a looping in IPPICV download that didn’t stopped. This instructions made me compile:

    To add to cmake command:
    -D WITH_IPP=ON ..


  156. einstienien October 1, 2017 at 12:59 pm #

    I’m getting a fatal error: linux/videodev.h: No such file or directory error. I checked /usr/include/linux directory and can verify I only have a videodev2.h file.

    I’m getting the same error for videoio.h. I’m currently trying to install in a 16.04.3 VM. I have successfully installed all the packages required. Maybe something changed in the packages since this write up.