Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Step by step instructions to build and install OpenCV from source on CentOS 7
# This gist is a step by step instructions to build and install OpenCV from source on CentOS 7
# note: The easy and quick way to install is
# sudo yum install opencv opencv-devel opencv-python
# But this easy pypi installation can’t open video files on GNU/Linux distribution or on mac OS X system.
# And on some system opencv binaries provided packages are not compiled.
# Therefor we have no way rather than build it from source.
### first update and upgrade pre-install yum packages.
sudo yum update
sudo yum upgrade
sudo yum install -y epel-release git gcc gcc-c++ cmake3
sudo yum install -y qt5-qtbase-devel gtk2-devel
sudo yum install -y libpng-devel jasper-devel openexr-devel libwebp-devel
sudo yum install -y libjpeg-turbo-devel libtiff-devel libdc1394-devel
sudo yum install -y tbb-devel eigen3-devel gstreamer-plugins-base-devel
sudo yum install -y freeglut-devel mesa-libGL mesa-libGL-devel boost boost-thread boost-devel libv4l-devel
sudo yum install -y libavcodec-devel libavformat-devel libavutil-devel libswscale-devel
# installing dnf
sudo yum install dnf -y
sudo dnf install gstreamer1-devel gstreamer1-plugins-base-tools gstreamer1-devel-docs
sudo dnf gstreamer1-plugins-base-devel gstreamer1-plugins-base-devel-docs
### downloading opencv and opencv_contrib packages from their GitHub repositories
git clone
git clone
### checkout the same version opencv and opencv_contrib
cd opencv
git checkout 3.4.5
cd ..
cd opencv_contrib
git checkout 3.4.5
cd ..
#### now compile and install OpenCV with contrib modules
# create a build directory
cd opencv
mkdir build
cd build
# configure our build using cmake
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D PYTHON3_EXECUTABLE=/usr/bin/python3.6 \
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.6 \
-D BUILD_opencv_cudacodec=OFF \
-D WITH_1394=OFF \
# OPENCV_EXTRA_MODULES_PATH path can be differenet depending upon opencv_contrib/modules location
# python executable path can be found by entering following code in python terminal
# for python3 execuatable path open the terminal and type python3. and then enter
# import sys; print(sys.executable)
# compile opencV in the same the build folder
make -j4
# here 4 is the availabe core of my processor
# to find number of CPU cores in your machine enter "nproc" command on your terminal
# to speed up compiling process enter highest number of available cores on your processor.
# now install
sudo make install
# my is saved here
cd /usr/local/lib/python3.6/site-packages/
sudo mv
# to get site-packages location type: python3 -c "import site; print(site.getsitepackages())"
sudo cp /usr/lib/python3.6/site-packages/
## well that's it! to confirm your installation go to the python terminal and enter
## import cv2; print(cv2.__version__)
## if that outputs "3.4.5" and we are done !!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment