How to Install OpenCV on Raspberry Pi 3

Published on

4 min read

Install OpenCV on Raspberry Pi

OpenCV (Open Source Computer Vision Library) is an open-source computer vision library and has bindings for C++, Python, and Java. It is used for a very wide range of applications including medical image analysis, stitching street view images, surveillance video, detecting and recognizing faces, tracking moving objects, extracting 3D models and much more.

OpenCV can take advantage of multi-core processing and features GPU acceleration for real-time operation.

In this tutorial, we will explain how to install OpenCV on Raspberry Pi.


We’re assuming that you have Raspbian installed on your Raspberry Pi .

Installing OpenCV from the Raspbian Repositories

The OpenCV Python module is available from the standard Raspbian repository. At the time of writing, the version in the repositories is 3.2 which is not the latest version.

To install OpenCV Python module, run the following commands:

sudo apt updatesudo apt install python3-opencv

The command above will install all packages necessary to run OpenCV.

To verify the installation, import the cv2 module and print the OpenCV version:

python3 -c "import cv2; print(cv2.__version__)"

If you want to install OpenCV with Python 2 bindings install the python-opencv package.

Install OpenCV from Source

The recommended way of installing OpenCV is by building the library from the source. This way you will have complete control over the build options and OpenCV will be optimized for your system.

Start by increasing the swap space to avoid compilation hangups due to memory problems:

sudo nano /etc/dphys-swapfile

Change the CONF_SWAPSIZE value from the default 100 to 1024:


Save the file and run the following command for changes to take effect:

sudo /etc/init.d/dphys-swapfile restart

We can now start building OpenCV. First, update the packages index and install the build tools and required dependencies:

sudo apt update
sudo apt install build-essential cmake git pkg-config libgtk-3-dev "libcanberra-gtk*"
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev
sudo apt install libjpeg-dev libpng-dev libtiff-dev gfortran openexr libatlas-base-dev opencl-headers
sudo apt install python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev

Create the build directory , navigate to it and clone the OpenCV and OpenCV contrib repositories from Github:

mkdir ~/opencv_build && cd ~/opencv_build
git clone
git clone

At the time of writing, the default version in the GitHub repositories is version 4.1.1. If you want to install an older version of OpenCV, navigate to both opencv and opencv_contrib directories and run git checkout <opencv-version>.

Once the repositories are cloned, create a temporary build directory, and change to it:

mkdir -p ~/opencv_build/opencv/build && cd ~/opencv_build/opencv/build

Set up the OpenCV build configuration with cmake:

    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \

The output will look something like below:

-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/opencv_build/opencv/build

Run make to start the compilation process:

make -j4

The process will take some time, around 1 - 2 hours, depending on the Raspberry Pi model. If the compilation fails at some point, due to resources not available, run the make command again and the process will continue from where it stopped.

When completed you will see something like below:

[100%] Linking CXX shared module ../../lib/python3/
[100%] Built target opencv_python3

The final step is to install compiled OpenCV files:

sudo make install
-- Installing: /usr/local/bin/opencv_version
-- Set runtime path of "/usr/local/bin/opencv_version" to "/usr/local/lib"

To check whether OpenCV has been installed successfully type the following commands and you should see the OpenCV version:

C++ library:

pkg-config --modversion opencv4

Python library:

python3 -c "import cv2; print(cv2.__version__)"

Clean Up

If you don’t have much free space on your SD card, delete the source files:

rm -rf ~/opencv_build

Heavy swap usage could damage your SD card. Change back the swap space to its original size:

sudo nano /etc/dphys-swapfile

Edit the CONF_SWAPSIZE value to 100:


Save the file and activate the changes:

sudo /etc/init.d/dphys-swapfile restart


We have shown you how to install OpenCV on your Raspberry Pi board. Building OpenCV from source gives you more flexibility and it should be your first option when installing OpenCV.

If you have any questions or feedback, feel free to comment below.