Installing OpenCV on the Raspberry Pi

This article covers how to install OpenCV on raspberry pi. You might already have a raspberry pi with OpenCV installed. You can check using:

$ python
>>> import cv2
>>> print cv2.__version__

You should get ‘3.2.0’.

To install things on the raspberry pi, you will need unrestricted internet access. You can either try to use a proxy (this may not work) or take the raspberry pi home and directly plug it into your router with an ethernet cable.

You will also want an HDMI cable and a monitor so that you can see the rasbperry pi gui, as well as an extra keyboard and mouse.

To power the raspberry pi, you’ll need a mini USB power cable (the type of charging cable usually used for most android phones).

Installing Raspbian

If you don’t already have a microSD card with Raspbian on it, you will have to create one. The minimum SD card size to install Rasbian is 8 GB, but if you don’t want to have to deal with mounting a flash drive to download OpenCV, you will want a 16 GB microSD card.

If the Raspberry Pi boots up and shows the desktop and/or a command line, then Raspbian is already installed and you can skip this section.

There are 2 variants of Raspbain, the full version with a Desktop and a Lite version that is just a command line. The Lite version is reccomended.

Instructions for installing Raspbian with NOOBS (a very large but streamlined installer) can be found here, but it’s reccomended that you just directly flash the Pi with a raspbian image.

To Flash the Pi with Raspbain:

  1. Download a copy of the raspbian image from here, choosing either the desktop or lite variant
  2. Obtain a microSD card that’s at least 8 GB (16 GB recommended for easier installation of OpenCV).
  3. Download a copy of Etcher and follow the instructions to select the SD card and raspbian image.
  4. Flash the SD card with Etcher
  5. Put the SD card in the Raspberry Pi and boot it up. To turn on the raspberry pi, you only need to plug it in.

Once the raspberry pi has booted up and you can see the command line or the desktop, you’re done.

That should be it for installing Raspbian.

Installing OpenCV

Since Python already comes installed with Raspbian, we don’t need to install Python. However, we do need to install OpenCV.

To do so, follow the instructions here.

These instructions are copied down (since GitHub is sometimes blocked on school wifi), with a few explanations and edits.

1. Update the RPi

Update software:

$ sudo apt-get update
$ sudo apt-get upgrade

Update firmware:

$ sudo rpi-update

Reboot to apply updates.

$ sudo reboot now

2. Install necessary software

Install cmake, which is used to build OpenCV from its source code

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

This next step might take a long while, since it’s a lot of software to install.

$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

$ sudo apt-get install libgtk2.0-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libv4l-0 libv4l-dev

$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev

$ sudo apt-get install libatlas-base-dev gfortran

$ sudo apt-get install python-numpy python-scipy python-matplotlib

$ sudo apt-get install default-jdk ant

$ sudo apt-get install libgtkglext1-dev
$ sudo apt-get install v4l-utils

3. Install numpy, which is necessary to use OpenCV

Install pip, python’s package manager:

$ wget
$ sudo python

Install numpy:

$ sudo pip install numpy

4. Format and mount the USB drive to store OpenCV

If you’re using a 16 GB (or larger) SD card, skip this step.

If you’re using an 8 GB SD card, you will have to download OpenCV to a flash drive, since OpenCV is quite large and won’t fit on the SD card. However, once built, the OpenCV library is quite small and can easily fit on the SD card even though the source code may be too large to fit. So, we simply download the OpenCV source code to the flash drive and build it to the Raspberry Pi.

We’re going to have to format the flash drive to ext4, which will wipe it, so make sure nothing important is on there. The flash drive has to be formatted, because usually, flash drives don’t support symbolic links.

So, plug in the flash drive and run:

$ ls /dev/sd*

You should see which directory the USB drive is under. Usually, it’s something like “/dev/sda”, “/dev/sda1”, or “/dev/sda2”.

For the rest of this guide, I assume that the directory was “/dev/sda1”. Remember, however, to replace “dev/sda1” with the correct directory if yours is different.

To format the USB drive to ext4, which supports symbolic links, use:

$ sudo mkfs.ext4 /dev/sda1 -L untitled

Now, we will mount the USB to the RPi so that you can access it.

First, make the directory you’re going to mount to.

$ sudo mkdir -p /mnt/usb

Then, mount:

$ sudo mount /dev/sda1 /mnt/usb

5. Download and extract the OpenCV source code

If you followed Step 4, then change directories to your USB drive.

$ cd /mnt/usb

If you didn’t have to, then change to your home directory.

$ cd ~

Then, download and extract OpenCV.

$ wget -O
$ unzip
$ wget -O
$ unzip

These commands will take a while depending on your internet connection since OpenCV is quite large.

6. Prepare to build OpenCV

If you followed step 4, use:

$ cd /mnt/usb/opencv-3.2.0/

Otherwise, use:

$ cd ~/opencv-3.2.0/

Prepare to build OpenCV with these options:

$ mkdir build
$ cd build
        -D CMAKE_INSTALL_PREFIX=/usr/local \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.2.0/modules \
        -D ENABLE_NEON=ON ..

To enter arguments on a newline in the terminal as shown above, press the key and then press enter. Note that you can just enter all those arguments on one line, however messy that may be.

7. Build OpenCV

It’s recommended to let this run overnight, since it takes a LONG time (4 or more hours).

The safest option is to use:

$ sudo make

If you want to use multiple cores (to speed things up), use:

$ sudo make -j3

This may fail sometimes, however. If the process fails, use:

$ sudo make clean

and try again.

8. Install the build

This step also takes a while, but not nearly as long as building.

$ sudo make install
$ sudo ldconfig

9. Configure OpenCV

Edit the OpenCV config file:

$ sudo nano /etc/

The file will be blank, so add to the bottom of the file:


with a blank line at the end.

Use Ctrl-O, then Ctrl-X to save and exit.

$ sudo ldconfig

Now, configure bashrc:

$ sudo nano /etc/bash.bashrc

Add these lines at the bottom:


Add a blank line at the bottom, and once again, Ctrl-O and Ctrl-X to save and exit.

Now, reboot.

$ sudo reboot now

10. Check to make sure it worked

When you use:

$ python
>>> import cv2
>>> print cv2.__version__

it should print out ‘3.2.0’. If it did not, then something went wrong, and recheck your steps.

If it worked, then you’ve successfully installed OpenCV on the RPi. You may want to make a backup image of the SD Card using a tool like Win32 Disk Imager so that you never have to go through this extremely long process again.

Installing the Picamera

f you’re using a RPi and OpenCV, you’re probably using a Raspberry Pi Camera. This means that you must also install the Picamera.

Firstly, plug in the camera.

Then, use raspi-config and enable the camera:

$ sudo raspi-config

Then, select Enable Camera, and enable it.

To test the camera, use:

$ raspistill -t 5000 -o image.jpg

This shows a preview of the camera feed for 5 seconds, then takes a picture and saves it to image.jpg.

Then, if the picamera works, install the python package:

$ sudo pip install picamera

$ sudo pip install "picamera[array]"

After that’s done, then the entire installation process is complete. Hooray! I hope you never have to do that again.