Install OpenCV 3.1 and Python 2.7 on CentOS 7

Among different Linux distributions, Fedora, Redhat, and CentOS are closely related and you can call them a family. In a nutshell, Redhat is a commercial release of Fedora, run by Redhat company and is not free. CentOS is a free version of commerical release of Redhat run by the community. I'm going to show you how to install OpenCV 3 with Python 2.7 support in CentOS 7 but you should be able to apply similar steps in order to install OpenCV on Fedora and Redhat.

Preparing the environment

Check your CentOS version:

$ cat /etc/centos-release

I have tested these steps on CentOS 7 but it should work seamlessly on CentOS 6 as well.

Update yum package manager:

$ sudo yum -y update

Note: This will update every installed package. In rare cases you may not want to update all of your packages!

At the time of writing this post, for OpenCV 3.1, there is no pre-built binaries available in CentOS repositories. They may add it later and you can check its availablity by running this command

$ yum list available opencv\*

and if you see there is an available package for 3.1 you can just go ahead and install it through yum. Having said that, you can always compile OpenCV from the source and I will walk you through all the steps in this tutorial.

Let's start by installing core development tools for CentOS. These are basic tools that are required to compile software and build new rpms. Additionally any CentOS user may need them, even if you are not a developer, so go ahead and install them by typing the following command in your command line

$ sudo yum groupinstall 'Development Tools'

and continue with installing some additional developer tools cmake, git, and pkgconfig

$ sudo yum install cmake git pkgconfig

Note: Some of these tools might be already installed

The main purpose of OpenCV is to work with images and multimedia files. To support various image formats such as PNG, JPG, JPEG2000, TIFF, WebP etc., we need to install the following libraries

$ sudo yum install libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel

and for working with video files

$ sudo yum install libdc1394-devel libv4l-devel gstreamer-plugins-base-devel

Additionally you need to install GTK development library to display images

$ sudo yum install gtk2-devel

If you need to take advantage of Intel's Threading Building Blocks (TBB) in OpenCV, you need to install tbb-devel library. Also OpenCV uses another library Eigen for optimized mathematical operations.

$ sudo yum install tbb-devel eigen3-devel

Install python package manager pip

$ curl "" -o ""
$ sudo python

Install virtualenv and virtualenvwrapper in order to create separate python environments. If you are new to python and are not familiar with how virtualenv works, fear not! just look it up and take a moment to read about this great tool. As a developer, you'll see how powerful this tool is and how it will make your life easier.

$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip

In order to ensure that virtualenv and virtualenvwrapper are loaded when you login, you need to update your ~/.bashrc by adding these two lines to the file.

$ export WORKON_HOME=$HOME/.virtualenvs
$ source /usr/bin/

then reload the content of ~/.bashrc file by running

$ source ~/.bashrc

Now is the time to create a virtual environment, let's call it cv

$ mkvirtualenv cv

You will notice a (cv) indicator at your command line. It means that we are in the virtual environment where we will be installing OpenCV. You can exit this environment by typing

$ deactivate

Important: From now on, at each step of installation and development, you should make sure that you are in the virtual environment that you want to install OpenCV. So, if you are not in the cv virtual environment, just type in

$ workon cv

and you will be back in the cv environment, easy! right?

To make sure that a right version of python is available in your virutal environment, just type in

$ python --version

and you should see Python 2.7 version.

You can install other versions of python in this virtual environment without changing the original python that is shipped with the operating system. In fact, you should abstain from working with the default Python interpreter as critical system tools depend on it. Instead, you should opt for creating virtual environments using virtualenv and start working inside these environments. This way you can avoid interrupting operating system. Now you can realize why virtual environments are important specially when you are dealing with python.

Install Python 2.7 development tools

$ sudo yum install python-devel

Install NumPy since OpenCV is using its arrays (and it is a very powerful package as well)

$ sudo pip install numpy

Installing OpenCV

