Installing OpenCV on your Raspberry Pi Zero


In this blog post I’ll demonstrate how to install OpenCV 3 on the Raspberry Pi Zero.

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.

I’ll also be making the following assumptions in this installation guide:

  • You are using Raspberry Pi Zero hardware (so the timings supplied with each command will match up).
  • You have Raspbian Jessie installed on your Pi Zero.
  • You want to install OpenCV v3.0 with Python 2.7 bindings (for Python 3 support, see this post).

Again, I have already covered installing OpenCV on multiple Raspberry Pi platforms and Raspbian flavors — the primary goal of this tutorial is to get OpenCV up and running on your Pi Zero so you can get started learning about computer vision, image processing, and the OpenCV library.

Installing OpenCV on your Raspberry Pi Zero

If you haven’t seen the Raspberry Pi Zero yet, it’s a really cool piece of hardware. It packs a single core 1GHz ARM processor. 512mb of RAM. And it’s smaller than a credit card.

But the best part?

It’s only $5!

While the Pi Zero isn’t quite fast enough for advanced video processing, it’s still a great tool that you can use to learn the basics of computer vision and OpenCV.

Step #1: Expand filesystem

If you’re using a brand new install of Raspbian Jessie, then the first thing you should do is ensure your filesystem has been expanded to include all available space on your micro-SD card:

Select the first option “1. Expand Filesystem”, arrow down to “Finish”, and reboot your Pi:

Figure 1: Expanding the filesystem on your Raspberry Pi Zero.

Figure 1: Expanding the filesystem on your Raspberry Pi Zero.

After rebooting, your filesystem will have be expanded to include all available space on your micro-SD card.

Step #2: Install dependencies

I’ve discussed each of these dependencies are in previous posts, so I’ll just provide a brief description, the command(s) themselves, along with the amount of time it takes to execute each command so you can plan your OpenCV install accordingly (the compilation of OpenCV alone takes 9+ hours).

First, we need to update and upgrade our existing packages:

Timing: 2m 29s

Install our developer tools:

Timing: 49s

Let’s grab the image I/O packages and install them:

Timing: 36s

Along with some video I/O packages (although it’s unlikely that you’ll be doing a lot of video processing with the Raspberry Pi Zero):

Timing: 36s

We’ll need to install the GTK development library for OpenCV’s GUI interface:

Timing: 2m 57s

Let’s also pull down a couple routine optimization packages leveraged by OpenCV:

Timing: 52s

Lastly, let’s install the Python 2.7 headers so wen can compile our OpenCV + Python bindings:

Timings: 55s

Note: I’ll only be covering how to install OpenCV 3 with Python 2.7 bindings in this post. If you would like to install OpenCV 3 with Python 3 bindings, please refer to this post.

Step #3: Grab the OpenCV source

At this point, all of our dependences have been installed, so let’s grab the 3.0.0  release of OpenCV from GitHub and pull it down:

Timing: 1m 58s

Let’s also grab the opencv_contrib repository as well:

Timing: 1m 5s

It’s especially important to grab the opencv_contrib  repo if you want access to SIFT and SURF, both of which have been removed from the default install of OpenCV.

Now that  and  have been expanded, let’s delete them to save space:

Step #4: Setup Python

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

Timing: 49s

Let’s also install virtualenv  and virtualenvwarpper , allowing us to create separate, isolated Python environments for each of our future projects:

Timing: 30s

Note: I’ve discussed both virtualenv  and virtualenvwrapper  many times on the PyImageSearch blog. If this is your first time using them, I suggest referring to this blog post on installing OpenCV 3 on Raspbian Jessie.

To complete the install of virtualenv  and virtualenvwrapper , open up your ~./profile :

And append the following lines to the bottom of the file:

Now, source  your ~/.profile  file to reload the changes:

Let’s create a new Python virtual environment appropriately named cv :

Timing: 31s

The only requirement to build Python + OpenCV bindings is to have NumPy installed, so let’s use pip  to install NumPy for us:

Timing: 35m 4s

Step #5: Compile and install OpenCV for the Raspberry Pi Zero

We are now ready to compile and install OpenCV. Make sure you are in the cv  virtual environment by using the workon  command:

And then setup the build using CMake:

Timing: 4m 29s

Now that the build is all setup, run make  to start the compilation process (this is going to take awhile, so you might want to let this run overnight):

Timing: 9h 42m

Assuming OpenCV compiled without error, you can install it on your Raspberry Pi Zero using:

Timing: 2m 31s

Step #6: Finishing the install

