Introduction
Install
├─ Build from sources
└─ Package manager
Upgrade
Uninstall
Conclusion
References
Appendix
Introduction
OpenCV is a powerful computer vision library used by developers worldwide. In this tutorial, we will walk you through the steps to install/ upgrade/ uninstall any version of OpenCV on Linux by building from sources or using package manager, focusing on Python and C++ languages.
Install
Build from sources
To build OpenCV from sources, follow these steps:
Create/ activate the Python environment and make sure to install NumPy in the environment.
1
2
3
4
5
6
7
conda create -n test_opencv python=3.9
conda activate test_opencv
which python
# /home/$(whoami)/anaconda3/envs/test_opencv/bin/
python --version
# Python 3.9.17
pip install numpy
Update and upgrade the system packages, and install essential build tools and libraries needed for OpenCV.
1
2
sudo apt update
sudo apt upgrade
1
2
3
4
5
6
sudo apt install build-essential
sudo apt install gcc cmake git wget unzip
sudo apt install libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt install libtiff5-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev
Clone the official opencv/opencv releases
and opencv/opencv_contrib tags
repositories and checkout the desired version.
1
2
3
4
git clone https://github.com/opencv/opencv ~/opencv
git clone https://github.com/opencv/opencv_contrib ~/opencv_contrib
cd ~/opencv/ && git pull origin master && git checkout <version>
cd ~/opencv_contrib/ && git pull origin master && git checkout <version>
If desired, customize the build configuration using CMake (python module for example). Adjust the options based on our specific requirements.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
cd ~/opencv/
mkdir -p build && cd build
cmake -D BUILD_TIFF=ON \
-D WITH_CUDA=OFF \
-D ENABLE_AVX=OFF \
-D WITH_OPENGL=OFF \
-D WITH_OPENCL=OFF \
-D WITH_IPP=OFF \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D WITH_EIGEN=OFF \
-D WITH_V4L=OFF \
-D WITH_VTK=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D PYTHON3_EXECUTABLE=$(which python) \
-D PYTHON_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \
-D PYTHON3_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
-D PYTHON_DEFAULT_EXECUTABLE=$(which python) \
~/opencv/
# General configuration for OpenCV 3.4.16 =====================================
# -- Version control: 3.4.16
#
# ...
#
# -- C/C++:
# -- Built as dynamic libs?: YES
# -- C++11: YES
#
# ...
#
# -- Python 3:
# -- Interpreter: /home/$(whoami)/anaconda3/envs/test_opencv/bin/python3 (ver 3.9.17)
# -- Libraries: /home/$(whoami)/anaconda3/envs/test_opencv/lib/libpython3.9.so (ver 3.9.17)
# -- numpy: /home/$(whoami)/.local/lib/python3.9/site-packages/numpy/core/include (ver 1.23.4)
# -- install path: /home/$(whoami)/anaconda3/envs/test_opencv/lib/python3.9/site-packages/cv2/python-3.9
#
# ...
#
# Install to: /usr/local
Start the build process using make, and then install OpenCV to the system.
1
2
3
4
5
6
7
make -j$(nproc --all) # jcore check cores: grep 'cpu cores' /proc/cpuinfo | uniq hoac nproc --
# [100%] Linking CXX shared module ../../lib/python3/cv2.cpython-39-x86_64-linux-gnu.so
sudo make install
# -- Installing:
# -- Set runtime path of "..." to "/usr/local/lib"
# -- Installing: /home/$(whoami)/anaconda3/envs/test_opencv/lib/python3.9/site-packages/cv2/python-3.9/cv2.cpython-39-x86_64-linux-gnu.so
# -- Set runtime path of "/home/$(whoami)/anaconda3/envs/test_opencv/lib/python3.9/site-packages/cv2/python-3.9/cv2.cpython-39-x86_64-linux-gnu.so" to "/usr/local/lib"
Configure the dynamic linker to look for shared libraries in the OpenCV installation path, and verify the installed version.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Add /usr/local/lib into cache /etc/ld.so.cache
# by modifying /etc/ld.so.conf or /etc/ld.so.conf.d/opencv.conf file
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
# Update cache /etc/ld.so.cache
sudo ldconfig
# Check opencv shared files in cache
# ldconfig -p | grep opencv
# Or using LD_LIBRARY_PATH
# echo 'export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib"' >> ~/.bashrc
# /usr/local/lib/pkgconfig/opencv
pkg-config --modversion opencv
# 3.4.16
pkg-config --modversion opencv4
# /usr/local/lib/pkgconfig/opencv4
If we want to use OpenCV in other Python env, create a symbolic link the shared file ~/opencv/build/lib/python3/cv2.cpython-39-x86_64-linux-gnu.so
to the Python bindings in our pip/ anaconda environment.
1
2
3
ln -s ~/opencv/build/lib/python3/cv2.cpython-38-x86_64-linux-gnu.so ~/anaconda3/envs/test_opencv/lib/python3.9/site-packages/cv2.so
python -c "import cv2; print(cv2.__version__)"
# 3.4.16
Note that we can use the following Python command to find the location where the cv2 package is installed:
1
python -c "import cv2; print(cv2.__file__)"
In summary, my bash script opencv.sh simplifies the installation and upgrade of OpenCV. By executing the script with the desired OpenCV version, we can build OpenCV from sources with custom configurations, enabling us to leverage the latest features and capabilities in our computer vision projects.
1
2
3
4
5
6
# Download opencv.sh
wget --no-check-certificate --content-disposition https://maiminh1996.github.io/scripts/opencv.sh -O opencv.sh
# Run this script for install opencv <version>=4.6.0
chmod +x opencv.sh
./opencv.sh <version>
Package manager
Alternatively, we can use pip or conda to manage packages:
1
2
3
conda activate test_opencv
which python
# /home/$(whoami)/anaconda3/envs/test_opencv/bin/python
1
2
pip install opencv-python==<version>
pip uninstall opencv-contrib-python==<version>
Upgrade
Activate the Python environment and ensure we have NumPy installed in the environment.
1
2
3
4
5
6
conda activate test_opencv
which python
# /home/$(whoami)/anaconda3/envs/test_opencv/bin/
python --version
# Python 3.9.17
pip install numpy
Next, upgrade to the desired version of OpenCV from OpenCV Releases. Use the following commands to download the upgrade script and execute it.
1
2
3
4
wget --no-check-certificate --content-disposition https://maiminh1996.github.io/scripts/opencv.sh -O opencv.sh
chmod +x opencv.sh
./opencv.sh <version>
Uninstall
If we built OpenCV from source and still have the build folder, run the following commands to uninstall:
1
2
cd ~/opencv/build
sudo make uninstall
If we no longer have the build directory, we can manually remove OpenCV files and configurations:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo find / \
\( -path /snap -o \
-path /proc -o \
-path /var -o \
-path /tmp -o \
-path /run \) -prune \
-o -iname "*opencv*" | grep -i opencv
sudo find / \
\( -path /snap -o \
-path /proc -o \
-path /var -o \
-path /tmp -o \
-path /run \) -prune \
-o -iname "*cv2*.so" | grep -i cv2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sudo rm -rf /usr/local/bin/*opencv*
sudo rm -rf /usr/local/include/*opencv*
sudo rm -rf /usr/local/lib/*opencv*
sudo rm -rf /usr/local/lib/cmake/*opencv*
sudo rm -rf /usr/local/lib/pkgconfig/*opencv*
sudo rm -rf /usr/local/share/licenses/*opencv*
sudo rm -rf /usr/local/share/*opencv*
sudo rm -rf /usr/local/share/*OpenCV*
sudo rm -rf /etc/ld.so.conf.d/*opencv*
sudo rm -rf /usr/bin/*opencv*
sudo rm -rf /usr/include/*opencv*
sudo rm -rf /usr/lib/*opencv*
sudo rm -rf /usr/lib/x86_64-linux-gnu/cmake/opencv*
sudo rm -rf /usr/lib/pkgconfig/*opencv*
sudo rm -rf /usr/lib/x86_64-linux-gnu/pkgconfig/*opencv*
sudo rm -rf $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")/*opencv*
sudo rm -rf $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")/*cv2*
# /home/$(whoami)/anaconda3/envs/test_opencv/lib/python3.9/site-packages/*opencv*
# /home/$(whoami)/anaconda3/envs/test_opencv/lib/python3.9/site-packages/*cv2*
# sudo find /home/$(whoami)/anaconda3/envs/test_opencv/lib/ -iname "*opencv*" -exec rm -i {} \;
# sudo find /home/$(whoami)/anaconda3/envs/test_opencv/lib/ -iname "*cv2*" -exec rm -i {} \;
Also, remove any Python-related files:
1
2
3
4
pip uninstall opencv-python
pip uninstall opencv-contrib-python
conda uninstall opencv-python
conda uninstall opencv-contrib-python
Conclusion
We have successfully installed, upgraded, and uninstalled OpenCV on your Linux system. We now have a fully functional OpenCV installation for our Python and C++ projects. Additionally, managing different versions of OpenCV is now more straightforward, allowing you to switch between versions as needed.
Note that building from sources provides control over configuration, multiple versions, and integration with custom libraries. However, it’s complex and requires maintenance. Meanwhile, using package manager (pip/ conda) offers simplicity, dependency resolution, and easy updates. But limited customization and potential version conflicts. So, choose based on our needs: build for control and flexibility, or use a package manager for convenience and quick setup.
References
- OpenCV Releases: github.com/opencv/opencv/releases
- OpenCV Contrib Tags: github.com/opencv/opencv_contrib/tags
- OpenCV 3.4.16 Installation in Linux: docs.opencv.org/3.4.16/d7/d9f/tutorial_linux_install.html
Appendix
Errors
Could not find a package configuration file provided by “OpenCV”
Could not find a package configuration file provided by “OpenCV” with any of the following names: OpenCVConfig.cmake
, opencv-config.cmake
Add the installation prefix of “OpenCV” to CMAKE_MODULE_PATH
or CMAKE_PREFIX_PATH
or set OpenCV_DIR
to a directory containing one of the above files. If “OpenCV” provides a separate development package or SDK, be sure it has been installed.
1
2
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)
set(CMAKE_PREFIX_PATH /usr/local/lib/cmake) # "path_to_cmake"