The preparation steps are done and now we are ready to get the OpenCV source code and install it. To check out the OpenCV core library, run the following commands

$ cd ~
$ git clone
$ cd opencv
$ git checkout 3.1.0

We also should check out the contributed modules for the same version. These modules are not released as a part of official OpenCV distribution but provide some of the most interesting functionalities in OpenCV and perhaps are the main reason for you to use OpenCV library.

$ cd ~
$ git clone
$ cd opencv_contrib
$ git checkout 3.1.0

Now that we checked out both OpenCV and its contributed modules, we can set up the build where cmake plays its role.

$ cd ~/opencv
$ mkdir build
$ cd build
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \

Then compile and install

$ sudo make
$ sudo make install
$ sudo ldconfig

Check the following folder and see if OpenCV is installed


Specifically you should see in this directory. If you don't see this file, the OpenCV library is not installed properly and you should check all the above steps again.

Now is the time to create a symbolic link to OpenCV library into the site-packages directory of the cv virtual environment:

$ cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/

Your installation is successfuly completed. You can confirm it by going into the virtual environment and importing OpenCV library.

$ workon cv
$ python
>>>import cv2

P.S. This tutorial will be updated in the future to adapt to new versions of OpenCV. (Last update: November 12, 2016)

P.S. This tutorial has been partially inspired by the PyImageSearch tutorials on installing OpenCV on Ubuntu.


many missing packages e.g. videodev.h, libavutil building on Centos 7

I have followed your instructions carefully but am gettign many errors building OpenCV on Centos 7. 

I looked for and installed any similar packages I could find (a lot) but am still getting most of the errors. 

$ sudo yum install gstreamer1
$ sudo yum install gstreamer1-libav
$ sudo yum install  gstreamer1-plugins-base-devel
$ sudo yum install libavutil libswscale libavresample ffmpeg libgphoto2 gphoto2
No package libavutil available.
No package libswscale available.
No package libavresample available.
$ sudo yum install vtk ... 


Errors from Cmake:

-- Looking for linux/videodev.h - not found
-- Looking for linux/videodev2.h
-- Looking for linux/videodev2.h - found
-- Looking for sys/videoio.h
-- Looking for sys/videoio.h - not found
-- checking for module 'libavcodec'
--   package 'libavcodec' not found ...
-- checking for module 'libavformat'
--   package 'libavformat' not found
-- checking for module 'libavutil'
--   package 'libavutil' not found
-- checking for module 'libswscale'
--   package 'libswscale' not found
-- checking for module 'libavresample'
--   package 'libavresample' not found
-- Looking for libavformat/avformat.h
-- Looking for libavformat/avformat.h - not found
-- Looking for ffmpeg/avformat.h
-- Looking for ffmpeg/avformat.h - not found
-- checking for module 'libgphoto2'
--   package 'libgphoto2' not found

I just posted a question "many missing packages e.g. videodev.h, libavutil building on Centos 7"

I want to add--after installing packages to try to fix the problem, some of the files are present on my system but in different places than OpenCv expects--

/usr/include/libpng/png.h: No such file or directory
-- Looking for linux/videodev.h - not found
-- Looking for sys/videoio.h - not found

Files exist but in different places: (after installing pjproject-devel, ptlib-devel)

$ locate png.h

$ ls /usr/include/ptlib/videoio.h

$ ls /usr/include/pjmedia-videodev/videodev.h

Hi Tom, thank you for your comment. The libraries that I have covered in this post are just some basic libraries that you may or may not need in your project. May I ask what exatcly you are trying to accomplish with these additional libraries?  Based on the information that you have provided, it seems that you did not install these libraries in the python environment. Please make sure to be inside the python environment when you are installing any additional library. Let me know if that helps.

when I did the step " sudo make ",some mistake appeared,

make[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/grfmt_jpeg2000.cpp.o] Error 1

make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2

make: *** [all] Error 2

would you please tell me how to solve this

Add new comment