How to install OpenCV 3 on Raspbian Jessie


A few weeks ago Raspbian Jessie was released, bringing in a ton of new, great features.

However, the update to Jessie also broke the previous OpenCV + Python install instructions for Raspbian Wheezy:

Since PyImageSearch has become the online destination for learning computer vision + OpenCV on the Raspberry Pi, I decided to write a new tutorial on installing OpenCV 3 with Python bindings on Raspbian Jessie.

As an additional bonus, I’ve also included a video tutorial that you can use to follow along with me as I install OpenCV 3 on my own Raspberry Pi 2 running Raspbian Jessie.

This video tutorial should help address the most common questions, doubts, and pitfalls that arise when installing OpenCV + Python bindings on the Raspberry Pi for the first time.


For this tutorial I am going to assume that you already own a Raspberry Pi 2 with Raspbian Jessie installed. Other than that, you should either have (1) physical access to your Pi 2 and can open up a terminal or (2) remote access where you can SSH in. I’ll be doing this tutorial via SSH, but as long as you have access to a terminal, it really doesn’t matter.

The quick start video tutorial

Before we get this tutorial underway, let me ask you two quick questions:

  1. Is this your first time installing OpenCV?
  2. Are you just getting started learning Linux and how to use the command line?

If you answered yes to either of these questions, I highly suggest that you watch the video below and follow along with me as a guide you step-by-step on how to install OpenCV 3 with Python bindings on your Raspberry Pi 2 running Raspbian Jessie:

Otherwise, if you feel comfortable using the command line or if you have previous experience using the command line, feel free to follow the tutorial below.

Installing OpenCV 3 on Raspbian Jessie

Installing OpenCV 3 is a multi-step (and even time consuming) process requiring you to install many dependencies and pre-requisites. The remainder of this tutorial will guide you step-by-step through the process.

To make the installation process easier, I’ve included timings for each step (when appropriate) so you know when to stick by your terminal, grab a cup of coffee, or go for a nice long walk.

If you’re an experienced Linux user or have already installed OpenCV on a Raspberry Pi (or another other system) before, you can likely just follow the steps outlined below.

However, if this is your first time installing OpenCV (or you don’t have much prior exposure to the Linux operating systems and the command line), I highly recommend that you watch the video above and follow along with me as I show you how to install OpenCV 3 on your Rasberry Pi running Raspbian Jessie.

That said, let’s get started installing OpenCV 3.

Step #1: Install dependencies

The first thing we should do is update and upgrade any existing packages, followed by updating the Raspberry Pi firmware.

Timing: 3m 33s

You’ll need to reboot your Raspberry Pi after the firmware update:

Now we need to install a few developer tools:

Timing: 51s

Now we can move on to installing image I/O packages which allow us to load image file formats such as JPEG, PNG, TIFF, etc.:

Timing: 42s

Just like we need image I/O packages, we also need video I/O packages. These packages allow us to load various video file formats as well as work with video streams:

Timing: 58s

We need to install the GTK development library so we can compile the highgui  sub-module of OpenCV, which allows us to display images to our screen and build simple GUI interfaces:

Timing: 2m 48s

Various operations inside of OpenCV (such as matrix operations) can be optimized using added dependencies:

Timing: 50s

Lastly, we’ll need to install the Python 2.7 and Python 3 header files so we can compile our OpenCV + Python bindings:

Step #2: Grab the OpenCV source code

At this point we have all of our prerequisites installed, so let’s grab the 3.0.0  version of OpenCV from the OpenCV repository. (Note: As future versions of OpenCV are released just replace the 3.0.0  with the most recent version number):

Timing: 2m 29s

For the full install of OpenCV 3 (which includes features such as SIFT and SURF), be sure to grab the opencv_contrib repo as well. (Note: Make sure your opencv  and opencv_contrib  versions match up, otherwise you will run into errors during compilation. For example, if I download v3.0.0 of opencv , then I’ll want to download v3.0.0 of opencv_contrib  as well):

Timing: 1m 54s

Step #3: Setup Python

The first step in setting up Python for our OpenCV compile is to install pip , a Python package manager:

Timing: 26s

I’ve discussed both virtualenv and virtualenvwrapper many times on the PyImageSearch blog before, especially within these installation tutorials. Installing these packages is certainly not a requirement to get OpenCV and Python up and running on your Raspberry Pi, but I highly recommend that you install them!

Using virtualenv  and virtualenvwrapper  allows you to create isolated Python environments, separate from your system install of Python. This means that you can run multiple versions of Python, with different versions of packages installed into each virtual environment — this solves the “Project A depends on version 1.x, but Project B needs 4.x” problem that often arises in software engineering.

Again, it’s standard practice in the Python community to use virtual environments, so I highly suggest that you start using them if you are not already:

Timing: 17s

After virtualenv  and virtualenvwrapper  have been installed, we need to update our ~/.profile  file and insert the following lines at the bottom of the file:

You can use your favorite editor to edit this file, such as vim , emacs , nano , or any other graphical editor included in the Raspbian Jessie distribution. Again, all you need to do is open the file located at /home/pi/.profile  and insert the lines above at the bottom of the file.

Now that your ~/.profile  has been updated, you need to reload it so the changes can take affect. To force a reload of the ~/.profile  file you can (1) logout and log back in, (2) close your terminal and open up a new one, or (3) just use the source  command:

Note: You’ll likely need to run the source ~/.profile  command each time you open up a new terminal to ensure your environment has been setup correctly.

The next step is to create our Python virtual environment where we’ll be doing our computer vision work:

The above command will create a virtual environment named cv  using Python 2.7.

If you want Python 3, run this command instead:

Again, it’s important to note that the cv  Python environment is entirely independent from the default version of Python included in the download of Raspbian Jesse.

If you ever reboot your system, logout and log back in, or open up a new terminal, you’ll need to use the workon  command to re-access the cv  virtual environment, otherwise you’ll be using the system version of Python instead:

You can ensure you are in the cv  virtual environment by examining 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 prompting, indicating that you are in the cv virtual environment.

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

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

Figure 2: If you do not see the “(cv)” text on your prompt, then you are not in the cv virtual environment.

If this is the case, you need to run the source  and workon  commands above.

Assuming that you are in the cv  virtual environment, we can install NumPy, an important dependency when compiling the Python bindings for OpenCV. You might want to grab a cup of coffee or go for a walk while NumPy downloads and installs:

Timing: 16m 10s

Step #4: Compile and install OpenCV

At this point, we are ready to compile OpenCV.

First, make sure you are in the cv  virtual environment:

Followed by setting up the build:

Update (3 January 2016): In order to build OpenCV 3.1.0 , you need to set -D INSTALL_C_EXAMPLES=OFF  (rather than ON ) in the cmake  command. There is a bug in the OpenCV v3.1.0 CMake build script that can cause errors if you leave this switch on. Once you set this switch to off, CMake should run without a problem.

Before you move on to the compilation step, make sure you examine the output of CMake!

Scroll down the section titled Python 2  and Python 3 .

If you’re compiling OpenCV 3 for Python 2.7, then you’ll want to make sure the Python 2  section looks like this (highlighted) in red:

Figure 3: Ensuring that Python 2.7 will be used for the compile.

Figure 3: Ensuring that Python 2.7 will be used for the compile.

Notice how both the Interpreter  and numpy  variables point to the cv  virtual environment.

Similarly, if you’re compiling OpenCV for Python 3, then make sure the Python 3  section looks like this:

Figure 4: Ensuring that Python 3 will be used for the compile.

Figure 4: Ensuring that Python 3 will be used for the compile.

Again, both the Interpreter  and numpy  variables are pointing to our cv  virtual environment.

In either case, if you do not see the cv  virtual environment for these variables MAKE SURE YOU ARE IN THE cv  VIRTUAL ENVIRONMENT PRIOR TO RUNNING CMAKE!

Now that our build is all setup, we can compile OpenCV:

Timing: 1h 35m

The -j4  switch stands for the number of cores to use when compiling OpenCV. Since we are using a Raspberry Pi 2, we’ll leverage all four cores of the processor for a faster compilation.

However, if your make  command errors out, I would suggest starting the compilation over again and only using one core:

Using only one core will take much longer to compile, but can help reduce any type of strange race dependency condition errors when compiling.

Assuming OpenCV compiled without error, all we need to do is install it on our system:

Step #5: Finishing the install

We’re almost there! Just a few more things and we’ll be 100% done.

For Python 2.7:

Provided you finished Step #4 without error, OpenCV should now be installed in /usr/local/lib/python2.7/site-packages :

Note: In some instances OpenCV can be installed in /usr/local/lib/python2.7/dist-packages  (note the dist-packages  rather than site-packages ). If you do not find the  bindings in site-packages , be sure to check dist-packages  as well.

The last step here is to sym-link the OpenCV bindings into the cv  virtual environment:

For Python 3:

OpenCV should now be installed in /usr/local/lib/python3.4/site-packages :

For some reason, unbeknownst to me, when compiling the Python 3 bindings the output .so  file is named  rather than .

Luckily, this is an easy fix. All we need to do is rename the file:

Followed by sym-linking OpenCV into our cv  virtual environment:

Step #6: Verifying your OpenCV 3 install

At this point, OpenCV 3 should be installed on your Raspberry Pi running Raspbian Jessie!

But before we wrap this tutorial up, let’s verify that your OpenCV installation is working by accessing the cv  virtual environment and importing cv2 , the OpenCV + Python bindings:

You can see a screenshot of my terminal below, indicating that OpenCV 3 has been successfully installed:

Figure 5: OpenCV 3 + Python 3 bindings have been successfully installed on my Raspberry Pi 2 running Rasbian Jessie.

Figure 5: OpenCV 3 + Python 3 bindings have been successfully installed on my Raspberry Pi 2 running Rasbian Jessie.


Q. When I try to use the mkvirtualenv  or workon  commands, I get an error saying “command not found”.

A. Go back to Step #3 and ensure your ~/.profile  file has been updated properly. Once you have updated it, be sure to run source ~/.profile  to reload it.

Q. After I reboot/logout/open up a new terminal, I cannot run the mkvirtualenv  or workon  commands.

A. Anytime you reboot your system, logout and log back in, or open up a new terminal, you should run source ~/.profile  to make sure you have access to your Python virtual environments.

Q. When I open up a Python shell and type import cv2 , I get the dreaded ImportError: No module named cv2  error.

A. The reason for this error is hard to diagnose, mainly because there are multiple issues that could be causing this problem. For starters, make sure you are in the cv  virtual environment using workon cv . If the workon  command is giving you problems, then see the previous questions in this section. From there, you’ll want to investigate the site-packages  directory of your cv  virtual environment located in ~/.virtualenvs/cv/lib/python2.7/site-packages/  or ~/.virtualenvs/cv/lib/python3.4/site-packages/ , respectively. Make sure that the sym-link path to the  file is valid. If you do not know how to do this, please consult the video tutorial at the top of this post.

So, what’s next?

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

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

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

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


All code examples covered in the book are guaranteed to run on the Raspberry Pi 2 and Pi 3 as well! Most programs will also run on the B+ and Zero models, but might be a bit slow due to the limited computing power of the B+ and Zero.

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


In this lesson we learned how to install OpenCV 3 with Python 2.7 and Python 3 bindings on your Raspberry Pi 2 running Raspbian Jessie. I provided timings for each step so ensure you can plan your install accordingly.

It’s also worth mentioning that I provide OpenCV v2.4 and v3 install instructions for Raspbian Wheezy in the following posts:

If you run into any issues during the installation process, please see the Troubleshooting section above. Additionally, I would suggest watching the video tutorial at the top of this post to aid you in the setup process.

Before you go…

I tend to cover a lot of great computer vision projects using OpenCV and the Raspberry Pi, so consider entering your email address in the form below to be notified when these posts go live!

, , , , , ,

