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
$ 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
$ curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" $ sudo python get-pip.py
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
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/virtualenvwrapper.sh
then reload the content of
~/.bashrc file by running
$ source ~/.bashrc
Now is the time to create a virtual environment, let's call it
$ 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
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
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 https://github.com/Itseez/opencv.git $ 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 https://github.com/Itseez/opencv_contrib.git $ 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
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D INSTALL_C_EXAMPLES=OFF \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D BUILD_EXAMPLES=ON \ -D BUILD_OPENCV_PYTHON2=ON ..
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
cv2.so 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/cv2.so cv2.so
Your installation is successfuly completed. You can confirm it by going into the virtual environment and importing OpenCV library.
$ workon cv
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.