How to Install OpenCV on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9
Install OpenCV on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 from PyPI, from EPEL, or by building the current release from source.
OpenCV is the standard open source library for computer vision: image and video processing, feature detection, object tracking and neural-network inference, with C++ and Python interfaces. This guide shows three ways to install it on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9: a prebuilt Python wheel, the EPEL package, and a build of the current release from source. CentOS 7 reached end of life on 30 June 2024 and no longer receives updates; the RHEL 9 family replaces it, and the commands below use dnf instead of yum.
Choose an installation method
| Method | What you get | Use it when |
|---|---|---|
| Python wheel from PyPI | A current OpenCV for Python only, no compiler needed | You write Python and want the quickest route |
| EPEL package | OpenCV 4.6.0 with C++ headers, updated through dnf | You want distribution packages and do not need the newest version |
| Build from source | The current release (5.1.0 at the time of writing), C++ and Python, with the options you choose | You need the newest version, the contrib modules or custom build options |
The OpenCV project maintains two release lines: 5.x, and 4.x for existing code that has not been ported. Use only one installation method per environment; mixing them leads to the wrong library being loaded.
Option 1: Install the Python wheel
The OpenCV project publishes prebuilt wheels on PyPI. On a server, use the headless variant: it has no GUI dependencies and is the one recommended when you do not call cv2.imshow. Install it into a virtual environment so it does not interfere with system Python packages:
sudo dnf install python3 python3-pip
python3 -m venv ~/opencv-env
source ~/opencv-env/bin/activate
pip install --upgrade pip
pip install opencv-python-headless
There are four wheel packages. Install exactly one of them in an environment:
opencv-python– main modules, with GUI support for desktopsopencv-contrib-python– main and contrib modules, with GUI supportopencv-python-headless– main modules, for servers and containersopencv-contrib-python-headless– main and contrib modules, for servers and containers
Check the result:
python -c "import cv2; print(cv2.__version__)"
Option 2: Install the EPEL package
OpenCV is not in the base repositories of the RHEL 9 family, but EPEL carries it. EPEL packages depend on the CRB repository, so enable both:
sudo dnf install epel-release
sudo dnf config-manager --set-enabled crb
sudo dnf install opencv opencv-devel
On AlmaLinux and Rocky Linux, dnf config-manager is provided by the dnf-plugins-core package; install it first if the command is missing. EPEL 9 ships OpenCV 4.6.0. Confirm what was installed:
rpm -q opencv opencv-devel
Option 3: Build OpenCV from source
Compiling takes a while and needs memory. Plan for at least 4 GB of RAM, or add swap space, and several gigabytes of free disk for the build directory.
Install the build tools
You need a C++ compiler, CMake 3.13 or newer, make and tools to fetch the sources. The Python development files and NumPy are needed only if you want the Python bindings.
sudo dnf install gcc gcc-c++ make cmake wget unzip python3-devel python3-numpy
Download the sources
Fetch the source archive of the release you want from the project's GitHub repository. Check the releases page for the current version number and substitute it below.
mkdir ~/opencv-build && cd ~/opencv-build
wget -O opencv.zip https://github.com/opencv/opencv/archive/refs/tags/5.1.0.zip
unzip opencv.zip
The contrib repository holds extra and experimental modules. It is optional; if you want it, download the archive with the same version number as the main sources:
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/refs/tags/5.1.0.zip
unzip opencv_contrib.zip
Configure
Build in a separate directory. Without the contrib modules:
mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ../opencv-5.1.0
With the contrib modules, point CMake at their modules directory instead:
mkdir -p build && cd build
cmake -DCMAKE_BUILD_TYPE=Release -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-5.1.0/modules ../opencv-5.1.0
CMake prints a summary at the end. Read it before you build: it lists the modules that will be compiled, the detected image and video libraries and, under the Python 3 heading, whether the interpreter and NumPy were found. During configuration CMake may download some third-party components; if that fails, the affected features are disabled. If you change options or something goes wrong, delete the build directory and configure again rather than reusing it.
Useful options to add to the cmake command:
-DCMAKE_INSTALL_PREFIX=$HOME/.local– install into your home directory instead of/usr/local-DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF– skip the test programs to shorten the build-DOPENCV_GENERATE_PKGCONFIG=ON– also write a.pcfile forpkg-config; the project marks this as deprecated in favour of CMake'sfind_package(OpenCV)
Compile and install
Use one job per CPU core. If the compiler is killed for lack of memory, lower the number.
make -j$(nproc)
When the build finishes, the libraries are in build/lib and the programs in build/bin. Install into the default prefix, /usr/local:
sudo make install
The headers go to /usr/local/include/opencv4 or the corresponding directory for the version you built, and the CMake package files go under the library directory. On the RHEL family, /usr/local library directories are not in the dynamic linker's search path by default. Find out where the libraries were installed, register that directory and refresh the cache:
ls -d /usr/local/lib64 /usr/local/lib
echo /usr/local/lib64 | sudo tee /etc/ld.so.conf.d/opencv.conf
sudo ldconfig
Use /usr/local/lib in the second command if that is where the libopencv_*.so files are.
Verify the build
Check the installed libraries and, if you built the Python bindings, import the module:
ldconfig -p | grep libopencv_core
python3 -c "import cv2; print(cv2.__version__)"
To test the C++ side, create test.cpp:
#include <opencv2/core.hpp>
#include <iostream>
int main() {
std::cout << "OpenCV " << CV_VERSION << std::endl;
return 0;
}
And a CMakeLists.txt next to it:
cmake_minimum_required(VERSION 3.13)
project(cvtest CXX)
find_package(OpenCV REQUIRED)
add_executable(cvtest test.cpp)
target_link_libraries(cvtest ${OpenCV_LIBS})
Build and run it:
cmake -B out .
cmake --build out
./out/cvtest
Updating and removing
The wheel is updated with pip install --upgrade opencv-python-headless inside the virtual environment, and the EPEL package with sudo dnf upgrade. A source install is not registered with the package manager and cannot be uninstalled automatically, which is why the OpenCV documentation advises against system-wide installation for ordinary users. Keep the build directory: it contains install_manifest.txt, the list of every file that make install copied, which you need to remove them later. To move to a newer release, download the new sources, build them in a fresh directory and install again.
Frequently asked questions
Should I use OpenCV 5 or OpenCV 4?
For new projects use the current 5.x release. The 4.x line still receives releases and is the safer choice when you maintain existing code or depend on a library that has not been ported yet. To build it, use a 4.x version number in the download URLs; the build steps are the same.
Why does import cv2 fail with a libGL error?
That happens with the non-headless wheels on a server without graphics libraries. Uninstall opencv-python and install opencv-python-headless instead; do not keep both in the same environment.
Can I read video files with these builds?
The PyPI wheels bundle their own video libraries. A source build only supports the video backends whose development packages were present at configure time; check the Video I/O section of the CMake summary, install the missing -devel packages and configure again in a clean build directory.
Does this work on AlmaLinux or Rocky Linux 10?
The wheel and source methods work the same way. EPEL 10 carries a newer packaged version, OpenCV 4.10.0, than EPEL 9.