Provided you completed Step #5 without an error, your OpenCV bindings should now be installed in /usr/local/lib/python2.7/site-packages :

All we need to do now is sym-link the  file (which are our actual Python + OpenCV bindings) into the site-packages  directory of the cv  virtual environment:

Step #7: Verifying your OpenCV install

All that’s left to do now is verify that OpenCV has been correctly installed on your Raspberry Pi Zero.

Whenever you want to use OpenCV, first make sure you are in the cv  virtual environment:

And from there you can fire up a Python shell and import the OpenCV bindings:

Or you can execute a Python script that imports OpenCV.

Once OpenCV has been installed, you can remove both the opencv-3.0.0  and opencv_contrib-3.0.0  directories, freeing up a bunch of space on your filesystem:

But be cautious before you run this command! Make sure OpenCV has been properly installed on your system before blowing away these directories, otherwise you will have to start the (long, 9+ hour) compile all over again!


If you ran into any error installing OpenCV 3 on your Raspberry Pi Zero, I would suggest consulting the Troubleshooting section of this post which goes into added detail for each installation step.

The post also includes a complete 22 minute video where I demonstrate how to run each command to flawlessly get OpenCV 3 installed on your Raspberry Pi:

Figure 2: Getting OpenCV up and running on your Raspberry Pi.

Figure 2: Getting OpenCV up and running on your Raspberry Pi.

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 .


This post detailed how to install OpenCV 3 on your Raspberry Pi Zero. The purpose of this blog post was to provide accurate timings that you can use when planning your own install of OpenCV on your Pi Zero.

In order to get OpenCV up and running, I made the following assumptions:

  • You are running Raspbian Jessie on your Raspberry Pi Zero.
  • You are installing OpenCV v3.
  • You want to use Python 2.7 with your OpenCV bindings.

If you would like to use Python 3+ with your OpenCV bindings, please consult this post, where I have elaborated more on each step, provided more detailed information, and included a 22 minute video that walks you through step-by-step on installing OpenCV 3 on your Raspberry Pi.

, , , , ,

