AlmaLinuxRHEL Based

How To Install OpenCV on AlmaLinux 9

Install OpenCV on AlmaLinux 9

OpenCV (Open Source Computer Vision Library) is a powerful tool widely used in computer vision and machine learning applications. It provides a comprehensive set of tools for image processing, video analysis, and machine learning. Installing OpenCV on AlmaLinux 9 can enhance your ability to develop sophisticated applications that involve image recognition, object detection, and more. This guide will walk you through the process step-by-step, ensuring that you can set up OpenCV effectively on your AlmaLinux system.

Prerequisites

Before diving into the installation process, ensure that your system meets the following prerequisites:

  • System Requirements: A machine running AlmaLinux 9 with at least 2 GB of RAM and sufficient disk space.
  • Basic Knowledge: Familiarity with Linux command-line operations is essential.
  • Sudo Privileges: Ensure you have administrative access to install packages.

Step 1: Update Your System

The first step in installing OpenCV is to ensure that your system is up to date. This ensures that you have the latest security patches and software updates.

sudo dnf update -y

This command will refresh your package manager’s cache and update all installed packages to their latest versions. It’s a good practice to perform this step before any installation.

Step 2: Install EPEL Repository

The Extra Packages for Enterprise Linux (EPEL) repository provides additional packages that are not included in the standard repositories. Installing EPEL is crucial for accessing some dependencies required by OpenCV.

sudo dnf install epel-release -y

This command installs the EPEL repository, allowing you to install additional software packages easily.

Step 3: Enable PowerTools Repository

The PowerTools repository contains development tools and libraries essential for compiling software from source. Enabling this repository is necessary for a successful OpenCV installation.

sudo dnf config-manager --set-enabled crb

This command enables the necessary repository, ensuring that you have access to all required components for building OpenCV.

Step 4: Install Required Dependencies

OpenCV requires several dependencies to function correctly. Installing these dependencies beforehand will prevent errors during the installation process.

  • Development Tools: These include compilers and libraries necessary for building software from source.
  • Python Libraries: Python bindings for OpenCV require specific Python packages.
  • Image Processing Libraries: Libraries like libjpeg, libpng, and others are essential for handling various image formats.

The following command installs all required dependencies:

sudo dnf install git gcc gcc-c++ cmake3 qt5-qtbase-devel python3 python3-devel python3-pip cmake python3-numpy gtk2-devel libpng-devel jasper-devel openexr-devel libwebp-devel libjpeg-turbo-devel libtiff-devel tbb-devel libv4l-devel eigen3-devel freeglut-devel mesa-libGL mesa-libGL-devel boost boost-thread boost-devel gstreamer1-plugins-base -y

This comprehensive command ensures that all necessary libraries and tools are installed on your system.

Step 5: Download OpenCV Source Code

The next step is to download the OpenCV source code from its official GitHub repository. This allows you to build the library from scratch, ensuring you have the latest version.

git clone https://github.com/opencv/opencv.git

This command clones the OpenCV repository into your current directory. You can navigate into the directory using:

cd opencv

Step 6: Create a Build Directory

It’s best practice to build OpenCV in a separate directory to keep your source files organized. This helps avoid clutter and makes it easier to manage builds.

mkdir ~/opencv_build && cd ~/opencv_build/opencv && mkdir build && cd build

This series of commands creates a new directory for building OpenCV and navigates into it.

Step 7: Configure OpenCV with CMake

CMake is a powerful tool that helps configure the build process of software projects. It generates makefiles based on specified options, which are then used to compile the software.

The following command configures OpenCV with various options:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_GENERATE_PKGCONFIG=ON -D BUILD_EXAMPLES=ON ..
  • CMAKE_BUILD_TYPE=RELEASE: This option specifies that you want a release build optimized for performance.
  • CMAKE_INSTALL_PREFIX=/usr/local: This defines where OpenCV will be installed on your system.
  • INSTALL_C_EXAMPLES: Enables installation of example programs written in C.
  • INSTALL_PYTHON_EXAMPLES: Enables installation of example programs written in Python.
  • OPENCV_GENERATE_PKGCONFIG: Generates pkg-config files for easier library management.
  • BUILD_EXAMPLES: Compiles example applications provided by OpenCV.

If there are any missing dependencies or errors during this step, CMake will provide feedback in the terminal. Make sure to resolve any issues before proceeding to the next step.

Step 8: Compile OpenCV

The compilation process converts the source code into executable binaries. Depending on your system’s hardware specifications, this process may take some time. To utilize all available CPU cores during compilation, use the following command:

make -j$(nproc)

This command tells `make` to use all available cores (as determined by `nproc`) for faster compilation. Monitor the output for any errors; if any occur, they may indicate missing dependencies or configuration issues that need addressing before proceeding.

Step 9: Install OpenCV

Once compilation is complete without errors, you can install OpenCV on your system using the following command:

sudo make install

This command copies the compiled binaries and libraries to their designated locations as specified during configuration. After this step, OpenCV should be installed on your system and ready for use!

Step 10: Verify Installation

The final step is to verify that OpenCV has been installed correctly. You can do this by checking its version using pkg-config or by importing it in Python:

pkg-config --modversion opencv4
python3 -c "import cv2; print(cv2.__version__)"

If both commands return version numbers without errors, congratulations! You have successfully installed OpenCV on AlmaLinux 9. If there are any issues, double-check previous steps for potential misconfigurations or missing dependencies.

Troubleshooting Tips

  • If CMake fails with “Could not find …” errors, ensure all dependencies were installed correctly in Step 4.
  • If compilation fails due to memory issues, consider closing other applications or increasing swap space temporarily.
  • If Python cannot import cv2 after installation, ensure that Python was correctly configured with CMake (check Step 7).
  • If pkg-config cannot find opencv4, ensure that pkg-config paths are set correctly or try reinstalling pkg-config tools.

Congratulations! You have successfully installed OpenCV. Thanks for using this tutorial for installing the OpenCV (Open Source Computer Vision Library) on AlmaLinux 9 system. For additional help or useful information, we recommend you check the official OpenCV website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button