How to install OpenCV on CentOS, Fedora, or Red Hat

OpenCV packages on Red Hat-family Linux let Python scripts import cv2 without compiling the computer-vision library from source. Installing the packaged Python bindings keeps image codecs, video support, and NumPy aligned with the repositories that DNF manages for the host.

Fedora publishes python3-opencv in its normal repositories. Red Hat Enterprise Linux and compatible systems such as CentOS Stream, Rocky Linux, and AlmaLinux commonly use EPEL for the same package, and EPEL depends on the extra CodeReady Builder or CRB repository for some libraries.

After installation, Python should report the OpenCV version and read back a small generated PNG file. Use a project virtual environment with opencv-python from PyPI instead when a project needs a newer OpenCV release than the distribution package provides.

Steps to install OpenCV with DNF:

  1. Open a terminal with sudo privileges.
  2. Install the DNF repository helper on RHEL-compatible systems.
    $ sudo dnf install dnf-plugins-core

    Fedora systems that already have the standard repositories enabled can skip the EPEL and CRB setup steps.

  3. Enable CRB on CentOS Stream, Rocky Linux, or AlmaLinux.
    $ sudo dnf config-manager --set-enabled crb

    On RHEL 9, enable CodeReady Builder instead with sudo subscription-manager repos --enable codeready-builder-for-rhel-9-$(arch)-rpms.

  4. Enable EPEL on RHEL-compatible systems.
    $ sudo dnf install epel-release
    Dependencies resolved.
    ================================================================================
     Package              Architecture   Version          Repository           Size
    ================================================================================
    Installing:
     epel-release         noarch         9-10.el9         extras               19 k
    ##### snipped #####
    Complete!

    On RHEL 9, install the EPEL release package from https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm when epel-release is not available from the enabled repositories.

  5. Install the OpenCV Python bindings.
    $ sudo dnf install python3-opencv python3-numpy
    Dependencies resolved.
    ================================================================================
     Package                  Architecture  Version              Repository    Size
    ================================================================================
    Installing:
     python3-numpy            x86_64        1:1.23.5-2.el9_7     appstream    5.3 M
     python3-opencv           x86_64        4.6.0-9.el9          epel         1.7 M
    ##### snipped #####
    Installed:
      opencv-4.6.0-9.el9.x86_64
      opencv-contrib-4.6.0-9.el9.x86_64
      opencv-core-4.6.0-9.el9.x86_64
      python3-numpy-1:1.23.5-2.el9_7.x86_64
      python3-opencv-4.6.0-9.el9.x86_64
    
    Complete!

    The exact version and architecture vary by distribution release. The transaction can be large because OpenCV pulls image, video, GUI, and math libraries.

  6. Confirm that Python can import cv2.
    $ python3 -c "print(__import__('cv2').__version__)"
    4.6.0
  7. Run a small image write/read smoke test.
    $ python3 - <<'PY'
    import cv2
    import numpy as np
    
    image = np.zeros((40, 40, 3), dtype=np.uint8)
    image[:] = (0, 128, 255)
    cv2.imwrite("opencv-smoke.png", image)
    
    loaded = cv2.imread("opencv-smoke.png")
    print("OpenCV", cv2.__version__)
    print("Image shape", loaded.shape)
    PY
    OpenCV 4.6.0
    Image shape (40, 40, 3)

    The printed shape confirms that OpenCV wrote a PNG file and read it back as a 40 by 40 pixel, three-channel image.

  8. Remove the smoke-test image.
    $ rm opencv-smoke.png