62 Responses to Installing OpenCV on your Raspberry Pi Zero

  1. Sol December 14, 2015 at 11:25 am #


    • Adrian Rosebrock December 14, 2015 at 5:33 pm #

      There actually isn’t picamera module hookup on the Pi Zero. You’ll need to use a USB camera. I prefer the Logitech C920 since it is plug-and-play capable with the Pi.

  2. Marek December 15, 2015 at 3:19 pm #

    I had to install python-dev before installing numpy.

    • Adrian Rosebrock December 15, 2015 at 4:32 pm #

      Thanks for pointing this out Marek! I’ve updated the installation instructions to include installing the Python headers.

  3. Serban Balamaci December 18, 2015 at 5:54 am #

    Hi Adrian, cool site, I’m wondering about the hardware setup for PiZero you have there. I see it has two micro usbs – one will be used for power I think-. To me it’s a pity they did not drop the hdmi in favor of another microusb, I don’t understand how you can use it. You attach the usb/network camera either to the other microusb but then how do you control it – you either need a keyboard attached(and the cool looking small hdmi monitor you have) or a usb->network adapter or usb wifi. Do you have an usb hub?

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

      Indeed, it has two micro-USB ports. The first one is for power. And the second one is used for any peripherals. I ended up plugging in a USB hub, which then allows me to connect USB wifi, keyboard, mouse, and webcam. The HDMI is also min-HDMI.

      • Sam Fattah July 11, 2016 at 1:10 pm #

        Hi Andrian, What type of USB hub did you used? bz mine dose not work for the USB cam.

  4. Loek January 18, 2016 at 1:47 pm #

    Hi Adrian,

    I have installed OpenCV because I’m interested in exploring the possibilities of robot vision. The tests and demo’s work fine. There is, however, one thing I would like to be clarified on. When doing Python stuff I like to work with IDLE. I have started it from the command line in the virtual cv environment with IDLE &. Import numpy works fine, but when I try to import cv2 I get “Import error: No module named cv2. When I start Python from the command line importing cv2 works fine. But then, of course I miss all the advantages of IDLE. Is there a way of fixing this? I already tried sudo apt-get install python from within the virtual environment, but the message I get says the the latest version is already installed.
    Thank you

    • Adrian Rosebrock January 18, 2016 at 3:17 pm #

      I don’t personally use IDLE (other than the command line version), so I’m not sure about this problem. If you like using the GUI version of IDLE, I would suggest installing IPython Notebooks, that way you can code similar IDLE, only in your browser. Plus, this has the advantage of working with the Python virtual environment.

  5. Loek January 19, 2016 at 2:57 pm #

    Hi Adrian,

    Thank you very much for your reply. Meanwhile I investigated the matter a little bit further and found out that putting the symbolic link ln -s /usr/local/lib/python2.7/site-packages/ in the /usr/bin/ directory because that’s where the Python executable is. This solves the problem and I am now able to use IDLE within the virtual environment.

    Kind regards

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

      Great, thanks for sharing Loek!

  6. Randy Marsh June 15, 2016 at 6:59 am #

    Have you tried running make -j to speed up the build ?

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

      Yes, you can use the -j flag to speedup the build; however this is only useful on the Pi 2 and Pi 3 where there are multiple cores. On the Pi Zero, this will not speedup the build.

  7. jonasdig August 2, 2016 at 3:09 pm #

    Hi, do you think the Zero is powerful enough to handle motion detection with OpenCV and the camera module v2?

    • Adrian Rosebrock August 4, 2016 at 10:19 am #

      For very basic motion detection, you can use the Zero, but you’ll struggle to get more than a couple frames per second processed (if that). I would suggest using this tutorial as a starting point.

  8. zook October 20, 2016 at 10:58 am #

    Don’t know why but in my case cv2 go to /usr/local/lib/python2.7/dist-packages not /usr/local/lib/python2.7/site-packages

  9. Frank November 22, 2016 at 9:37 am #

    Is it possible to have an image of raspbian + opencv for raspberry pi zero, 2 and 3 ready to download and flash it on the SD please ?
    I did try to install open cv following your tutorial on raspberry pi 2 and zero but there is always something wrong somewhere.
    For example, right now I have issues on cmake, it failed for many reasons and it will take huge amount of time for a newbie to understand what’s wrong.

    Please, I think that is the fasted and more helpful way.

  10. Edison March 10, 2017 at 9:13 pm #

    Hi. Adrian.
    What about using cross compiling to speed up make?
    For the RPi zero, avoid virtual environment wouldn’t be better?

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

      I haven’t tried cross-compiling before for the Raspberry Pi, it might be something worth looking into. As for avoid Python virtual environments on the Pi Zero, I’m not sure what the end goal would be? That wouldn’t impact performance.

  11. Edison March 13, 2017 at 4:34 pm #

    Thank you.
    I’ll try cross-compiling. If I succeed I’ll come back with the results.

  12. Rodo April 12, 2017 at 4:44 am #

    Hi Adrian. Thanks for the post. I suggest people compiling it on a headless pi (using SSH) to call make as:
    $make > progress.txt 2>&1 &
    It runs make in background, writing the stdout and stderr output to a file avoiding the typical issues with the SSH connections and allowing to close the session. To check the progress, open a new SSH and call:
    $tail -f progress.txt
    Kind regards

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

      I would also recommend using screen as well.

    • Charles Schultz May 16, 2017 at 1:19 pm #

      Actually, I would recommend installing ‘screen’ and run it that way, especially if your connection is short-lived for any reason (intentional or accidental).

      sudo apt-get screen

      ^A^D to leave screen while it is still running
      screen -r to get back to your screen session, even from a completely different ssh session.

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

        The screen command is an excellent utility, thanks for mentioning it Charles!

  13. hashbang April 16, 2017 at 5:16 pm #


    for a lane detection project with opencv in python do you believe pi zero w is good choice or i should get pi 3?

    • Adrian Rosebrock April 19, 2017 at 1:04 pm #

      The Pi Zero realistically isn’t suitable for working with video. I would recommend you use the Pi 3.

  14. Pranav Jain April 24, 2017 at 12:30 pm #


    I tried installing opencv along with opencv_contrib , i did not run into any issue and it compiled , i am able to import opencv in my python environment but the modules from the opencv_contrib (xfeatures2d etc) are not available. I followed all the procedures listed here exactly. Do i need to add something extra in cmake statement . Any ideas as to how i can resolve this ?

    • Adrian Rosebrock April 25, 2017 at 11:53 am #

      Double-check your CMake and specifically check the “Modules to be Compiled” section. You’ll likely find that xfeatures2d was not included. 99% of the time that is due to your path to the opencv_contrib/modules directory being incorrect.

  15. Charles Schultz May 16, 2017 at 1:16 pm #


    Thanks for putting this up, and your other posts as well – I am really digging the tutorial/”learning by example” approaches. This is excellent stuff!

    Just curious, beyond object detection, have you used CV for “objects in motion” detection? I know you have done some stuff with people/facial recognition, but I am gearing towards controlling a small quad-copter (orange cheerson cx-10). I am completely new to CV and still have a lot to learn, but I am trying to figure out the best way to determine 3d movement from a 2d image stream – obviously, directions parallel to the plane of viewing is pretty clear (up, down, left, right), but along the plane of view seems more of a challenge (forward, backward). Have you tackled anything like that?

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

      If both your camera and your object is moving then things become a bit more challenging. Methods like correlation tracking would work better in that situation.

    • Ansh May 25, 2017 at 3:49 pm #

      Oh I came across a similar situation in tracking objects and associating a control logic for the motors. The x and y as you said is pretty simple. For the depth (forward and backward), what worked for me was the area of ROI rectangle. What I mean –

      The recognized object can encapsulated in a rectangle (see Adrian’ previous blogs)~cv2.rectangle from where one is getting – x,y,w,h values. w*h provides the area of the rectangle. So as the object moves away from the camera the area of rectangle decreases and as the camera moves closer the area of the rectangle increases. Also at each depth plane the value of this area is unique. This value of the area can be interpreted as Z value and combined with X and Y values can provide variables to do design a control logic.

      I have yet to play with DLIB library and correlation tracking etc and maybe that will be better. But the above mentioned approach works for me just fine.

  16. Ansh May 25, 2017 at 3:51 pm #

    Hi Adrain,

    Do you have comments/adivce on overclocking the Zero for a better Open CV performance? I am just using for Face Recog using LBPHFace Detector. If my application needs, do you recommend some safe way/settings for the same?

    ~Ansh Verma

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

      I really don’t recommend the Zero for real-time computer vision applications. Use the Raspberry Pi 3 instead. It’s much faster and better suited for CV applications.

  17. Ansh May 25, 2017 at 5:31 pm #

    Also, I understand that we can run OpenCV applications via the GPU of the raspberry pi for better performance – it is suppose to be faster?

    Do you have any comments on it? Say instead of overclock the pi. Will python codes run via GPU and how does one do that?

  18. Carlos Simoes June 7, 2017 at 4:47 am #

    Hi Adrian,

    I am from Portugal, do you have programming for recognize one label in glass from car .

    my ideia is install two camera in park and they see the glass front and back and recognize the label if the car have this label. if not create alarme.

    can you help me about this idea .

    thanks your attention .

    best regards.


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

      Hi Carlos — I’m not sure what you mean by “recognize one label in glass from car”. Can you please elaborate?

  19. Rui L. June 28, 2017 at 11:34 pm #

    I just followed this post and successfully compiled opencv-3.0.0 on C.H.I.P headless 4.4. The final build took a little bit more than 5 hours. I didn’t need an external USB drive as the build only needed about 2.3 GB free space.

    Thanks Adrian!

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

      Congrats on getting OpenCV installed, Rui — nice job!

  20. Mohamed Abdelkader July 2, 2017 at 4:12 pm #

    Thanks for the tutorial. What is the expexted frame rate if I want to do blob detection, just simple color detection, on Pi zero?

    • Adrian Rosebrock July 5, 2017 at 6:21 am #

      The Pi Zero really isn’t suitable for doing real-time frame processing. I would highly suggest that you use a Pi 2 or a Pi 3 instead.

  21. NVibe July 27, 2017 at 8:04 am #

    Thanks for this write up. After going through the compilation process of 3.2.0 a few times, I keep getting an “illegal instruction” message when in python, importing cv2 on the Pi Zero. I must confess I had followed another tutorial, so will give your a go.

    On another note, I am curious to know what can be realistically achieved on a PI Zero in small project terms. I am a newbie to the topic.

    Is basic face tracking &/or recognition possible?

    Is basic object tracking & /or recognition possible?

    How about multiple faces (counts &/or recognition)?

    You mention real time frame processing not suitable on the platform, does that discount using the above methods entirely?

    Do you have a guide for the types of applications (examples) best suited to each Pi platforms limitations in plain speak?

    Thanks again for your write up.

    • Adrian Rosebrock July 28, 2017 at 9:50 am #

      In all honesty, I do not recommend the Pi Zero for anything other than super, super basic techniques. It’s more of a “learning platform” for basic OpenCV operations. Face tracking and face recognition will be far too slow on the Pi. I would highly encourage you to use a Pi 3 instead. The speed boost from the 4 cores is well worth it.

      For what it’s worth, both the Quickstart Bundle and Hardcopy bundle of my book, Practical Python and OpenCV, include a pre-configured Raspbian .img file with OpenCV pre-installed. Simply download the .img, flash it to your Pi, and boot. It’s by far the fastest way to get up and running with OpenCV on your Pi.

  22. khaled July 28, 2017 at 6:32 am #

    when I ran : ls -l /usr/local/lib/python2.7/site-packages

    I get total of 0

    there were not any problem with build and make

    • Adrian Rosebrock July 28, 2017 at 9:42 am #

      Check the output of your build/lib directory to see if your bindings are there. If not, then your CMake command was configured improperly.

  23. Sam B September 22, 2017 at 12:06 pm #

    I got a fatal error for math.h

    • JanakaS October 5, 2017 at 10:26 am #

      Hey, I had the same problem.. this has got to do with gcc using precompiled correct this do the following:

      In the cmake command in Step#4 include another ‘segment’ at the end as:

      The time it takes to compile not is much greater… maybe 20 hrs.. but, it compiles all the way!

      Found it from:


      • Graham December 20, 2017 at 6:26 am #

        Thank you, thank you, thank you 🙂

  24. Danish December 2, 2017 at 1:08 pm #

    Thanks JanakaS m looking for this solution from past 2 days and finally it’s compiling

  25. Vixel December 12, 2017 at 10:16 am #

    hey, the power to the raspberry pi zero got interrupted and now the putty became inactive! and it was 31%. I was doing it for four hours on that ‘make’ command and its all gone now!!! Is there any easier way? like, imaging it in the sd card directly? Will it again run for 9hrs or will there be any data already present to speed up upto 31%??

    • Adrian Rosebrock December 15, 2017 at 8:44 am #

      I have two questions:

      1. Use the “screen” command to multiplex your terminal. This way if you lose network connection to the Pi the compile is not interrupted. If you lose power; however, there is not much you can do. You will need to restart the compile. In general restarting an interrupted compile will lead to errors (not always, but it does happen).

      2. I actually offer a pre-configured Raspbian .img file inside the Quickstart Bundle and Hardcopy Bundle of my book, Practical Python and OpenCV. Just download the .img file, flash it to your card, and boot. Be sure to take a look!

      I hope that helps.

      • Vixel December 22, 2017 at 12:18 am #

        Hey, is it possible to bring Raspbian OS on a VMware or something, install opencv (the long ever make command), make an image file and put it on the Pi board. That way you can use the processor of our laptop. Is it possible????

        • Adrian Rosebrock December 22, 2017 at 6:38 am #

          You can now install Raspbian on your laptop/desktop and play around with it, but you cannot configure Raspbian on your laptop, build the .img, and then move it to your Pi.

  26. Rich1812 December 26, 2017 at 7:57 am #

    Hello Adrian, I installed opencv on the pi Zero successfully. However I only have python2 installed in the virtual environment, because at the time I wasn’t sure if it will work, but it does. Now I would like to add python3 capability, I would like to add with python3 binding.

    my question is, after opencv is complied, can I simply install python3? Do I need to re-compile the whole thing again? I use a pi Zero W, It took about 32 hours for me to download all dependencies to fully compile and install, it was painfully slow, time consuming! I really don’t want to have to go through it again. Thanks.

    • Adrian Rosebrock December 26, 2017 at 3:47 pm #

      Unfortunately you would need to recompile OpenCV for Python 3 all over again. I know that’s a bummer but unfortunately there isn’t a way around it at this point.

  27. Amir December 27, 2017 at 6:07 pm #

    Also is it possible to recompile opencv from the very start of this tutorial again without having to reformat the card and lose everything else? Like I can access the files via VNC viewer so i’m wandering if i delete the opencv folder completely in VNC, would I be able to run everything again from the start of this tutorial from step 1 again or no? If not whats the easiest way?


    • Adrian Rosebrock December 28, 2017 at 2:10 pm #

      Yes, just delete your OpenCV or “build” directories and re-run CMake and make.

  28. Daniel January 17, 2018 at 5:04 am #

    Hi Adrian, Thanks for your great tutorial. I was wondering if running a virtual environment would also cause an increase in power consumption? I am running a headless setup with the pi camera as a motion sensing camera, but need it to be as power efficient as possible. Thanks again!

    • Adrian Rosebrock January 17, 2018 at 10:09 am #

      No, a Python virtual environment would have zero increase in power consumption. A Python virtual environment is not a “virtual machine” that has heavy implications on your CPU/RAM/power consumption. A Python virtual environment, in essence, just creates a new Python install in your home directory, under a new name, allowing you to have multiple environments on your Pi.

Leave a Reply