402 Responses to How to install OpenCV 3 on Raspbian Jessie

  1. Anastasios Selalmazidis October 26, 2015 at 12:56 pm #

    Great guide Adrian. I was looking forward for this. Worked like a charm for python2.7.
    I am going to install it now for python3.

    Thank you !!!

    • Adrian Rosebrock October 27, 2015 at 4:52 am #

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

    • Aldi December 9, 2015 at 11:54 pm #

      Hello Adrian thanks for your tutorial it was great. I’ve just followed trough your guide and I am able to do the example from your site but How can I run the example from the opencv ?
      Thank you for your attention

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

        Hey Aldi, I’m not sure what you mean by “run the example from OpenCV”? Most tutorials on this blog post assume you are executing a Python script via command line argument. I would suggest going through a more recent post, like this on on pedestrian detection to see how Python files are created and executed. Alternatively, if you’re just getting started using Python and OpenCV, Practical Python and OpenCV would be a good start.

  2. Supra October 28, 2015 at 6:30 am #

    It worked for Model B+ w/out using virtualenvwrapper by using python 3 merely.

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

      As I mentioned in the post, using virtual environments is entirely optional. But I do highly recommend it for Python development.

      • nathan January 13, 2017 at 1:25 pm #

        Im rather new to python so bear with me

        I can get open cv to run directly from the terminal but I can’t get it to work with the python shell. I’m assuming this is because the python shell has no idea that open cv is in a different “virtual environment”

        if I type in “import cv2” into the python shell it says

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

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

          By Python shell do you mean the GUI version of IDLE? If so, then you are correct — the GUI version of IDLE does not work with virtual environments. I would suggest using Jupyter Notebooks instead.

          • Colin Rutter February 4, 2017 at 6:36 pm #

            Adrian, i followed this tutorial to a successful install also only to realise when readibg this question that i would not be able to access openCV using idle ! (perhaps a warning in the guide itself would be handy) How can i ‘re-install’ without virtualenvs to allow opencv use with idle ?


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

            In general I wouldn’t recommend using IDLE or your system Python install. Use Jupyter Notebooks instead.

  3. Michael November 4, 2015 at 12:24 am #

    check. Thanks Maestro!

  4. Brian November 4, 2015 at 9:03 pm #

    When I try to unzip OpenCV it gets about halfway through and then I get a write error, asking if my disk is full. This is a brand new 16gig class 10 microSD with Jessie installed. It’ can’t possibly be full already. I tried the wget again thinking the download was corrupt but it still gives me a write error. Any ideas?

    Ah-ha! I was able to successfully unzip opencv.
    A critical step that you might want to mention before running the “unzip” command:

    Navigate to the Raspberry Pi Configuration Tool and select EXPAND FILESYSTEM

    Hope that helps someone!

    • Adrian Rosebrock November 5, 2015 at 6:38 am #

      Thanks for the tip Brian! I (incorrectly) made the assumption that everyone had already expanded their filesystems fully.

      • Matt November 21, 2015 at 12:55 pm #


        My Raspberry Pi 2 won’t allow me to expand the filesystem like Brian mentioned. I used NOOBS to install Raspbian Jessie and am using an 8GB micro SD but only 1GB is available once the operating system is installed.

        Any help would be much appreciated


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

          Normally to expand the file system all you need to do is run raspi-config, select the first option to “Expand filesystem”, and reboot. That should expand your 8GB card. Make sure you reboot after running the command. If you’re still not getting your file system expanded, either (1) your file system has already been expanded or (2) something very odd is going on and you should consult the Raspberry Pi documentation.

          • Nathan June 20, 2016 at 5:03 am #

            Hi Adrian,

            I have also run into this.

            The compiled code seems to up take more than 4GB disk space (that’s why people are having problems), but the installed libs take <100M. If we don't have enough space on the SD card, would using a USB stick or an external hard disk for the build directory work. Then after installing OpenCV, delete the build directory?

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

            Yep, that would absolutely work!

          • Samuel July 5, 2016 at 5:33 am #

            I also have a problem for not having not enough space on the SD card, how can i use a USB stick to accomplish compiling? i guess it’s not as easy as simply creating the build folder in the USB stick right…?
            Please help me Q.

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

            Actually, it is 🙂 Just download the OpenCV .zip files to the USB stick, create your build directory there, and let it compile.

        • Roger January 6, 2016 at 8:07 am #

          Howdy Matt

          I ran into the same issue you expereined; not being able to expand the memory on SD card running NOOBS version for rsapi.
          So, I decided to directly dd the rsapian os without all the oother os’es onto SD Card. I suggest this to you, too.
          Just download the raspian os and copy via dd command in Linux CLI or with an additional software under windows; sorry I am not aware of a good one since I am not working with windows.
          Google may help here.


          • Matt January 20, 2016 at 10:58 am #

            Hi Roger,

            Yes I have done the same and it works now.



    • Kevin February 8, 2016 at 12:50 pm #

      Are you sure you’re using the entire 16GB? Have you expanded the root partition, or are you running Jessie from the installer image? See or others on how to get the full 16GB

  5. Sigi November 15, 2015 at 4:54 pm #

    Hi Adrian,
    thank you for this great tutorial – it worked like a charm rpi2 – jessi – python3 – opencv3

    I have a script for circle detection that runs well on rpi2 – wheezy – python2 – opencv2.

    but with opencv3 it crashes at the line:

    ” wp = cv2.HoughCircles(blurred,,1,75,param1=50,param2=12,minRadius=7,maxRadius=9)) ”

    with this error message:

    ” AttributeError: ‘module’ object has no attribute ‘cv’ ”

    What can I do?

    Thanks in advance


    • Adrian Rosebrock November 16, 2015 at 3:35 pm #

      For OpenCV 3, you’ll need to change to: cv2.HOUGH_GRADIENT.

      • Sigi Weichenrieder November 17, 2015 at 4:01 pm #

        Hi Adrian,

        that worked :-)) Thank you!!!!!!!!!!!!!!!!!!!!


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

          Awesome, I’m glad it worked Sigi!

          • kk November 20, 2015 at 6:21 am #

            Hi, I tried with rpi2 – jessi – python2.7 – opencv3.0 installation as guided in this post.

            throws error message:
            AttributeError: ‘module’ object has no attribute ‘cv’

            Is similar change required? But,
            (“Correlation”, cv2.CV_COMP_CORREL),
            throws error either:
            AttributeError: ‘module’ object has no attribute ‘CV_COMP_CORREL’

          • Adrian Rosebrock November 20, 2015 at 6:36 am #

            Try using cv2.HISTCMP_CORREL.

  6. Nichten November 16, 2015 at 6:49 pm #

    Hello Adrian,

    I have successfully installed OpenCV 3 with Python 3.4 on Raspbian Jessie. thank, you for your great work :).

    But I have some problem with package python-serial. How to install it in virtualenv with python 3? I type command workon cv and I’m trying to install it, but the OS return information that it is already installed. But I can’t import serial in python3. I can do it with python2. What I’m doing wrong?


    • Adrian Rosebrock November 17, 2015 at 6:21 am #

      Congrats on getting OpenCV 3 installed, that’s great! I personally have not used python-serial before so I am unfamiliar with it. I believe in Wheezy that you had to install and run it as root, but with Jessie you can install it and run it as as normal user — is that correct?

      It sounds like you may have installed python-serial via apt-get which is why it’s not working for you. If you do:

      It should work for you.

      • Deepak January 11, 2016 at 11:25 pm #

        Hello Adrian,
        I am also facing the same issue with serial module. I have tried pip install pyserial. Even then I am not able to import the serial module to cv virtual env. Outside the virtual env, serial module is working fine. Please advice.

        • Adrian Rosebrock January 12, 2016 at 6:26 am #

          Since I am not a pyserial/GPIO user, I’m honestly not sure about the problem. I’ve added this to my queue as a potential new blog post to help resolve the issue.

          • Yvder February 23, 2016 at 5:12 pm #

            Please, have someone got the issue fixed? I am facing the same issue with pyserial in CV virtual env.

  7. Eddie Mitchell November 19, 2015 at 12:42 am #

    Beautiful walk through. worked like a charm and im super excited to dive into computer vision. getting your book[s] for christmas! keep up the awesome work Adrian!

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

      Thanks for the kind words Eddie! 🙂 I’m glad the tutorial worked for you.

  8. kk November 20, 2015 at 8:03 am #

    Hello Adrian,
    Thanks for the great work. With my installation of OpenCV 3.0 with Python 2.7 on Raspbian Jessie, I’m running your blog codes one by one. I’m really enjoying it.
    But I have a problem with installing “scipy”. “pip install numpy” works, but “pip install scipy” is failed. I wonder if I’m the only one who is suffering from the problem.
    It would be greatly appreciated if you could help me. Thanks in advance.

    • Adrian Rosebrock November 20, 2015 at 8:50 am #

      Did you get an error message when installing Scipy? And which Raspberry Pi model are you using?

  9. armin November 21, 2015 at 2:22 pm #

    Thanks for your tutorials, according to these instructions I’m trying to build and compile opencv3 on RPI2 running ubuntu mate 15.10, but i’m facing this error in compilation step:

    make file:136 recipe for target ‘all’ failed
    error 2

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

      Hey Armin — this tutorial is dedicated to installing OpenCV on Raspbian Jessie. Theoretically the same steps can be applied to Ubuntu Mate, but I have not personally tried them on Ubuntu Mate before. If you’re getting an error when running the make command, more times than not it’s due to a missing dependency. Make sure you have ran all of the previous apt-get commands to pull in the required prerequisites.

    • Marijn December 31, 2015 at 6:11 am #

      I ran into a similar error when ‘making’ with make -j4 on an Pi B+. I then did ‘make clean’ followed by the normal ‘make’ command. I took another 10+ hours, but finished without errors after that. And was able to finish the guide.

      • Adrian Rosebrock December 31, 2015 at 6:54 am #

        If you’re using a B+, then definitely only use make instead of make -j4. The B+ does not have four cores like the Pi 2 does so it makes running into dependency issues during compile a lot more likely.

  10. Nico November 22, 2015 at 6:28 am #

    Hello Adrian

    Thanks for your great walk throughs and tutorials. I successfully installed OpenCV3 and Python3 on my Pi2 with camera module and step 6 of this post returns the expected results.
    When I tried to run the basic motion detection script from your article ( nothing happend. I created the script, opened command line, changed to the virtualenv cv and tried to run the script with this command: python After hitting execute nothing happens for 2 seconds and then a new command line shows up waiting for input. No windows appear to show me the live camera view.
    The 2 seconds gap is probably the time it takes to import OpenCV. I tried to run the command as sudo, I have given the script different permissions and I have changed some code. Sadly nothing helped to solve my problem. Commands as raspistill work perfectly nice so there can_t be any camera issues.
    FYI I am working with the Pi’s GUI and not via SSH.

    I would greatly appreciate your help. Thanks in advance.

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

      The basic motion detection script uses the cv2.VideoCapture function which can require additional drivers for the Pi. I recommend using this post for motion detection since it is 100% out-of-the-box workable with the Pi and the Pi camera module.

      • Nico November 22, 2015 at 9:47 am #

        Thanks Adrian the home surveillance tutorial works like a charm!

        • Adrian Rosebrock November 23, 2015 at 6:39 am #

          Awesome, I’m glad to hear it Nico! 🙂

  11. Gary November 25, 2015 at 1:40 pm #

    Help! I just upgraded to Jessie, and something broke on the previously stable virtual environment I setup using your instructions for Python 2.7, Open CV 2.X and Wheezy.

    The upgrade to Jessie seemed to go well, but I am now seeing something broken with the virtual environment. Symptom:

    1. If I try and execute any python in the virtual environment I had previously, python import fails. For instance, trying to import diatomite is failing.
    2. If I go into Python, and manually import datetime, it fails.
    3. If I am NOT in the virtual environment, and go into Python, I can import datetime just fine.

    How can I repair the virtual environment here? Do I have to go through all of the steps above, or are their shortcuts that are faster and simpler?

    Thanks Adrian!!

    • Adrian Rosebrock November 25, 2015 at 1:50 pm #

      Hey Gary — just to clarify, did you upgrade from Wheezy to Jessie? Or did you do a fresh install of Jessie? I’m assuming the former. Unfortunately, I have never tried upgrading before, only installing on a fresh OS. Hopefully another PyImageSearcher reader can help you out with this one!

      • Gary November 25, 2015 at 2:12 pm #

        I upgraded. I followed all steps here which seems to be what most folks are pointing to for the best upgrade path. The upgrade seemed to go well.

        One other clue for any reader….. if I load the virtual environment, and then perform step 6 above to verify all is well, I get an error from Python on the import cv2 statement:
        “ImportError: numpy.core.multiarray failed to import”

        Odd. If I go to ~/.virtualenvs/cv/lib/python2.7/site-packages, numpy is still there.

        Makes me wonder if the bindings / sym-link is somehow trashed?

        hoping to avoid having to reinstall everything above. If anyone has any experience on upgrading a previously stable Wheezy virtual environment to Jessie, please let me know.


        One more thing, if I go to check the links setup in Step 5 they are still valid and showing appropriately in the virtualenvs directory as well as when using the readlink command. Hmmmmmmm…………

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

          As you suggested, it sounds like your sym-links have been destroyed. I can’t speak for all the other packages on your system, but for OpenCV you should be able to re-create them using the ln command like we did in this tutorial. At that point you should have OpenCV back.

          I hate to say it, but you’ll likely need to do a re-install. I’ve ran into issues where my sym-links where destroyed (on OSX, not Raspbian) and every since then I never an “upgrade” to the latest OS. If I want to upgrade, I have to commit fully and re-install from scratch. It takes awhile and it’s a lot of effort, but it’s the best guarantee that you’re development environment will be 100% setup correctly.

          • Gary Lee November 26, 2015 at 12:25 pm #

            A hint to others…. I reinstalled numpy and it seemed to get everything back in order. Weird. I also (just to be safe) decided to start fresh with a new install of everything. Thanks for a great tutorial for getting this done. If others decide to follow the upgrade path, reinstalling numpy may be the solution.

  12. Rob Jones November 28, 2015 at 5:23 pm #

    Adrian – This all worked *great* and I installed picamera[array] in the cv virtual env – I am able to read image frames from the camera, process them and output them

    I’m using python 2.7

    Now I want to bring RPi.GPIO into the mix … that works fine by itself outside the cv virtualenv as Jessie includes RPi.GPIO v0.6.0a3 by default – this gives you access to the GPIO pins without having to be root.

    But the cv virtualenv does not see this… and the latest version of RPi.GPIO available by pip is 0.5.11 which won’t cut it…

    How can I get v0.6.0a3 installed in the virtualenv ?
    Compile from source inside that env ?

    I’m not totally on top of pip / virtualenvs so I’m reluctant to just try it

    I’m hoping that you have already dealt with this

    thanks !


    I figured out the answer to my own question :^)

    You just manually copy over the system RPi package files into the virtual environment like this:

    $ cp -r /usr/lib/python2.7/dist-packages/RPi ~/.virtualenvs/cv/lib/python2.7/site-packages/

    • Adrian Rosebrock November 29, 2015 at 7:10 am #

      Hey Rob — for future reference you can use pip and install it into your virtual environment. This is the preferred method rather than copying files directly:

      $ pip install rpi.gpio

  13. Philip November 29, 2015 at 10:05 am #

    Hi Adrian,

    Thank you for your excellent tutorial. Everything worked perfectly except for a few errors installing numpy. Uninstalling and reinstalling fixed that. I also ran out of space building OpenCV as I was using Noobs. Formatting the SD card and just using the Jessie image file freed up enough space for the install.
    I’m just wondering if you have had any experience with drivers for webcams on the Pi? When I use any of my webcams and try to get/set attributes such as CV2.CAP_PROP_FPS, I get the following error:
    VIDEOIO ERROR: V4L: Property (16) not supported by device
    I’ve got the libv4l library installed and the WITH_LIBV4L boolean is set to ON in the opencv build configuration.
    I’ve googled but I haven’t come up with anything so far. Weird! :-/

    • Adrian Rosebrock November 30, 2015 at 6:35 am #

      Hey Philip — I’m sorry to say that I don’t have much experience using webcam drivers for the Raspberry Pi. I either use the actual Pi camera board or a Logitech 920C which works out of the box. Be sure to check this page of compatible USB webcams for the Raspberry Pi to see if there are any known issues with it.

  14. SK November 29, 2015 at 1:46 pm #

    Hi Adrian,

    There is excellent content on your website – thanks you so much!

    I have a request – will you be able to prepare a tutorial to install OpenCV with QT, VTK, and these additional performance libraries (CUDA, etc) for Raspberry Pi (all that it can support) and linux computers? Thank you for all you do, I am considering buying one of your packages.


    • Adrian Rosebrock November 30, 2015 at 6:33 am #

      It’s something that I can certainly look into.

  15. rap November 30, 2015 at 10:19 am #

    Thanks for the instructions! However, could you please tell if this will work for C++ as well as I wonder about the virtual environment part? If I install for Python 3 according to these instructions is the opencv library accessible for C++ from the default environment too or what should be done otherwise. Thanks!

    • Adrian Rosebrock November 30, 2015 at 7:07 pm #

      Once you run make install, you have OpenCV fully installed (as far as C++ is considered). The Python virtual environments have no impact on the C++ installation.

      • rap November 30, 2015 at 7:27 pm #


  16. Mau December 6, 2015 at 11:14 am #

    Hi Adrian, might I be able to compiling with raspberry b+ 512?
    Thanks for your tutorials.

    • Adrian Rosebrock December 6, 2015 at 11:18 am #

      This tutorial should work with the Raspberry Pi B+; however, I highly encourage you to use a Pi 2 for computer vision, especially if you want to do any type of video processing.

  17. Bob December 8, 2015 at 7:30 pm #

    Is it bad if I accidentally ran “make clean” again after the long “make” process but canceled it almost immediately?

    • Adrian Rosebrock December 9, 2015 at 6:53 am #

      Technically no, you can just run make clean again and let it finish cleaning up. However, you will need to run make again. Just to be safe, I would suggest deleting your build directory, re-creating it, and then running cmake just to make sure you are starting fresh.

  18. Birkan December 10, 2015 at 12:33 pm #

    can we delete opencv-3.0.0 and opencv_contrib-3.0.0 folders after succesfull installation?

    • Adrian Rosebrock December 10, 2015 at 2:22 pm #

      Yes, once you have OpenCV installed you can safely delete the opencv-3.0.0 and opencv_contrib-3.0.0 directories.

      • Birkan December 12, 2015 at 4:44 pm #


  19. mohamed abo amer December 12, 2015 at 6:50 pm #

    it says no enough space when downloading open cv
    (wget -O

    • Adrian Rosebrock December 13, 2015 at 7:31 am #

      If you’re getting an error that there is not enough space to download the .zip file, then your micro-SD card is filled. Either delete unneeded files to make room for OpenCV or make sure you have already ran raspi-config to ensure the entire micro-SD card is available for usage.

  20. Brian December 12, 2015 at 10:09 pm #

    Just to clarify, does this installation (OpenCV 3.0.0, Jessie, RPI2) run the OpenCV scripts in multi-core mode?

    • Adrian Rosebrock December 13, 2015 at 7:29 am #

      No, it does not directly leverage multiple cores. You’ll need to write your own scripts to distribute processes to multiple cores/threads if.

  21. Florian December 21, 2015 at 9:31 am #

    Hello Adrian,

    thanks for the great tutorial, but i have problems building with GTK+ support. I installed all the librarys and dependencies, but everytime i run the cmake command it remports back with “GTK+ No”.

    Do you have any idea?

    Thanks Florian

  22. deepak December 22, 2015 at 11:52 am #

    hello Adrian thanks for your great tutorial i am compiling open cv on my pi2 but i have got a question how to compile opencv-contrib can to tell me the steps please and how to install PIL once again thankyou

    • Adrian Rosebrock December 23, 2015 at 6:43 am #

      Hey Deepak, this tutorial already details how to compile and install opencv-contrib. Please see Step 2 where the OpenCV source code is downloaded and Step 4 where OpenCV is actually compiled.

      You can also install OpenCV into your virtual environment by using pip:

       workon cv
      $ pip install PIL
  23. Mahmoud Tolba December 23, 2015 at 11:11 am #

    Thanks for your great effort.
    May I ask you a question please?

    In case of using Raspberry pi type b, Should I change this command (make -j4) to be (make -j1) ?

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

      If you’re using the B, then just use make and leave off the -j switch.

  24. David Kadouch December 25, 2015 at 3:43 pm #

    I’m so grateful to the deep level of detail of your tutorials. It works like a charm.
    For the sport I tried first to install opencv 3.1 that was just released a few days ago. Alas it fails at the make stage with a ‘no targets’ error message. In fact no Makefile was created after ‘cmake’. I must say that it’s a bit frustrating that they break the install process for minor versions. It makes upgrading a real pain. Any insights here?

    • Adrian Rosebrock December 26, 2015 at 8:55 am #

      Hey David, please see my reply to Nelson Chen above.

  25. Nelson Chen December 25, 2015 at 7:34 pm #

    Thanks for great effort.

    I have a question for you. My opencv is v3.1.0, and when I try to run cmake, I got an error. I have tried several times and found that I had to remove the INSTALL_C_EXAMPLES thing from the command line, or it led to an error. What do you think of it?

    • Adrian Rosebrock December 26, 2015 at 8:55 am #

      I actually ran into this error myself when trying to compile OpenCV 3.1.0. Try removing -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON from the cmake command and it should generate a working Makefile.

      • Tom December 26, 2015 at 3:31 pm #

        It seems that the issue is in the c examples, I had to specifically add -D INSTALL_C_EXAMPLES=OFF before I got it to work, the rest of what you mentioned can stay in the cmake command without issues.

        Thanks for the great tutorial

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

          Thanks for the clarification regarding the cmake options!

    • Mario January 1, 2016 at 7:16 am #

      Thanks, I ran into the same problem.

      • Adrian Rosebrock January 1, 2016 at 7:23 am #

        You can fix the issue by removing the -D INSTALL_C_EXAMPLES=ON from the CMake command.

  26. Syed Tariq December 29, 2015 at 12:22 am #

    Excellent Tutorial. I followed all the steps and tested it by using a simple program to display video from a Sony PS Eye. The only issue I am facing is that the ‘import cv2’ is only recognized when my python program is in the site-packages subdirectory. I used the instructions in ‘Installing OpenCV 3.0 for both Python 2.7 and Python 3+ on your Raspberry Pi 2’ but I am using Raspbian Jessie OS. I did compare it to the instructions on the ‘How to install OpenCV 3 on Raspbian Jessie’ and did not see anything that would affect the installation. Any help would be appreciated.

    • Adrian Rosebrock December 29, 2015 at 7:57 am #

      Are you using the workon cv command to drop into the Python virtual environment before executing your script?

      • Syed Tariq December 29, 2015 at 4:36 pm #

        I am using ‘workon cv’. I can see ‘(cv) in the command line. I don’t totally understand virtual environment. I will read up on it and try debugging. It appears that the link is not global even though it has attributes lrwxrwxrwx. If I create a symbolic link in the directory from which I run the python script it seems to work.

        Thank you for your help.

        • Adrian Rosebrock December 30, 2015 at 7:00 am #

          If it works within your current working directory, then the sym-link inside the site-packages of the cv virtual environment is likely not right. I would delete the sym-link and then re-create it.

          • Syed Tariq December 31, 2015 at 2:48 pm #

            Adrian – Thank you. It is all working right now. It was a great tutorial and it helped me immensely.

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

            Awesome, I’m glad you were able to resolve the issue 🙂

          • Nanne Kuperus January 27, 2017 at 8:19 pm #

            I seem to have made a hash of things and have accidently already installed an incorrect cv2 symbolic link; how do I remove it?

            Entering ‘sudo rm -r /usr/local/lib/python3.4/site-packages/’ gives me the ‘no such file or directory’ error.

            Awesome tutorial, really happy with the easiness of steps. Just wish I had paid a bit better attention 🙁

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

            You can remove the sym-link using the rm command. Regarding your error, give your command another look — you’re trying to remove a file in your local directory (which doesn’t exist), hence the error.

        • Fra February 1, 2016 at 8:52 am #

          Hi Syed/Adrian, I have the same issue works within my current working directory, so even for me It appears that the link is not global.

          How did you fix it?

          I followed video and tutorial in any details…

          When I run my in ~ I get “ImportError: No module named cv2” otherwise in site-packages it is fine.

  27. Bill Kearson January 3, 2016 at 10:09 am #

    Great job,

    With your detailed instructions and the comments I have OpenCV3.1 and Python 3 working, I had to remove the C examples, INSTALL_C_EXAMPLES=OFF, from the cmake command and ended up having to use one core to avoid errors during the make.

    In the end everything is working.

    Using a 8G card I was almost out of space but I saved an image before and after removing the downloaded files and folders. Left me with about %55 free space. Not bad.
    A good time was had by all over two days. I did start over three times making noob mistakes: Expand the file system running raspi-config on first run.
    Now I have my pi running headless, wifi connection, ssh primary connection and vnc available with manual startup, if needed.

    Thank you

    • Adrian Rosebrock January 3, 2016 at 12:45 pm #

      Congrats on getting your setup working Bill! And I’m in the process of updating the OpenCV 3 install tutorials to mention removing the INSTALL_C_EXAMPLES=OFF flag. It looks like you beat me to it! 🙂

  28. Sebastian January 3, 2016 at 10:38 pm #

    Hi Adrian

    First I wanna thank you for the tutorials, I tried for a long time install OpenCV in a Raspberry, but I can’t do it. I followed your instructions, and I don’t have any error during the process, but the problem is when I tried to import the library in Python, I have the next error: “ImportError: dynamic module does not define init function (initcv2)”, do you have any idea to solve that?. Thanks.

    • Adrian Rosebrock January 4, 2016 at 6:48 am #

      I think one of two things happened. Either you compiled OpenCV with Python 2.7 support and then tried to import OpenCV with Python 3, or vice versa. Make sure you are in the cv virtual environment prior to importing OpenCV. If that still doesn’t work, go back to the CMake command and make sure you are compiling OpenCV for your intended version of Python.

      • Sebastian January 5, 2016 at 9:32 am #

        Hi Adrian

        Yes, it was my problem, thank you so much. as we say in my country: “you deserve the sky and more”, thanks!!!

        • Adrian Rosebrock January 5, 2016 at 1:53 pm #

          Nice, congrats on resolving the issue 🙂

  29. Tim Farnworth January 4, 2016 at 10:50 pm #

    Hi Adrian,

    Thank you for these tutorials man. They are great!

    I installed OpenCV last night and paused the installation just after the make -j4 command. I resumed this morning. Other then adding -D WITH_OPENMP=ON and set -D INSTALL_C_EXAMPLES=OFF to the cmake install, I have followed the tutorial closely. But for some reason I am having issues.

    cv2.imshow() and cv.ShowImage() dont open any image windows. I’m getting frustrated because I cant see where my code is wrong.

    Here is a bare bones version of my code:

    The code terminates in the cv2.imshow() function and does not reach the print “Image shown” line.

    Do you have any ideas what could be wrong? Cheers

    • Adrian Rosebrock January 5, 2016 at 6:46 am #

      It’s very strange that the “Image shown” text is never printed to your screen. I would point that unless you are using a video stream, you likely don’t want to use cv2.waitKey(1). This will only wait 1ms before automatically closing the window. You should use cv2.waitKey(0) to wait indefinitely for a keypress.

      Since you deviated slightly from the tutorial, I would suggest doing two things as a sanity check:

      1. Blow away the install and start from scratch. Specifically, pausing during make -j4 might have introduced a hard to track down dependency issue. Allow OpenCV to compile without pausing the process.
      2. Secondly, re-compile with -D WITH_OPENMP=OFF. I’ve never encountered an issue with OpenMP before, but since it deviates from the tutorial, I think it’s good to perform a sanity check.

  30. Nicolas Roux January 6, 2016 at 2:28 pm #

    It’s a great guide, I think I already used one of your tutos in the past so a thank you was long overdue.
    Here it is:

    Thank you, keep up the good work.

    • Adrian Rosebrock January 6, 2016 at 2:31 pm #

      Thanks for the kind words Nicolas 🙂

  31. Malef January 7, 2016 at 10:43 am #

    i try to compile opencv 3.1.0
    but it stalls after 5 mins [21%] on opencl/perf_arithm.cpp
    6%CPU and 59% MEM
    Using Swap Total 2097148 6800 used
    what can i do waiting longer or is there something wrong?

    is compliling with OpenCL makes sence ? is the GPU is used?
    Thanks for help

    • Adrian Rosebrock January 7, 2016 at 12:39 pm #

      That’s strange that the compilation has stalled. Are you trying to compile with OpenCL support? If so, I would suggest disabling and trying again.

      Also, try using compiling using only a single core with: make

      This will avoid any potential dependency/race conditions with the compile.

      • Malef January 8, 2016 at 5:55 am #

        i switched of the opencl now the same thing at 82% building python module cv2
        kswapd uses most time the cpu() and the cc1plus process
        and then it stalls
        maybe a swapping problem and then a crash?

        • Adrian Rosebrock January 8, 2016 at 6:28 am #

          How long into the compile? Also, which Pi hardware are you using?

          • Malef January 11, 2016 at 5:44 am #

            Raspberry B first version 256MB RAM 128MB Split
            Swap on SD with 3 times Ram
            I try it now with 16MB memory split for GPU

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

            The RAM definitely sounds like the issue. These OpenCV + Raspberry Pi tutorials are intended for either the B+ or the Pi 2. I have never tried with the B, simply because it has too little RAM and isn’t the most suitable for computer vision and image processing. If at all possible, you should try to get a Pi 2.

  32. Paul Maher January 10, 2016 at 6:11 am #

    Thanks for posting the detailed instructions which have been invaluable to me. I have a question though about the virtual environments. I’ve not used them before so I managed to tie myself in a knot.

    If I switch to the CV virtual env as in your instructions everything works fine… but if I try to import the GPIO library it says its not found.I also cant use ‘sudo’ to run my OpenCV python programs (which I will need to do as I want to access use the GPIO ports as well as the camera). So at the moment I can *either* use the CV virtual env and get OpenCV OR not use the virtual env and get access to sudo and GPIO. Not sure how to dig myself out!

    Any advice gratefully received!

    • Adrian Rosebrock January 10, 2016 at 7:40 am #

      I’ve actually addressed this problem a few times on the PyImageSearch blog. First, you should read my reply to Rob James above. Secondly, do a ctrl+f and search this page for GPIO (especially in the comments section). You’ll notice that the solution is to launch a root shell, create a virtual environment for the root user, install OpenCV into it, and execute your script.

      The important point is that you need to actually launch a root shell rather than just using sudo to call your script.

      Finally, you could try ignoring the virtual environment entirely and ensure both OpenCV and NumPy are installed into your normal Python site-packages directory. This will also solve the problem.

      • Paul Maher January 11, 2016 at 3:30 am #

        Adrian, thanks for the prompt reply. I will try omitting the virtual env steps and see how I get on. Thanks again for the reply.

  33. manish January 10, 2016 at 7:44 am #

    how to import cv2 on python shell …
    I followed your all steps and it goes correct..
    but I didn’t get on shell as it shows

    no module name cv2

    I m using
    Raspi-2,model B
    with NOOBS

    • Adrian Rosebrock January 10, 2016 at 8:18 am #

      Please see the “Troubleshooting” section at the bottom of this post. I would also encourage you to watch the installation video at the top of this post and follow along with the exact commands that I perform. This will ensure that OpenCV is properly configured and running on your system.

  34. manish January 10, 2016 at 8:25 am #

    Sir I followed your video and at the terminal it is going right..
    but it didn’t work on python shell….
    I m a beginner in Raspberry pi …
    Can you take me out of this??

    it was all right but not no shell

    Opencv is installed on my pi
    I checked it through terminal as you mentioned in last step..

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

      Hey Manish, so just to clarify OpenCV is indeed installed on your Pi?

      If it’s not, then I would suggest going through the “Troubleshooting” section of this post. I’m also not sure what you mean by it’s working in the terminal, but not in the Python shell.

      • Carlos Alberto Giron May 2, 2016 at 2:15 pm #

        Hello Adrian and thanks for the tutorial.

        I have the same problem, if I open the terminal and follow the steps source ~/.profile –> workon cv –> python –> import cv2, it does it without any problems. But if i open a script directly from python and start with import cv2 then i get the error: ImportError: No module named cv2.

        I believe it must have something to do with my python script not running on the profile and the cv environment but i don’t know how to make it run from there.

        If you can help me i would really appreciate it.

        • Adrian Rosebrock May 2, 2016 at 7:46 pm #

          Hey Carlos — this actually is the intended behavior of using a virtual environment. Your cv environment is meant to be sequestered and independent from the system install of Python. I detail how virtual environments work in the first half of this blog post.

  35. Arash January 10, 2016 at 4:05 pm #

    Thank you for the tutorial.
    As mentioned in the update I got the make error after (C_EXAMPLES=ON) so I removed the build folder and redid the cmake with C_EXAMPLES=OFF however I am still getting “No targets specified and no makefile found. Stop.”

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

      After you removed the build directory, you need to re-create it and enter it before running CMake again:

      From there you should run the CMake command.

  36. manish January 12, 2016 at 11:08 am #

    sorry to have a late response sir.
    I got it .and solved the problem .
    thanks for your support and suggestions..

    and thanks for this is really helpful.

    • urby March 12, 2016 at 11:57 am #

      Hello manish.
      What did you do to solve the problem? Because I went trough troubleshooting and I still get the error.

  37. Kewin January 14, 2016 at 11:50 am #

    I have 8 gb sd card with raspian jessie. When I install raspian on it, the folder manager of rasperry pi shows the free space about 700mb. Is that normal. If 8gb is not enough I will buy a 32gb sd card. Because I want to install opencv and python and the disk space is not enough. Please help me

    • Adrian Rosebrock January 16, 2016 at 9:40 am #

      700MB is very much borderline. Make sure you have ran raspi-config and expanded your Raspberry Pi filesystem to use the entire micro-SD card. Otherwise, if you have already expanded your filesystem, I would suggest either (1) deleting files to make extra room or (2) getting a large micro-SD card.

  38. Hilman January 15, 2016 at 6:21 am #

    Hey Adrian.
    I want to ask something. Is a 4GB microSD card big enough to install Raspbian Jessie, OpenCV3, and all the things you’ve wrote above? What if I want to make project on the Raspberry Pi (by using the Raspberry Pi camera and your tutorial of course), will it still be big enough?

    • Adrian Rosebrock January 16, 2016 at 9:34 am #

      Realistically, a 4GB card is not large enough — I would suggest using an 8GB card.

  39. Joseph January 16, 2016 at 3:03 pm #

    I followed every step (doing Python3) but when I got to Step #5: Finishing the install, my python3.4 directory is empty. python2.7 has a bunch of files, but not I looked back at all the commands I entered and everything looks right. Not sure what went wrong or where I could backtrack to.

    • Adrian Rosebrock January 17, 2016 at 5:29 pm #

      Be sure to take a look at the “Troubleshooting” section of this post. Be sure to take a look at the build/lib directory as well.

      • Manu March 28, 2016 at 12:48 pm #

        Hi Adrian,

        Thanks so much for your detailed blog on how to install OpenCV, it’s very good.

        I have had a similar issue to Joseph, and I found the file under build/lib folder as you explained.

        I copied the file to /usr/local/lib/python2.7/site-packages and proceeded with Step #5 as you explained.

        Everything seems to work just fine. Am I right to think the system is installed correctly now?


        • Adrian Rosebrock March 28, 2016 at 1:24 pm #

          As long as you ran make install and then manually copied over the file, then yes, everything is installed.

  40. mai January 20, 2016 at 7:26 am #

    went though make with no error.
    However, make install gives following errors:
    How do I post a screen shot?

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

      I would suggest uploading an image to imgur and then posting the link.

  41. Vishwa January 24, 2016 at 3:51 am #

    Followed the guide till the end, I can import cv2 in terminal but not in IDLE, none of the comments helped. There is a file in site-packages. I don’t understand what you mean by running it in the cv virtual environment. Help

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

      To access the Python virtual environment, you need to use:

      $ workon cv

      And this will drop you down into the cv virtual environment. You can then use the terminal version of IDLE. The GUI version doesn’t properly work with the Python virtual environment.

  42. zevero January 29, 2016 at 9:57 am #

    If you end up not having enough space like me on raspian (on 8GB) try to free some space:

    500MB: sudo apt-get  remove –purge wolfram-engine
    100MB: sudo apt-get  remove –purge scratch
    xxxMB sudo apt-get clean autoclean

    • Adrian Rosebrock January 29, 2016 at 12:53 pm #

      Thanks for sharing the tip!

  43. mikel January 31, 2016 at 1:39 am #

    thank you so much!

    • Adrian Rosebrock January 31, 2016 at 8:55 am #

      No problem Mikel, I’m happy the tutorial worked for you.

  44. Fra January 31, 2016 at 5:01 am #

    Hi Adrian,
    I followed your instructions until step 4. Once I run CMake I checked Python 2 and 3 section.
    Unfortunately for me both have “packages path” set to “lib/python3.4/site-packages”

    What can be wrong?

    • Adrian Rosebrock January 31, 2016 at 8:57 am #

      To start, make sure you are in the cv virtual environment prior executing CMake:

      $ workon cv

      Otherwise, assuming you are trying to build Python 3 bindings, it should still be okay. You can always copy/sym-link into the site-packages of the virtual environment after compiling.

  45. PetriK February 2, 2016 at 7:16 am #

    excellent instructions, suggest also adding

    sudo apt-get purge wolfram-engine

    to release some space on the 8Gb sd car, without removing there is not enough space to install opencv.

  46. PetriK February 2, 2016 at 3:39 pm #

    Another suggestion for installation – just learned that when mounting usb disc as /build directory the filesystem must be ext4. If filesystem is FAT then libraries can not be build on due to user rights error. Needed to have an external disc as running short of memory during make even after removing wolfram-engine.

    Looks like the /build directory needs around 1.5gigs free space for the make being successful. So altogether 3gigs free space on sdcard may not be enough for the full opencv installation, so needed an external usb drive for /build.

  47. Hilman February 3, 2016 at 7:20 am #

    Hey Adrian, I have a question.
    When I entered this command:

    “wget -O” (I changed the 3.0.0 to 3.1.0, that is the latest one isn’t?)

    I get the “ERROR 404: Not Found”. I can’t even go to the link in the command manually using my laptop’s browser. How can I fix this?

    Thanks in advance.

    • Adrian Rosebrock February 4, 2016 at 9:19 am #

      That’s very strange that the .zip is 404’ing. It’s downloading perfect on my machines. Try cloning down the entire OpenCV repository and then checking out the 3.0.0 version:

      • Hilman February 6, 2016 at 1:41 am #

        Already figured it out. It was DNS issue…

  48. greg February 5, 2016 at 2:36 am #

    Uhh I’ve been using linux for about 15 years and I can’t get this to compile, I guess I’m retarded. Gave up after 3-4 hours of messing around.

    • Adrian Rosebrock February 5, 2016 at 9:19 am #

      Which version were you trying to compile for? Python 2.7 or Python 3? Also, is that your full output from CMake? Normally there is a longer trace.

  49. GINO February 7, 2016 at 4:05 am #

    i dont have “.profile”in my directory how to fix

    • Adrian Rosebrock February 8, 2016 at 3:53 pm #

      If you do not have a .profile file in your home directory, create it using an editor such as nano:

      $ nano ~/.profile

      • ChicagoChuck April 27, 2016 at 2:50 pm #

        You can also navigate with the GUI editor (the file cabinet at the top of rasbarian) and go through the Directory Tree to the home/pi folder. Once there, right click and check on “show hidden” which will show the file.

        So, the .profile is there, you just cant see it to edit it. If you create another one, you are going to overwrite your original one. Once you uncover it through this method, then you can click on the file and edit .profile

        Also, the path that you have as source ~/.profile can also be accessed through:

        source /home/pi/.profile if the source ~./profile isn’t working.

        • Adrian Rosebrock April 28, 2016 at 3:17 pm #

          Thanks for sharing the GUI editor tip! As for the ~/.profile, that is simply a shortcut for the full path: /home/pi/.profile. As long as you are logged in as the pi user, then source ~/.profile should work.

  50. Girish Kumar February 8, 2016 at 12:00 pm #

    Thanks Adrian, Open CV 3.0.0 installed like a breeze, great write – Keep up the good work


    • Adrian Rosebrock February 8, 2016 at 3:44 pm #

      Fantastic, I’m glad the tutorial worked for you Girish! 🙂

  51. liudr February 14, 2016 at 1:57 am #

    Thank you Adrian! It took me a few tries on RPI 2B jessie before I eventually got to the finishing line with python 2.7

    As of 2/14/16, if one updates and upgrades jessie before following your tutorial, he needs a 16GB card. My 8GB card got only 4GB left with the 11-25-15 jessie distro and that shrank to 2GB right after upgrade. The opencv build needs about 2.4GB. I ended up mounting a hard drive for the build folder. My build took 7 hours to make, which is way above your mark. Will try to install opencv for python 3.5 that I compiled. It’ll be a challenge. I installed it in an opt subfolder in /usr/local

    • Adrian Rosebrock February 14, 2016 at 9:49 am #

      Thanks for sharing your install experience!

  52. jamaludin indra February 14, 2016 at 2:51 am #

    thank’s for your tip
    when i entered this command

    pip install numpy

    i get error:
    ImportError: No module named pip

    how can i fix this..?

    • Adrian Rosebrock February 14, 2016 at 9:48 am #

      Please see Step #3 of this tutorial to install pip.

  53. Thomas Overby February 17, 2016 at 10:15 am #

    This might be one of the most structured tutorials i have ever seen. Thank you for the effort and the time you spent to make this quality guide! Really, really nice work. Again, thank you for this!

    • Adrian Rosebrock February 17, 2016 at 12:33 pm #

      Thanks for the kind words Thomas, I’m happy the tutorial worked for you 🙂

  54. Alex March 2, 2016 at 2:30 pm #

    Hello Adrian,
    I am on step 4 trying to compile Opencv but when i enter the command

    cd ~/opencv-3.1.0/

    it returns

    bash: cd: /home/pi/opencv-3.1.0/: No such file or directory.
    I replaced the 3.0.0 with 3.1.0 for both opencv and opencv_contrib.
    I followed all the steps and I am in the (cv) virtual environment.
    Do you have an idea why this might be happening?

    • Adrian Rosebrock March 3, 2016 at 7:07 am #

      You’ll need to double check where you downloaded the original opencv-3.1.0 file to. It seems like it was not downloaded to your home directory. If you did download it to the home directory, you need to unzip it first.

  55. Carlos March 3, 2016 at 9:59 am #

    “make -j4” crashes my Raspberry Pi 3 instead I used “make” only (no other parameters).

    • Adrian Rosebrock March 3, 2016 at 11:09 am #

      Thanks for sharing. My Raspberry Pi 3 should be here soon and then I’ll be testing this tutorial — and possibly creating a new one, if need be.

  56. Mark March 3, 2016 at 4:39 pm #

    ok i think the problem was I used sudo pip install numpy which told me it was already installed but I assume using sudo it wasn’t checking the cv environment?

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

      The cv virtual environment is entirely independent than your system install of Python (and associated packages). Therefore, you need to install NumPy into the cv virtual environment as the blog demonstrates how to do.

  57. Vit Hovorka March 4, 2016 at 5:34 am #

    Firstly I would like to express my respect to you concerning your great contribution to machine vision community. Hopefully some day I’ll gain enough knowledge in machine vision in order to be also useful to community 🙂 But now I’m on beginning of my learning.

    And I would like to thank you specially for this detailed tutorial.

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

      Thanks so much for the kind words Vit 🙂

  58. MichauPe March 6, 2016 at 3:33 pm #

    Hello Adrian, hello All.

    First of all – great tutorial. I`ve installed openCV on rpi2 with success. Now I have rpi 3 and all have been going well since cmake part. I cant find why it throws a error: “CMake Error At CMAkeLists.txt.:107 = Cmake fails to determine the bitness of target platform”.

    I have fresh installation of Jessie , cmake version is 3.0.2. In txt file is statement tha this is a popular CMake conf mistake. What i have to do now, please help 😛 ?

    • Adrian Rosebrock March 7, 2016 at 4:12 pm #

      I don’t own a Raspberry Pi 3 yet — mine was placed on back order, unfortunately — so I can’t speak directly to what the problem is. Once I have a Raspberry Pi 3, I’ll try the tutorial myself and create an updated version if need be.

      • MichauPe March 8, 2016 at 6:01 pm #

        In order of deep investigation i have managed to install openCV 3.1.0 on rpi 3 with this cmake command first:

        and make command without -j4.

        I wish this can save a night to somebody 🙂

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

          Thanks for sharing Michal! It looks like the main addition is CMAKE_SIZEOF_VOID_P. I presume this is likely because of the 64-bit architecture of the Pi 3. Once I get my hands on a Raspberry Pi 3 (I have an order placed, it’s in the process of shipping), I’ll be sure to compile OpenCV myself and likely create a separate tutorial.

  59. Yazheng March 7, 2016 at 10:38 pm #

    Hello Adrian. Thanks for your great work!
    Followed your tutorial, I have successfully installed OpenCV 3 with Python 2.7 on Raspbian Jessie. But when I ran the sample file called, there was a warning: unable to open video source. It cannot display the video captured by picamera or even the picamera cannot capture the object. Do you have any idea about this condition? Thanks again.

    • Adrian Rosebrock March 8, 2016 at 4:14 pm #

      I would double check that your Raspberry Pi camera module is connected properly. Also make sure that you have ran raspi-config, enabled the camera module, and rebooted your system.

  60. Jasper March 8, 2016 at 7:35 am #

    I got some errors with cmake. I thought some dependencies were missing, but no: turns out that I overlooked the OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules setting which was set to the wrong version (I use 3.1.0). I also changed all settings for building examples to OFF. Now cmake runs fine.

    Maybe this will help some other poor fellow who, like me, knows nothing about compiling…

    Thanks Adrian for the great tutorial!

    • Adrian Rosebrock March 8, 2016 at 4:13 pm #

      Thanks for sharing Jasper!

  61. anu March 11, 2016 at 5:47 am #

    do we need net connection for make -j4 command in installing opencv for raspberry pi

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

      No, the make -j4 command does not require an internet connection. It is only used to compile OpenCV after all dependencies have been downloaded and installed.

  62. Prateek Xaxa March 12, 2016 at 2:17 am #

    THANK YOU very much … I cannot describe how much I needed it

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

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

  63. mike March 12, 2016 at 1:49 pm #

    rpi-update killed by SD card. Using a brand new RasberryPi3.

    I had to erase the card and reload raspian. Not sure that this step is required!

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

      Thanks for the tip Mike. I’ll have my Raspberry Pi 3 later this week and I’ll be sure to test the install tutorial (and likely create a new one specific to the Raspberry Pi).

      • Kevin March 14, 2016 at 2:42 pm #

        This past weekend I have successfully and painlessly installed OpenCV 3 on my Raspberry Pi 3 following this post.
        fyi I also previously discovered that sudo rpi-update screwed up my card when I tried it on the latest Jessie version from the RPi website. You might want to consider removing that line from your blogpost.
        I also successfully and painlessly got my RPi camera working by following your blogpost “Accessing the Raspberry Pi Camera with OpenCV and Python”
        I am now working through my copy of Practical Python and OpenCV + Case Studies on my RPi.
        So far I’ve been following it using the cv python environment but I’ve now come to a small hurdle at Chapter 7 Histograms where there appears to be no module matplotlib in the cv environment.
        How easy is it to install matplotlib in the cv environment and how is it done ?
        Is the same situation likely to be the case when I move onto the later Chapters and Case Studies which use mahotas, scikit-learn and scikit-image ?
        As an interim workaround I have installed matplotlib directly onto my RPi so I can run the code in Chapter 07 but only outside the cv environment. I would like to know how to do it the right way.
        I am so glad I’ve found your blog. Many thanks

        • Adrian Rosebrock March 14, 2016 at 3:10 pm #

          Hey Kevin, thanks for the comment. My Pi 3 will be here in the next few days, then I can run through the tutorial myself and I’ll make any necessary updates (such as removing the rpi-update command).

          Also, congrats on getting OpenCV installed on your Pi along with the camera module setup, that’s awesome! As for installing matplotlib (or any other package that is pip-installable), you just need to enter the cv virtual environment first and then install:

          This will install matplotlib into the cv Python virtual environment.

          If you run into any with matplotlib, I would suggest consulting this blog

  64. nancy March 13, 2016 at 1:11 pm #

    thank you for ur video:). everything went well but when i gave import cv2 it shows importerror: no module named cv2 in the terminal but when i change the path using cd usr/bin/local/python2.7/site-packages/ there is no error in import cv2 but when i run in idle again it shows error:( what shoukd i do now?

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

      Please see the “Troubleshooting” section of this blog post. It sounds like you are not in the cv virtual environment prior to importing OpenCV:

      • nancy March 14, 2016 at 11:58 pm #

        I used your commands
        $ workon cv
        $ python
        >>> import cv2
        still i get error as
        importerror: no module named cv2

        • Adrian Rosebrock March 15, 2016 at 4:41 pm #

          It seems like you forgot to sym-link the file into the site-packages directory of your virtual environment. Go back to Step #5 and make sure the sym-link is valid and working.

          • nancy March 16, 2016 at 4:08 am #

            yeah i worked.i did what you have mentioned in step5 for python2.7

        • GAURAV PUROHIT July 29, 2016 at 6:18 am #

          i think you forget to did source and workon cv command
          then only it will work

  65. Stefan Arnold March 14, 2016 at 11:19 am #

    Very nice and smooth installation !!
    Thank you very much !!

    Minor problem – when you don’t stick to the tutorial literally:
    When you work on an external file-base due to space-limitations on the internal SD: Be careful with the link/mount to your filesystem. E.g. “symlinks” will irritate the make-process.

  66. Anu March 17, 2016 at 2:09 am #

    Smooth installation.I installed it and it is working properly.Thank you so much for your tutorial

    • Adrian Rosebrock March 17, 2016 at 7:03 am #

      No problem, I’m happy the tutorial worked for you 🙂

  67. vj March 19, 2016 at 4:48 am #

    hi adrian… thanks for ur video!!!!:) my code shows ” no module named drawmatches” when i try to import sift and surf it shows no module named “sift” plz help me 🙁

  68. Red March 19, 2016 at 12:45 pm #

    I would go for a cross-compilation here. When building OpenCV it tends to eat up a lot of resources – CPU, RAM and storage. The first two one can cope with but the last – storage – can actually lead to failure in finishing the build process. And even if able to build it there might not be enough space left on the SD card for the installation because it copies the files from the build folder to their respective places in the system.

  69. oussema March 19, 2016 at 2:28 pm #

    raspberry pi2
    raspbian jessie

    AttributeError: ‘module’ object has no attribute ‘SURF’
    you can help me please

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

      This blog post will take care of the problem 🙂

  70. Steve Ryan March 19, 2016 at 6:53 pm #

    Adrian- AWESOME tutorial! -and this from someone who never uses uppercase. Seriously, this was fantastically detailed and spot-on procedure to get OpenCV working on RPi Jessie. I stumbled quite a bit until I found this post, and then in a mere 2 hours (1.8 of which was waiting for the actual build) I was done!

    Kudos, my friend- I am deeply in your debt!

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

      Thanks Steve! I’m so happy the tutorial helped you out! Congrats on getting OpenCV installed on your Pi, enjoy.

  71. Evgeny March 21, 2016 at 10:14 am #

    Hi Adrian,

    Just wanted to thank you for this amazing tutorial and your books! Bought the course on Sat, already done with the first book and now I’m enjoying Case Studies. These are definitely worth buying.
    One q comment – there will be some changes needed in this tutorial in order to make everything work smoothly on RPI 3. You already gave some explanations in the comments, but this also would be great to have some notes on step 4/5/ somewhere else.


    • Adrian Rosebrock March 21, 2016 at 6:36 pm #

      Thanks for the comment Evgeny — and it’s great to hear that you are enjoying Practical Python and OpenCV! I’m actually just wrapping up an install tutorial for the Raspberry Pi 3, so be sure to keep an eye on the PyImageSearch blog, it should be published in the next 3-4 weeks.

      • gaspar teleki March 22, 2016 at 1:45 pm #

        Adrian Rosebrock!

        I spent days trying get opencv working on my raspi B+.
        No luck.
        I have a job and a nice family so im not willing to spend more time fight the compatibility chaos with raspberry. I want to test my ideas on the pi machine, have fun…thats all:)
        Do you know of any existing wheezy/jessie image (with WORKING opencv/python2.7 on it) i could download?



        • Adrian Rosebrock March 22, 2016 at 4:13 pm #

          I’m sorry to hear about the install problems, that is super frustrating! At the current time, I don’t have a downloadable Raspbian .img with OpenCV + Python pre-configured and pre-installed, but that’s something I’m currently working on. Expect to have an announcement within the next couple of weeks 🙂

          • gaspar teleki March 22, 2016 at 7:12 pm #

            Dear Adrian,

            Thanx for your reply for both of my messages.

            Happy to hear that there might be a professional computer vision image avaible.
            I think many people is googling around to find one.

            According to free space:
            i removed libre office by:
            sudo apt-get remove –purge libreoffice*
            Deleted the opencv folders and zip fles.

            Mentioned Error:
            Pip install scipy takes bunch of hous to “wheel” and finishes with error for me. Sorry i couldnot provide error…formatted the sd after that. To archive the same error i would need ti try to install again.

            Virtual environment speed:
            It seemd to me without measuring that executing a py took longer in the virtual enviroment. No measurement i made!!! So its not official.
            How could i measure it? Do you have any suggestion?
            Have a nice day!

          • Adrian Rosebrock March 24, 2016 at 5:22 pm #

            Thanks for the tip on removing Libre Office, I’ve made note of that. As for profile Python code (and associated binaries), I would consult the Python documentation.

  72. Gaspar Teleki March 22, 2016 at 3:51 am #

    Dear Adrian!

    You are doing a great work doing tutorials!
    Ive installed opencv310 on my pi B+ with the above given method.
    it looks ok.

    I have some issues after install:

    1. No free space left on my 8 gig card (i guess i should delete opencv zip files and the unzipped folders, but im not sure)

    2. Cant do :pip install scipy + pip install matplotlib (i get tons of errors) = cant run my .py files requiring matplotlib.

    3. virtualenv seems to slow down the pi and makes things complicated (not foolproof).
    Is there any advantages an average user can get from virtualenv?

    Plesase reply me if you have the time.



    • Adrian Rosebrock March 22, 2016 at 4:22 pm #

      1. After you have compiled and installed OpenCV, you should absolutely delete the .zip files and the original source code directories. This will free up a bunch of space. You might also want to consider deleting the Wolfram Engine to free up space, as well as Open Office if it’s installed.

      2. It’s hard to tell what the error message is without seeing your error message.

      3. The virtual environment will not slow down the Pi at all. Do you have any benchmarks that make you think that? The primary benefit of using virtual environments is that you can create a sequestered, independent environment for each project you work on. This allows you to have different versions of various libraries on the same machine, all without conflicting with each other.

  73. Dave March 23, 2016 at 5:13 pm #

    Hi, I was successful in installing opencv and running it. However, once in the virtual environment I am unable to import any additional modules (ie. PIL). How do I import additional modules once in the virtual CV environment?

    Thanks for your help.

    • Adrian Rosebrock March 24, 2016 at 5:14 pm #

      You need to install each Python module into your virtual environment. Keep in mind that the virtual environment is independent from all other Python installs in your system. For example, to install PIL (or the more preferred Pillow):

  74. FELIPE MARTINEZ March 24, 2016 at 3:01 pm #

    I wrote ” wget -O ” and there is a problem “Cannot write to ‘’ (No space left on device)” Whatt hapen? help me

    • Adrian Rosebrock March 24, 2016 at 5:09 pm #

      Please see my reply to “Mohamed” and “Matt” above. You either need to (1) expand the filesystem on your Pi using raspi-config or (2) free up space on your Pi by deleting unneeded files. In an extreme case, you might need to purchase a larger micro-SD card.

  75. ravishankarkm March 28, 2016 at 8:38 am #

    Hi, I have followed your steps …………..while doing make -j2 its showing NO space left on device …………. complilation terminated ………. and i am using raspberry pi B+ board.


    • Adrian Rosebrock March 28, 2016 at 1:30 pm #

      Have you expanded your Raspberry Pi to use the entire filesystem? Make sure you executed raspi-config, selected the first option to expand the filesystem, and then rebooted your Pi. Ideally, you should have an 8GB or 16GB card when installing OpenCV.

      • ravishanakarkm March 30, 2016 at 2:09 am #

        yes. I have expanded the filesystem but no use . But i have not tried on 16 GB . Let me try. thank you for replay.


  76. Alex March 30, 2016 at 9:25 am #

    Worked for me on a Raspberry Pi 3 running Raspbian Jessie with OpenCV 3.1.0
    Thank you.

    • Adrian Rosebrock March 30, 2016 at 12:43 pm #

      Awesome, I’m happy to hear it Alex! 🙂

  77. ravishankarkm March 31, 2016 at 11:36 pm #

    Thank you . Yes it is working now. This time i used 16GB.


  78. sathya April 1, 2016 at 8:41 am #

    Hey, Im looking for instructions that will help me Install cv2 (OpenCV) with bindings to Python 2.7 in RASPBIAN JESSIE. PLEASE HELP. DESPERATE. FOR A COLLEGE PROJECT.

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

      Please see the blog post you are commenting on. It demonstrates how to install OpenCV (cv2 bindings) with Python 2.7 and Raspbian Jessie.

  79. Manasi Shinde April 3, 2016 at 8:30 am #

    i have tried and tried this tutorial about 7 times and finally open cv3 was installed!! i have spent more than 24 hours of my life. This tutorial saved me. Thanks a million.

    i completed my installation completely, i did a test run and it worked. but when i opened the gui via vnc, the cv2 module did not exist.
    i guess this is 7th time unlucky.

    • Adrian Rosebrock April 3, 2016 at 10:18 am #

      Congrats on getting OpenCV installed Manasi! Also, after opening a GUI via VNC, make sure you use the workon command to access your Python virtual environment before you try to import cv2:

      • Manasi Shinde April 18, 2016 at 6:36 am #

        it throws an error ‘workon command doesn’t exist’

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

          Please see the answer to the first question in the “Troubleshooting” section of this blog post — it details how to resolve this problem.

  80. Noam April 4, 2016 at 1:50 am #

    Should I specifically install libgtk 2.0 or just the newest version?

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

      libgtk-3.0 should work, but I’ve tended to use libgtk-2.0. If you try libgtk-3.0 and it works for you, definitely let me know!

  81. AndrewR April 6, 2016 at 4:38 pm #

    Hi Adrian,

    Thank you very much for the tutorial.
    I’ve done the installation I can check it in the terminal, but Python 2.7 shell (IDLE) was saying: “no module name cv2”.
    I’ve done a “sudo apt-get install python-opencv” and now the IDLE is saying that I have OpenCV 2.4. In the terminal I still have the correct one 3.1.0.

    Could you please explain me how to remove the 2.4 from the IDLE and then how to add 3.1.0 to the IDLE.

    I hope you will answer my question.


    • Adrian Rosebrock April 7, 2016 at 12:42 pm #

      You will need to use apt-get to remove OpenCV 2.4; however, at this point I would suggest reformatting the card and starting over. The reason I say this is because the apt-get version of OpenCV is very old and likely also installed a bunch of extra dependencies you don’t need.

      Secondly, make sure you see the “Troubleshooting” section of this post as it details the primary reasons why you are unable to import the cv2 bindings.

      • AndrewR April 7, 2016 at 4:15 pm #

        Thank you Adrian! Your work is huge and is highly appreciated!
        Regards, AndrewR.

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

          No problem, happy I could help!

  82. Jim April 6, 2016 at 7:15 pm #

    Has the issue regarding pyserial/GPIO library install to the virtual environment been resolved?

    • Adrian Rosebrock April 7, 2016 at 12:40 pm #

      It’s not an “issue”, it’s just a difference in how you install the script. Just access the virtual environment and install GPIO into the environment:

      Here is an excellent tutorial on the basics of virtual environments that I recommend everyone give a read.

  83. Jim April 6, 2016 at 8:17 pm #

    Very nice work, but it would very helpful if you could add instructions to test a simple python/opencv script at the end of the installation instruction for the purpose of verifying installation and giving the new user a hand in learning how to setup a simple script in the new cv virtual environment.

    • Adrian Rosebrock April 7, 2016 at 12:38 pm #

      Thanks for the feedback Jim. I’ve done that on previous OpenCV install posts, I’m not sure why I didn’t for this one. Also, if you’re looking to put your OpenCV + Raspberry Pi install to use, be sure to take a look at Practical Python and OpenCV which contains a bunch of tutorials to get you started quickly.

  84. Reza April 7, 2016 at 12:49 pm #

    Adrian , i have problem , the problem is mkdir no space left on device , can you help me to resolve this ?? thanks

    • Adrian Rosebrock April 7, 2016 at 12:55 pm #

      Make sure you have fully expanded the disk on your Raspberry Pi by using sudo raspi-config and selecting the first option to resize the disk, then rebooting. If you have already done this and there is still no space left, you’ll either need to (1) delete files on the disk to make more room or (2) get a larger micro-SD card.

  85. israel April 14, 2016 at 2:49 am #

    my raspberry pi turned off during the make. when i use the make it doesn’t go past 71% it looks like its looping. How do i get past it? I tried make clean and then make but that didn’t work. Would i have to recreate the build folder?

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

      If your Pi shut off during the make command, then I would suggest deleting the build directory and then re-creating it. If you tried compiling using multiple cores, then the shut off likely broke the build.

      • israel April 15, 2016 at 6:18 pm #

        Thanks, yeah during the multiple core compilation it broke. I just used the regular single core make command. Now its all gravy:) I reinstalled the build directory and installed dependencies and compiled it. Thanks for this tutorial.

        • Adrian Rosebrock April 17, 2016 at 3:36 pm #

          Nice, congrats on resolving the issue 🙂

  86. Adolfo April 16, 2016 at 12:58 pm #

    Hi there!

    I’m facing a problem I am not able to solve by myself (I am completely new to RPi and all this building and compiling things :))

    When I try to cmake opencv, being on cv virtual env, I get this error message:

    CMake Error: The source directory “/home/pi/opencv-3.0.0/build/BUILD_EXAMPLES=ON” does not exist.

    I tried to use -U with al keys to remove them from cache, but it keeps throwing the same error.

    Any clue? I’m lost

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

      It sounds like you’re not copying and pasting the cmake command correctly. Make sure you copy and paste the full command.

      • Adolfo April 19, 2016 at 6:01 am #

        Yep, I was missing the two trailling periods.

  87. Dumitru Popa April 17, 2016 at 2:53 am #

    Thank you, nice work !

    • Adrian Rosebrock April 17, 2016 at 3:28 pm #

      Thanks Dumitru!

  88. Adolfo April 17, 2016 at 1:05 pm #

    Hi everybody,

    Just in case some is or will get stuck at the beginning of step 4: the two periods at the end of the cmake line are meaningful!! I was getting an error message because I thought (ignorant me) they just meant you could add more parameters.

    Silly me!

  89. Stewart Baskin April 20, 2016 at 2:23 pm #

    Hi Adrian. Thanks for the great tutorial! Have you made the tutorial for the Pi 3?


  90. Jenny Ching April 21, 2016 at 8:17 pm #

    Hi Adrian,

    I got to step 4 (examining the output of CMake) and noticed that both the Interpreter AND numpy are both NOT in the virtual environment (cv). Which steps do I have to go back to to correct these? Thanks!

    • Adrian Rosebrock April 22, 2016 at 11:46 am #

      To start, make sure you are in the cv virtual environment before you run CMake. If you are, then you’ll need to manually supply the paths to the interpreter and NumPy directories. For more information on how to do this, please see the comments section of this post.

      • Jenny Ching April 22, 2016 at 1:54 pm #

        I double checked that I was in the cv environment before I ran CMake however when i run CMake I still get this…

        Python 2:
        — Interpreter: /usr/bin/python2.7 (ver 2.7.9)
        — Libraries: /usr/lib/arm-linux-gnueabihf/ (ver 2.7.9)
        — numpy: /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.8.2)
        — packages path: lib/python2.7/dist-packages


        • Adrian Rosebrock April 22, 2016 at 3:51 pm #

          It actually looks like the issue is related to your “numpy” and “packages path” variables. To resolve the Numpy issue, have you installed NumPy into the cv virtual environment?

          You can actually just leave the “packages path” variable alone and then manually copy the file into your site-packages directory after OpenCV compiles and installs.

  91. Akshay Jethani April 24, 2016 at 3:30 am #

    after giving the command of sudo apt-get upgrade,the pi is taking to long to upgrade like 4 hours sometimes, even after 4 hours its still upgrading and showing connection failed in between, can u plz help me regarding this issue.

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

      It sounds like you may not have a very strong internet connection. I would suggest making sure you have a good internet connection before running the upgrade step.

  92. Charles Baker April 28, 2016 at 11:05 am #


    I have followed this tutorial exactly and have installed OpenCV 3.1.0 without error however I still cannot seem to access any of the modules associated with opencv_contrib (i.e. xfeatures2d to use SIFT and SURF). I have been pulling my hair out over this, I have tried reinstalling many times, I include the -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \

    line when using cmake, there are no errors, the path provided is where my opencv_contrib folder is actually located so it is not as if I am pointed to the wrong directory, however I simply get an AttributeError: ‘module’ object has no attribute ‘xfeatures2d’ when attempting to use xfeatures2d

    • Adrian Rosebrock April 28, 2016 at 2:22 pm #

      Take a look at the output of CMake; specifically, where it outputs the list of modules to be compiled. Do you see any of the contrib modules in there? If not, it really sounds like the path to the contrib modules could be incorrect.

      • jasson August 14, 2016 at 9:34 am #

        Hi, Adrian. I have met the same issue. When I used “-lopencv_contrib” in g++ cmd, it gave an error as ” can’t find -lopencv_contrib”. Do you know why? And where can I find the opencv_contrib lib that I have installed? Thank you very much for your help!

  93. Ragunaath April 28, 2016 at 10:37 pm #

    make command resumed after a power failure. Will it present a problem?

    • Adrian Rosebrock April 30, 2016 at 4:03 pm #

      It could, depending on where in the compile stage you were. I would suggest running make clean and then recompiling to give yourself a fresh start.

  94. Collin Sale April 30, 2016 at 3:43 pm #

    Hi Adrian,

    nice one, .. works fine and I already received your hardcopy 🙂
    Thanks a Lot

    one thing: since I installed Samba-Share, a 5″ display and some other things before I started opven CV, I did actually run out of space on my 8GB SD, just like Bill mentioned earlier.

    Bill Kearson January 3, 2016 at 10:09 am #
    Using a 8G card I was almost out of space

    Maybe you could add a Step #7: CleanUp the SD-Card 🙂
    And mention on Step #1 that 8GB is just about to fit if you don’t install anything else earlier.

    Thanks and best Regards

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

      Thanks for the suggestion Collin, I’ll definitely look into this!

  95. Jesus De Jesus April 30, 2016 at 8:25 pm #

    Hellow Adrian, I resolve the problem of the comand wget, but now when I run the comand:
    wget -O

    the comand line says:

    –2016-02-03 17:25:20–
    Resolving (…
    Connecting to (||:443… connected.
    ERROR: The certificate of ‘’ is not trusted.
    ERROR: The certificate of ‘’ is not yet activated.
    The certificate has not yet been activated

    So, If you can help me with that I would appreciate it.

    • Adrian Rosebrock May 1, 2016 at 3:08 pm #

      Hi Jesus — try adding the --no-check-certificate switch to wget, like this:

      $ wget -O --no-check-certificate

      This should take care of the error.

  96. Lukas Vosyka May 3, 2016 at 3:58 pm #

    Hi Adrian,

    by now you became my number 1 resource for checking OpenCV questions, inspirations and how-to’s! Thanks so much for your contribution and excellent step-by-step tutorials. I am eventually new to this topic (coming from Windows and .NET ^^) and you helped me a lot. This thread was also especially helpful since I got to do some projects on rapsi and OpenCV.

    I’ll definitely get your hardcopy bundle and looking forward implementing some cool stuff with Raspi and OpenCV 😀

    Thanks again for your very detailed and admirable contribution to this topic about OpenCV! Guess you’ll hear from me 😉


    • Adrian Rosebrock May 3, 2016 at 5:41 pm #

      Thanks for the comment Lukas — and it’s awesome to hear that I could be of help 🙂

  97. ghanendra May 4, 2016 at 12:22 am #

    Helloo..!! Adrian

    How to add these commands to autostart when I power on my Raspberry Pi ?

    source ~/.profile
    workon cv
    vncviewer :1
    python picamera 1

    I want to open my python script using the virtual environment workon cv every time when i power up the Pi.

    • Adrian Rosebrock May 4, 2016 at 12:32 pm #

      Stay tuned for the blog post in 2 weeks where I demonstrate how to run commands + Python scripts on boot 🙂

      • ghanendra May 4, 2016 at 3:10 pm #

        OK!! Adrian. I am waiting for your fantastic posts. More and more is coming to learn.

  98. Maugy May 6, 2016 at 1:12 am #

    Wow, as someone who is trying to learn Linux, this is by far the clearest and best written tutorial I have ever seen!

    Thank you!

    • Adrian Rosebrock May 6, 2016 at 4:32 pm #

      Thanks Maugy, I appreciate that 🙂

  99. Vikram Bajaj May 9, 2016 at 9:41 am #

    Thanks for this awesome tutorial Adrian!
    Worked like a charm!

    • Adrian Rosebrock May 9, 2016 at 6:47 pm #

      Fantastic, I’m glad to hear it Vikram! 🙂

  100. Marc Boudreau May 12, 2016 at 7:42 pm #

    Hi Adrian,
    When you say “For some reason, unbeknownst to me, when compiling the Python 3 bindings the output .so file is named rather than .”, —I think that’s fixed.

    I got “”

    When I do the check, on the (cv)raspberrypi$ directory, everything works for the python check.
    However when I do the version check of CV2, doesn’t work.
    If I change directory to (cv) raspberrypi/.virtualenvs/cv/lib/python3.4/site-packages/

    and check the version, I get same message as you.

    When I start adding computer vision functionality, I’m afraid it wont find libraries.

    I must of done Something wrong.
    Is there easy fix or it’s best to reformat and redo procedure?

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

      Are you absolutely sure that you compiled OpenCV with Python 3 bindings? Double-check your output of CMake to make sure this is correct. You’ll typically see the file when compiling Python 2.7 bindings.

  101. Ashutosh May 16, 2016 at 11:10 am #

    Hi Adrian,
    Thanks for awesome tutorial.I have installed opencv and python as told by you.
    But when I use the picamera, I get considerable lag between input and output shown on result window.Please suggest something…..

    • Adrian Rosebrock May 17, 2016 at 11:37 am #

      How are you accessing your Pi camera? Natively via HDMI + monitor? Or over VNC/SSH? Also, which model Pi are you using? All of these can affect the lag you are experiencing.

  102. Steve Flynn June 5, 2016 at 9:22 pm #


    When i run a saved python script as in “sudo python” i get the cv2 module does not exist error. Howover, when i run the import directly from the command line it works perfectly. In both cases i am inside the virtual environment and see the (cv) on the left. Does this have something to do with the virtual environment or did something not work.

    Steve Fynn

    • Adrian Rosebrock June 7, 2016 at 3:28 pm #

      This does have to do with the virtual environment. Please read this blog post for more information, including the solution.

  103. Jit Craig June 7, 2016 at 3:13 am #

    awesome guide to installing opencv. every step worked correctly. now am getting ready to get my hands dirty with some real CV projects on my RPi 2

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

      Fantastic, I’m happy to hear the tutorial worked for you Jit! 🙂

  104. zoe June 11, 2016 at 11:03 pm #

    Thank you so much!!! That is really helpful!!!

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

      No problem, I’m glad I could help Zoe!

  105. Randy June 15, 2016 at 10:51 am #

    Following this tutorial everything seem to work fine and completed without error but at the end the is not present on my system.

    Any ideas?

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

      Hey Randy — if you’re not seeing the file, then either there was an error during the CMake/make step or the Python bindings were not build. Check the output of CMake to ensure that the python2/python3 bindings are in the list of modules to be build.

  106. Nick Shaver June 17, 2016 at 9:49 am #

    Adrian – thanks very much for all of your python+opencv tutorials. You’ve saved me dozens of hours of wheel reinventing. I’ve used this particular tutorial several times. I’ve recently learned about checkinstall, and have been using it to keep from having to compile the same thing over and over. Dropping it in place of the “make install” creates a deb file that can be used to install opencv3 on a similar machine, though I’m not sure if that works with your virtual wrapper. Do you see any reason not to use checkinstall, assuming versions haven’t changed since the deb was made?

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

      Wow, that’s really cool! I had not heard about checkinstall before, do you have any links to the documentation so I can readup on it?

      • Nick Shaver June 19, 2016 at 12:39 pm #

        There’s not really a whole lot to checkinstall. I just apt-get it on debian boxes, and at the point of any program that I’ve compiled I skip the “make install”, and instead as a normal user (not root), I simply type “checkinstall –install=no”. It will prompt for a few things, I accept its defaults with the exception of the package name and version. With opencv3, for instance, I’ll set package-name=opencv, and version=3.1.0, and then it lit it rip. You can type in dependencies but I haven’t tried that yet. It’ll churn for 5 minutes or so on a raspberry pi. After it’s done, I use dpkg to install the deb file that it created. That way if things work, I know for certain that they’re only working because the deb file made them work. That’s the only thing I do as root – “dpkg -i opencv_3.1.0.deb”. I’ve used it on some other programs as well (mjpeg-streamer for instance) and it worked great. It supposedly will also created rpm files, but I use debian everywhere so I haven’t tried that.

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

          This is really cool, thanks for sharing Nick. I’ll absolutely be looking into this!

  107. Sid June 19, 2016 at 6:18 pm #

    awesome tutorial on getting opencv installed on Rpi. Easy to follow and nicely explained! I’ve already signed up for your class..

    btw,,two things i had to do in addition were
    1) add WITH_GTK=ON in Cmake to get rid of some run time errors to enable QT to get the window working.
    2) ‘sudo modprobe bcm2835-v4l2’ to load the v4l2 driver so that cv2.VideoCaptire(0) works.

  108. Tony Pitman June 23, 2016 at 4:44 pm #

    When I do the cmake command I get several lines like the following. They may not be errors, but just configuration responses. Can you verify they are normal?

    Here is the github gist

    • Adrian Rosebrock June 25, 2016 at 1:36 pm #

      Everything looks okay to me. It’s okay for various libraries not to be found, that’s perfectly normal behavior — it all depends on what you are installing, etc. What really matters is your “Python 2/Python 3” configuration sections.

  109. moayed June 23, 2016 at 7:08 pm #

    Adrian !
    finally i got openCV on my raspberry pi B+ 😀
    thank you alot
    your book or private lessons teach how to detect any objects by raspberry pi camera module or USB cam

    • Adrian Rosebrock June 25, 2016 at 1:35 pm #

      Congrats Moayed, that’s awesome!

  110. Brendan Allen June 26, 2016 at 7:22 pm #


    When I am trying to edit the profile file I am getting a message telling me that Permission is denied, and I cannot seem to gain access in order to change the file. Do you have any suggestions to how I can access this file to make the appropriate changes?



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

      That’s strange that you’re getting a permission error since the .profile file should belong to the “pi” user. But in either cause, you can force edit the file using sudo:

      $ sudo nano /home/pi/.profile

  111. Mohsen July 8, 2016 at 9:12 pm #

    i did all those steps and i sucessed to install opencv.
    Thank you very much.
    i think an instruction for install opencv 3.0.0 + opencv-contrib for python and on windows
    is usefull too.
    good luck

  112. merad July 8, 2016 at 10:41 pm #

    first thanks for your instructions.
    when i type import cv2 or import numpy in python 2(idle)
    it gives me an error:
    import error: no module named cv2
    what do i od?

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

      Please see my reply to “Vishwa” above. You should also read the “Troubleshooting” section of this blog post.

  113. Sunny Lee July 13, 2016 at 12:15 am #

    Really thanks for your post! It helped a lot.

    However, I’ve been thinking, why the virtualenv is required. Since if I used virtualenv to install opencv, I can’t import any other library after that such as matplotlib and drawnow library which belonged to global environment. That will be better if cv2 library is available in global environment so that any user can access to the library. I understand that virtualenv is necessary for certain Python projects but for most cases, especially for learner like me, I think that it is not required. What do you think if I go installing openCV without using virtualenvs?

    • Adrian Rosebrock July 14, 2016 at 1:12 pm #

      You certainly can install OpenCV without a Python virtual environment. It’s not a required. But I highly recommend it. You can learn more about why I recommend using Python virtual environments in the first half of this blog post.

  114. Tuomo July 20, 2016 at 2:01 am #

    Thanks for making the effort and writing such excellent instructions! This helped me to install OpenCV 3.1 on Raspberry Pi 3.

    • Adrian Rosebrock July 20, 2016 at 2:35 pm #

      Glad to hear it Tuomo! 🙂

  115. Mike Crawford July 21, 2016 at 12:49 pm #

    Thanks Adrian for this install doc. For some reason, I tried to install for python3 but instead I got the python2.7 install. I definitely used mkvirtualenv cv -p python3 ( I checked the command history). Note, the output of cmake shows the python (for build): python2.7.
    Is there something else that needs to be configured to get the python3 install?


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

      Hey Mike — the “python (for build)” is really buggy. It will pretty much always report that bindings for Python 2.7 (instead of Python 3) are being compiled. I would ignore that and instead focus on the “Python 3” section above it and ensure the NumPy, library, site-packages, etc. settings are correct.

  116. Chen Sa July 25, 2016 at 9:23 am #

    Hey Adrian,
    Im trying to install openCV via a usb storage, since my Pi does not have enough space to compile opencv there. Thing is, the CMAKE result points to the /usr path python while trying to build on the usb stick. However, when I try to build on the Pi instead, it does point to the virtualenv. Of course I made sure to always be in the cv enviroment.
    Any idea why this might be the case? I am kinda new to the Pi and linux in general, so it is somewhat hard to find the cause.

    I’d appreciate some advice, thanks!

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

      Hmm, that’s quite strange. Can you run CMake on your SD card to confirm the issue is related to USB stick? Whether you are using your SD card or the USB stick to compile OpenCV shouldn’t affect the paths that CMake detects.

  117. Iqbal Aribaskara August 25, 2016 at 2:53 am #

    Hello Adrian, Thank so much for the complete and eloquently written tutorial. I had a lot of fun following them, currently reading your free 21 days crash course.
    I have a question, I’m building something that uses opencv for input, and doing some physical stuff via GPIO for the output.

    The thing is to use GPIO, I need to run my script under sudo, but since opencv is installed under a virtual environment, I get unrecognized cv2 error every time I run my script.

    Is there any way I can link to the installed opencv from outside the virtual environment? Or should I just install opencv outside the virtual env?

    Thanks in advance.

  118. Loic September 2, 2016 at 9:17 am #

    The installation went all good with me. But as I rebooted my raspberry pi 2 the command “workon cv” could not be found. Eventhoug i use de “source ~/.profile” command.

    • Adrian Rosebrock September 5, 2016 at 8:12 am #

      If your workon command could not be found after reboot, then there was likely an issue updating your ~/.profile file. Go back to the step where you edited the ~/.profile file and reapply the edits.

  119. Lukas September 5, 2016 at 3:42 pm #

    Hi Adrian.
    Thanks for your guide here. I followed every step and began to compile it with the command: “make -j4”. It all went well until 93%. Now it is frozen and doesn’t continue anymore. Can I just take away the power of the Pi 2 and start again or will I break something and have to begin again?

    • Adrian Rosebrock September 6, 2016 at 3:40 pm #

      I would suggest using ctrl+c to force stop the compile. Then delete your build directory, recreate it, re-run CMake, and then build using only a single thread:

      $ make

      Hopefully that should resolve the issue.

  120. ben September 6, 2016 at 7:04 pm #

    hello Mr Adrian i follow all steps without any error msg but when i run python and type import cv2 i got this msg :
    importerror no module named cv2

    • Adrian Rosebrock September 8, 2016 at 1:26 pm #

      If you are having trouble importing cv2, please see the “Troubleshooting” section of this blog post for more details on how to resolve the issue.

  121. PMARINA September 17, 2016 at 7:43 pm #

    Hey, I am using openCV with the raspberry pi’s GPIO pins. I managed to install RPi.GPIO with pip so that I can use sudo python3 in order to access the gpio pins from the cv virtual environment. Unfortunately, I was unable to do this for the openCV library itself. Could you help me link or reinstall openCV so that I can do sudo python3 and still have access to openCV?

    This is the same issue as Iqbal, except I need to be able to use openCV when I do sudo python3 to run programs/console.

    • Adrian Rosebrock September 19, 2016 at 1:09 pm #

      So if I understand your question correctly, you have OpenCV + RPi.GPIO installed as the “pi” user, but you need to run the Python script as “root”?

      If so, I would launch a root shell, access the virtual environment, and then run the script:

  122. Rafał September 18, 2016 at 5:48 pm #

    Hey Adrian !
    You’ve done a great work, I’m a new to a virtual environments, could you tell me how to add the module SMBus to the virtual environment? it’s installing in raspbian typing apt-get install python-smbus, but when I’m in virtualenv Cv the smbus is not working unfortunately.

    Greetings and Keep up the good work,

  123. AMAN KHATRI October 3, 2016 at 6:37 am #

    As always,Thank You Adrian for this tutorial.

  124. enrique October 10, 2016 at 8:40 pm #

    hi Adrian

    I’ve been using your notes to install opencv on raspbian jessie lite ,,,, I never get a failure installing all neccesary software, but when I trying to import opencv within python environment it does not work ……….. Do you know why?

    • Adrian Rosebrock October 11, 2016 at 12:53 pm #

      It’s hard to say exactly why without having physical access to your machine. I suggest you look at the “Troubleshooting” section of this blog post for more information. The Raspbian vs. Raspbian Lite shouldn’t make a difference.

  125. Gaby Borjas October 16, 2016 at 4:54 am #

    Great tutorial for the installation of OpenCV.
    I am extremely new to raspberry pi, openCV and linux so I am a little lost. I wanted to continue with a couple of tutorials for facial recognition, but I get the following error ImportError: No module named cv2/ How can I resolve this?

    • Adrian Rosebrock October 17, 2016 at 4:10 pm #

      If you are having an issue related to importing the cv2 module please see the “Troubleshooting” section of this blog post. It details common reasons why the OpenCV bindings may not be importing.

  126. Dan October 24, 2016 at 6:37 pm #

    Ok, sorry, I figured out what I did wrong. I didn’t CD into build, I was in make. I accidentally got it to work in make which made me go back and see what I did wrong and that’s when I realized I was in the wrong folder. I’m new to the command line and linux. So it did work in the make folder, is that going to mess anything up? Should I try and delete that if it’s possible?

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

      Without knowing the exact steps you’ve made I’d suggest just deleting the directories, unzipping the OpenCV code, and trying again. Theoretically this shouldn’t mess anything up, but again, without knowing what commands you executed I cannot guarantee this.

  127. Koray November 1, 2016 at 1:01 pm #

    Hey Adrian, Nice Tutorial
    Im having some issues when im trying to do the command
    “mkvirtualenv cv”
    ” mkvirtualenv cv -p python3″
    It always says
    “Command not found”
    I already looked at the Troubleshooting and it counldnt fix my Problem.
    Im new to OpenVC and Linux. If you could help that would be awesome thanks.
    Im German so sorry for my bad english ;).

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

      If you’ve already looked at the Troubleshooting section of the tutorial then it’s pretty hard to diagnose the error without having physical access to your machine. My guess is that your .profile isn’t updated properly, you didn’t source it, or perhaps more likely, there was an error installing virtualenv or virtualenvwrapper and you simply missed the error message.

      I would suggest trying to re-install both virtualenv and virtualenvwrapper and ensure both install without an error message.

  128. Juan M November 7, 2016 at 6:31 am #

    Hi Adrian,

    Thanks for your tutorial, I followed your steps and OpenCv was succesfully installed. However, I found some troubles which I could solve by some previous comments. Here follows what worked for me:

    1. SD of 8Gb is not enough, solution -> use an external hard drive with EXT4.

    * for windows users I did not find a software to write, only to read Ext2fsd. So, running a liveCD could help.

    As you mentioned, make the files in this EXT4 USB and install from it worked perfect!!

    2. The path for final steps did not worked, it was different, solution -> install mlocate

    $ sudo apt-get install mlocate
    $ sudo updatedb
    $ locate

    once it returned me the path, I just changed the one you mentioned.

    3. The path for the virtual environment did not was available even running the workon cv (it appeared the (cv) user@machine…solution -> run it anyway at the end you could make the symbolic link which was recommend in step 5.

    Thanks Adrian, you are doing a great job by making easier image processing in raspberry!

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

      Congrats on getting OpenCV installed, nice job! To address your points:

      1. I recently installed OpenCV on an 8GB card without an issue. I’m honestly not sure why this would happen on your card.

      2. Where did the file end up being?

      • Adrian G. November 14, 2016 at 12:17 pm #

        cv2 location was removed in my comment above. It’s installed here;

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

          In that case, just move to the site-packages directory and rename:

          $ mv /usr/local/lib/python3.4/dist-packages/ /usr/local/lib/python3.4/site-packages/

  129. Johnny November 12, 2016 at 12:07 pm #

    Success! Also, amazing support here Adrian.

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

      Nice job Johnny!

  130. Erik November 14, 2016 at 2:57 pm #

    Hi Adrian! Thanks for this awesome tutorial. I Just got a brand new RPi 3 and already started to learn and practice CV!

    Unfortunately I encountered a problem trying to use SIFT. I Installed Opencv with the contrib package, checked that they’re the same version (3.1.0) etc.

    What I get is the infamous “‘module’ object has no attribute ‘xfeatures2d'”, how can i fix this?

    For starters, how can I make sure that the contrib packages are installed successfully? And if not, what can I do to fix this? do I have to re-compile the whole thing?


    • Adrian Rosebrock November 16, 2016 at 2:00 pm #

      Awesome, it’s great to hear that you’re interested in learning OpenCV!

      If you are getting an error related to xfeatures2d then you compiled and installed OpenCV without opencv_contrib.

      Yes, you will have to recompile the whole thing. You can avoid the issue by making sure you supply the correct path to the -D OPENCV_EXTRA_MODULES_PATH as I do in the CMake command in the tutorial.

  131. Hetansh November 19, 2016 at 2:18 pm #

    hey i cannot find the ./profile file in my /home/pi/ what can be the reason?
    any help is appreciated thankyou.

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

      If it’s not there, simply create it.

  132. Malik Mehboob November 21, 2016 at 9:56 pm #

    i have install successfully but when i open python 2 IDLE shell it gives error ; cv2 no module found :/ help me

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

      Please see the “Troubleshooting” section of this blog post where I discuss the vast majority of errors you will see when trying to import the cv2 module.

  133. David Ferrara November 22, 2016 at 12:35 pm #

    Hi Adrian,
    Thanks for the great tutorial. I’m facing the error when compiling in Python: ImportError: No module named cv2. I think my problem lies in the cmake part. When I examine the output of cmake, the Libraries says NO. Do you have any idea why this is, and how to fix it? I’ve tried doing it both with ON and OFF as you said in your update.

    Thanks so much!

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

      Hey David — it’s hard to say without seeing your exact CMake output, but I discuss many of the errors related to importing OpenCV and compiling it in the “Troubleshooting” section of this blog post. If you are not getting the “Libraries” section automatically determined then I’m guessing that you’re not in the “cv” virtual environment before running CMake.

      For what it’s worth, I also offer a downloadable Raspbian .img file with OpenCV pre-configured and pre-installed inside Practical Python and OpenCV. This would be a great way to get started learning OpenCV quickly. Just download the Raspbian .img file, flash it to your card, and boot.

  134. Gabi November 22, 2016 at 5:48 pm #

    Can I remove the opencv-3.1.0 after the build?

    • Adrian Rosebrock November 23, 2016 at 8:34 am #

      Yes, once OpenCV has been successfully installed you can remove opencv-3.1.0 and the contrib directory as well.

  135. vaishali November 22, 2016 at 11:46 pm #


    I need to cross compile qt + opencv on raspberrypi 2B. I am able to cross compile simple qt code. but not with opencv. Can you please help..

  136. judson antu November 23, 2016 at 11:48 pm #

    hey adrian, i followed your instructions and everything is ok. but i now dont want to use the virtual env. but now i cant import cv2 into my python idle. what should i do?

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

      If you don’t want to use your virtual environment you should copy the file into the /usr/local/lib/python-X/site-packages where you need to replace “X” with your Python version (2.7, 3.4, 3.5, etc.)

      • judson antu December 1, 2016 at 2:03 am #

        thanks adrian. that worked well. i have another doubt, is the ffmpeg file available with the installation. i am not able to capture video from a file. on my desktop running windows, all i had to do was to copy the dll to the python directory. but i cant file anything similar here. any help would be appreciated

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

          It’s hard to say without having access to your physical machine, but normally if you want to use FFMPEG you need to compile OpenCV with FFMPEG support enabled.

  137. Lina December 5, 2016 at 8:13 am #

    Hello Adrian,

    thank you for your great tutorial ^^

    I am sorry to bother you, but could you please help me?
    I already try this tutorial, and it went okay until step 11. then I found a problem, when I try using python and import cv2,
    it said : ” ImportError: No module named cv2″

    I read all comment and replies and found that I didn’t update my ~/.profile file. Then I try this tutorial from step 0 and I am stuck in step 7. I update my ~/.profile file using vi, also using nano and follow your video tutorial, but it’s said “no such file in directory”.
    So I search it manually in my memory and found it in pi/home, so the file does exist. I don’t know why it’s said “no such file in directory”. I also try to remove the old ~/.profile file and make a new one and add the lines like step 7. but when I type source ~/.profile, it won’t work again.

    could you please help me? I don’t know how to fix it. I am a beginner in linux and rapberry pi. And I already do this tutorial (from step 0) for a couple time and I found that some files are written double in my memory, is that okay? Or I need to erase the old installation before start a new one? But I don’t know how to erase it. please help me..

    thank you before, thank you


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

      If you do not have a ~/.profile file, that’s okay — create it and save it.

  138. Andrew December 7, 2016 at 10:08 pm #

    I would like to add my thanks to all of the others. This is a beautifully written guide and there is no way I would have done this without your help. Thank you so much for this.

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

      Thank you Andrew, I’m happy the tutorial helped you! 🙂

  139. Chris Ryan December 9, 2016 at 1:32 pm #

    Hey great work.

    I’m having one issue. Every time I open a terminal I try $ source ~/.profile and then $ workon cv but I’m getting “-bash: workon: command not found”.

    Am I doing something wrong?

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

      Hey Chris — it sounds like your ~/.profile file was not updated correctly. Please check the “Troubleshooting” section of this post for more details.

      • Chris Ryan December 18, 2016 at 6:37 pm #

        Thanks. Think I messed that step up. Sorted now

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

          Awesome, glad to hear it 🙂

  140. Sahin December 11, 2016 at 3:07 am #

    Thank You very much. I’ve tried many tutorials before. and finally this worked.

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

      Congrats on getting OpenCV installed Sahin, nice job!

  141. Rijul Paul December 12, 2016 at 9:53 am #

    Hey Adrian,I am working with a Debian Jessie and trying to install opencv on that..Do you have any tutorial for that??

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

      If you are using Debian then follow this this Ubuntu tutorial. Ubuntu is Debian-based and all instructions will work on your system.

  142. Jeff Ward December 19, 2016 at 1:31 pm #

    While building OpenCV 3.1.0, I encountered “…fatal error: hdf5.h: No such file or directory”.
    To solve this problem, I manually applied the fix described here:
    by adding


    to the end of /home/pi/opencv-3.1.0/modules/python/common.cmake.

  143. İbrahim December 23, 2016 at 7:38 pm #

    Hi Adrian, thanks for this tutorial. However i get an issue when i try to compile. the “make” command gets me an error that is “No targets specified and no makefile found. Stop.” i searched but did not find any solution. What could be the problem?

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

      If you are getting this error it’s either because (1) you are not in the “build” directory when you executed “make” or (2) CMake exited with an error and you need to resolve the error first.

  144. Usman Aftab December 24, 2016 at 2:45 am #

    Hey Adrian
    Thanks man for a detailed procedure for Opencv 3 install on jessie

    I am working on a image processing project for that purpose i collected above 300 images for database but i am having trouble in making a database which will mach image taken through camera to the images in database someone suggested flann index Can you guide me in this regard

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

      I discuss how to create image datasets inside both Practical Python and OpenCV and the PyImageSearch Gurus course.

      I’m not sure what your database looks like, but if you’re trying to perform keypoint matching, I would suggest reading through the final chapter of Practical Python and OpenCV. Otherwise, the PyImageSearch Gurus course demonstrates how to build an image search engine that can scale to millions of images.

  145. Aman garg January 2, 2017 at 2:05 pm #

    hey adrian,

    Thanks for the tutorial. It has been a great help.

    Since, opencv has been installed using virtualenv, will it only work in the (cv) environment ? Is there any way that I don’t need to log into it? For example, if I need to run some scripts for opencv? will they always require (cv) ?

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

      You can still use OpenCV outside of the “cv” virtual environment provided that you have the Python bindings in your system site-packages directory.

  146. Tiberio Martinez January 5, 2017 at 12:49 am #

    Hi Adrian,

    Big thanks for the fantastic tutorial.
    It worked like a charm!

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

      Great job getting OpenCV installed Tiberio!

  147. Chase January 10, 2017 at 3:58 am #

    I also have the “no module named cv2″problem. could you please give me a short test code working on Python3(IDLE) which can import cv2 perfectly. THX in advance.

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

      If you are having trouble importing the “cv2” library into your shell please see the “Troubleshooting” section of this guide. Also, make sure you are using the terminal/console version of IDLE rather than the GUI version of IDLE since the GUI version does not respect virtual environments.

  148. joshua January 10, 2017 at 10:41 pm #

    failed to create symbolic link ‘’: File exists

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

      If the file already exists you can (1) make sure that the sym-link is valid and import-able into Python or (2) delete the sym-link and re-create it.

  149. Joe January 15, 2017 at 7:15 pm #

    Do you need to do “make” on the virual environment cv? thanks

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

      As long as you executed CMake in the virtual environment (and CMake exited without an error) you can run “make” outside the virtual environment if you wish.

  150. Snigdh January 17, 2017 at 7:25 am #

    hii i was trying to install it but i am getting no module named pip found error .i tried installing pip but it doesnt work. help me through.

    • Adrian Rosebrock January 17, 2017 at 8:40 am #

      You should be able to install pip via:

      Is there a particular error message you get when you try to run these commands?

  151. Adam January 21, 2017 at 9:47 am #

    Hey Adrian. Great tutorial! You are very thorough and detailed. Everything worked smoothly for me.

    • Adam January 21, 2017 at 10:52 am #

      Also, what size SD card are you using on your RPi?


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

        I’ve been using a 16GB card for the latest version of Raspbian. However, I’ve also used an 8GB card before without an issue. If you run into space issues make sure you delete the Wolfram Engine along with LibreOffice — that will free up ~1GB of space.

  152. Dave Xanatos January 24, 2017 at 2:12 pm #

    Fantastic tutorial, thanks very much. I’m installing OpenCV 3.2.0 and I’m watching the results of the make -j4 command, and it’s about 46% complete, so far with only one error noted in two functions: “warning: array subscript is above array bounds [-Warray-bounds]” The module is contours.cpp under cv_surface_matching. I’m running Rasbian Jessie, did all the updates, on a Pi 3B+, and while I saw the errors, it seemed to not phase the installation any, it is proceeding smoothly as I type. Is this anything I should worry about, and if so, is there an easy way to fix the issue, or would I need to start from the beginning and reinstall opencv?

    This is my first project on the RPi, having graduated up from microcontrollers like the PICs, Propeller and Basic Stamps. My goal is facial recognition and object tracking, as well as text recognition and reading, in a robotic project. Your tutorial is fantastic and has guided me perfectly (although I had to teach myself the vi editor again to edit ~./profile… last used vi in 1986 I believe!) Thanks very, very much for all the time and effort you have put into this. I will be visiting your kickstarter and getting that book!

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

      Hey Dave — how long has the command been stuck at the 46% point? If it’s been awhile (> 20 minutes) I would stop the compile, delete your “build” directory, re-create the “build” directory, re-run CMake, and then run “make” (without the -j4). This will avoid any race conditions that might be happening with your compile now.

      • Dave Xanatos January 24, 2017 at 4:43 pm #

        Hi Adrian, thanks for the really quick response! Actually, it zoomed along until 86% and hung building something from the non-photorealistic section of something. After about 45 minutes I figured it wasn’t coming back. Just deleted the build directory… starting back at CMake. Thanks very much, it’s great not feeling like I’m totally on my own, but it’s a blast learning all this new stuff.

  153. Dave Xanatos January 24, 2017 at 9:47 pm #

    Installation of OpenCV 3.2 completed successfully, verified as described (import cv2, cv2.__version__ returns ‘3.2.0’ – THANK YOU!!!!! Just in time, UPS Tracking reports my first Pi Camera should be delivered here tomorrow. I am immensely happy with your tutorials here. My ultimate goal is stereoscopic vision on two Pis, with the ability to greet people by face recognition, control two “eyes” via servos, speak (via a small hardware text to speech system I already have a dozen of [V-Stamp]), respond to voice commands and queries, and read from text placed in the visual range. Just a “small” initial project 🙂 You’ve been an enormous help in getting this project out of the single-digits of probability and into the double digits! 🙂 Thanks again.

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

      Congrats on getting OpenCV installed Dave, awesome job!

  154. Mesay February 1, 2017 at 3:50 pm #

    It’s a great tutorial..thanks!

  155. Randy Cahya Wihandika February 4, 2017 at 3:50 am #

    Great! This tutorial also works for OpenCV 3.2.0! Thank you!

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

      Yep, this blog post also works with OpenCV 3.2.0 as well! Congrats on getting OpenCV installed 🙂

  156. Emrick February 4, 2017 at 2:22 pm #

    Hi Adrian, I have a problem with the installation. After completing all steps, when i try to import cv2 in my Virtual environment, i get an error : no module named ‘cv2’.
    I checked the /.virtualenvs/cv/lib/python3.4/site-packages folder and i found the file, but when i open it, i see that it is empty .. What do i have to do ?

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

      You mentioned that the file is “empty” — can you verify that your sym-link is pointing to a valid file?

  157. Colin Rutter February 5, 2017 at 5:10 am #

    Adrian, hi.
    Thanks for the great tutorial. I have one issue however. At step 4 when i check the cmake output.
    the Python 2.7 section has entries for the iterpreter only. There are no entries for libraries, numpy or packages !

    The python 3 section is correct as per your post. I installed numpy within the cv environment.

    what steps can i take to correct this cmake error or can i edit the cmake output file to add the correct paths before compile ? if so which file and where is it located ?

    many thanks

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

      You can only compile OpenCV for Python for one Python version at a time. It sounds like you created a Python 3 virtual environment and are now trying to compile OpenCV for it?

  158. João Balão February 8, 2017 at 9:38 pm #

    Great tutorial Adrien! The steps were very clear and well written.
    I only had some trouble when compiling the whole thing, somehow I never had enough space on my 8GB sd card to finish it successfully (I didn’t know it would require about 3GB of free space). I had to uninstall some applications, but in the end it was worth it.
    I can finally start my projects, thanks a lot!

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

      Congrats on getting OpenCV installed João, nice job!

  159. Kerem Kambur February 10, 2017 at 8:59 pm #

    Hi Adrian !

    Quite useful and simplified guide thanks for this first of all ! I’ve tried lots of ways including your way but I couldn’t successfully installed face recognizer extra modules.. While I try to import cv2.createLBPHFaceRecognizer(), IDLE returns No Module Found. Is there anything additional I’ve to do after your guide ?

    Thanks again.


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

      I assume you’re using OpenCV 3 as detailed in the tutorial? If so, all face recognition code was re-organized into the cv2.faces.* sub-module.

  160. Babak February 18, 2017 at 6:08 am #

    Great tutorial Adrien! . I followed every step and began to compile it with the command: “make -j4” and “make” It all went well until 87%. Now it is frozen and doesn’t continue anymore. it seems that can not build opencv extra modules. then I use this following cmake that used for runing matlab mfile on raspberry pi and successfully to end, is it usable? please guide me about this problem.

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

      It seems probable that there is some race condition in the compile using multiple cores. Try restarting the compile using only a single core:

      This should resolve the issue.

  161. Suganya February 19, 2017 at 12:52 am #

    Hi Adrian
    I am using Raspbian that was installed using NOOBs. I am struggling with OpenCV installation at cmake. Sometime it stops at 19% other time at 25%. Please tell me which of your tutorial I have to follow. I have wasted 5 days in this.

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

      It sounds like your Raspberry Pi may be running into race conditions when using multiple cores. Try compiling with only a single core:

      That should resolve the issue.


  1. Installing OpenCV 3.0 for both Python 2.7 and Python 3+ on your Raspberry Pi 2 - PyImageSearch - October 26, 2015

    […] How to install OpenCV 3.0 on Raspbian Jessie. […]

  2. Installing OpenCV on your Raspberry Pi Zero - PyImageSearch - December 15, 2015

    […] Since I’ve covered how to install OpenCV on the Raspberry Pi in multiple, previous blog posts, I’ll keep this post on the shorter side and detail only the relevant commands necessary to get OpenCV up and running. For a more thorough discussion on how to install OpenCV 3 on your Pi (along with a 22-minute video installation guide), please refer to this post. […]

  3. Install guide: Raspberry Pi 3 + Raspbian Jessie + OpenCV 3 - PyImageSearch - April 18, 2016

    […] How to install OpenCV 3.0 on Raspbian Jessie. […]

Leave a Reply