Ubuntu 16.04: How to install OpenCV

ubuntu1604_anpr

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.

curious_about_cv

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.

Summary

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!

, , , ,

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

  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.

    Jaime

  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.

  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.

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

    Hi¡
    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.

  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,
    regards..

  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.

  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…
    Regards
    Ehsan

    • 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 #

    Hi,

    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
            https://github.com/opencv/opencv/issues/6050

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

            find_package(HDF5)
            include_directories(${HDF5_INCLUDE_DIRS})

            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.

            Thanks!

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

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

            Thanks all!

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

      cmake -D CMAKE_BUILD_TYPE=RELEASE \
      > -D CMAKE_INSTALL_PREFIX=/usr/local \
      > -D INSTALL_PYTHON_EXAMPLES=ON \
      > -D INSTALL_C_EXAMPLES=OFF \
      > -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
      > -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
      > -D BUILD_EXAMPLES=ON \
      > -DENABLE_PRECOMPILED_HEADERS=OFF ..

      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.

  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??
    Thanks

    • 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 #

    Hi,

    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.

    Regards

    • 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).

      Cheers,
      Bojan

      • 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 #

        Hi,
        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
        Mike

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

  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?

    Regards

    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 #

    Hi,
    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 #

    Hey,
    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 )

    regards,
    Amr

    • 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

    http://www.pyimagesearch.com/wp-content/uploads/2016/09/ubuntu1604_python3.jpg

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

    cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
    -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
    -D BUILD_EXAMPLES=ON ..

    • 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:
        numpy==1.11.3
        pkg-resources==0.0.0

        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.

  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 #

    Hey,

    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?

    Thanks!

    • 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!

    Thanks!

    • 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 ?
    Thanks

    • 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?

  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 #

    Hi,

    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?

Trackbacks/Pingbacks

  1. Install OpenCV 3 and Python 2.7+ on Ubuntu - PyimageSearch - October 24, 2016

    […] http://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/ […]

Leave a Reply