Easy Methods To Install OpenCV on AlmaLinux 8 – OrcaCore

Posted on

Easy Methods To Install OpenCV on AlmaLinux 8 - OrcaCore

Easy Methods To Install OpenCV on AlmaLinux 8 – OrcaCore

In this comprehensive guide, you will learn the different ways to Install OpenCV on AlmaLinux 8, specifically by leveraging the DNF package manager and building from source. OpenCV stands as a powerful open-source library, enabling a wide array of computer vision tasks, including face detection, object tracking, landmark detection, and more. Its versatility extends to supporting multiple programming languages like Python, Java, and C++. Follow the detailed steps provided in this guide to successfully Install OpenCV on AlmaLinux 8.

To begin, ensure you have access to your AlmaLinux 8 server as a non-root user with sudo privileges. If you haven’t already configured this, refer to our guide on Initial Server Setup with AlmaLinux 8.

Now, let’s dive into the methods for installing OpenCV on your system.

Method 1. OpenCV Installation with DNF on AlmaLinux 8

This method offers a straightforward approach to installing OpenCV directly from the AlmaLinux 8 repository.

First, update your local package index to ensure you have the latest information about available packages:

sudo dnf update -y

Next, install the EPEL (Extra Packages for Enterprise Linux) repository, which provides additional packages not included in the base AlmaLinux distribution:

sudo dnf install epel-release -y

Enable the PowerTools repository, which contains development and build tools:

sudo dnf config-manager --set-enabled powertools

Finally, install OpenCV and its development files using the following command:

sudo dnf install opencv opencv-devel -y

This command will install all the necessary packages to run OpenCV applications.

To verify the installation, you can use the pkg-config tool to check the installed version:

pkg-config --modversion opencv
**Output**
3.4.6

Method 2. OpenCV Installation From Source on AlmaLinux 8

Building from source is often the preferred method to Install OpenCV on AlmaLinux 8. This allows for greater control over the build process, optimization for your specific system, and access to the latest features and bug fixes.

First, update your local package index:

sudo dnf update -y

Required Dependencies for OpenCV on AlmaLinux 8

Install the necessary dependencies using the following command:

sudo dnf -y install epel-release git gcc gcc-c++ cmake3 qt5-qtbase-devel python3 python3-devel python3-pip cmake python3-devel 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

Now, create a dedicated directory for the OpenCV build and navigate to it:

# sudo mkdir ~/opencv_build
# sudo cd ~/opencv_build

Clone OpenCV Repositories

Clone the OpenCV and OpenCV contrib repositories from GitHub:

# sudo git clone https://github.com/opencv/opencv.git
# sudo git clone https://github.com/opencv/opencv_contrib.git

Once the download is complete, create a temporary build directory within the OpenCV directory and switch to it:

# sudo cd ~/opencv_build/opencv
# sudo mkdir build
# sudo cd build

Compile and Build OpenCV

Configure the OpenCV build using CMake:

sudo cmake ../

The output should resemble the following:

**Output**
-- Configuring done
-- Generating done
-- Build files have been written to: /root/opencv_build/opencv/build

Start the compilation process:

make -j2

Note: Adjust the -j flag to match the number of processor cores in your system. You can determine the number of cores using the nproc command.

The compilation process may take a significant amount of time to complete.

[ 99%] Linking CXX executable ../../bin/opencv_annotation
[100%] Built target opencv_annotation

After the compilation is finished, install OpenCV:

sudo make install

Finally, create a symbolic link for the opencv4.pc file to the /usr/share/pkgconfig directory and run ldconfig to update the library cache:

# sudo ln -s /usr/local/lib64/pkgconfig/opencv4.pc /usr/share/pkgconfig/
# sudo ldconfig

To verify that the Python modules are correctly installed, run the following command:

python3 -c "import cv2; print(cv2.__version__)"
**Output**
4.8.0-dev

Congratulations! You have successfully installed OpenCV on AlmaLinux 8 from source.

For further information, refer to the official OpenCV documentation at OpenCV.

Alternative Methods for Installing OpenCV on AlmaLinux 8

While the DNF package manager and building from source are common methods, here are two alternative approaches to Install OpenCV on AlmaLinux 8:

1. Using Anaconda (or Miniconda) Environment:

Anaconda is a popular open-source distribution of Python and R for scientific computing and data science. It includes a package manager called conda that simplifies the installation of libraries like OpenCV and their dependencies. This method is particularly useful if you want to isolate your OpenCV installation within a specific environment, preventing conflicts with other system-wide libraries.

Explanation:

Anaconda provides isolated environments that contain specific versions of Python and related packages. This prevents version conflicts between different projects that might require different versions of OpenCV or its dependencies. Using Anaconda, you can easily create, activate, and manage these environments.

Steps:

  • Install Anaconda or Miniconda: If you don’t have Anaconda installed, download and install it from the official Anaconda website. Miniconda is a lightweight alternative that only includes conda and its dependencies.

  • Create a new environment:

    conda create -n opencv_env python=3.8  # Replace 3.8 with your desired Python version
  • Activate the environment:

    conda activate opencv_env
  • Install OpenCV using conda:

    conda install -c conda-forge opencv
  • Verify the installation:

    python -c "import cv2; print(cv2.__version__)"

Code Example (Python):

import cv2

print(f"OpenCV version: {cv2.__version__}")

# Load an image
img = cv2.imread("image.jpg")

# Check if the image was loaded successfully
if img is None:
    print("Error: Could not load image")
else:
    # Display the image (optional)
    cv2.imshow("Image", img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

2. Using Docker:

Docker is a containerization platform that allows you to package applications and their dependencies into isolated containers. This approach ensures consistency across different environments and simplifies deployment.

Explanation:

Docker containers provide a consistent and isolated environment for running applications. By using a Docker image that includes OpenCV and its dependencies, you can avoid dependency conflicts and ensure that your application runs correctly regardless of the underlying operating system.

Steps:

  • Install Docker: Install Docker Engine on your AlmaLinux 8 system following the official Docker documentation.

  • Find or Create a Docker image: You can either use an existing Docker image from Docker Hub (e.g., one based on Ubuntu or CentOS with OpenCV pre-installed) or create your own Dockerfile to build an image with your specific requirements.

  • Run the Docker container:

    Example using a pre-built image:

    docker run -it -v $(pwd):/app  --name opencv_container  your_opencv_image /bin/bash

    Replace your_opencv_image with the name of the Docker image you choose. The -v $(pwd):/app option mounts your current directory into the container’s /app directory, allowing you to access your code and data from within the container.

  • Within the container, verify the installation:

    python -c "import cv2; print(cv2.__version__)"

Code Example (Dockerfile):

This Dockerfile example builds a basic OpenCV environment based on AlmaLinux 8.

FROM almalinux:8

# Install necessary dependencies
RUN dnf update -y && 
    dnf install -y epel-release && 
    dnf config-manager --set-enabled powertools && 
    dnf install -y python3 python3-pip opencv-python3

# Set working directory
WORKDIR /app

# Copy application code (optional)
COPY . .

# Command to run the application
CMD ["python3", "your_script.py"] # Replace with your script name

Key Considerations:

  • Environment Isolation: Both Anaconda and Docker provide excellent environment isolation, preventing dependency conflicts.
  • Reproducibility: Docker excels at ensuring reproducibility, as the Dockerfile defines the entire environment.
  • Ease of Use: Anaconda is generally easier to set up for simple Python projects, while Docker requires a bit more initial configuration but offers greater flexibility and control.
  • Resource Usage: Docker containers can be more lightweight than Anaconda environments, especially for complex applications.

Conclusion

In this guide, you’ve learned different methods to Install OpenCV on AlmaLinux 8: using the DNF package manager, building from source, leveraging Anaconda environments, and using Docker containers. Building and installing OpenCV from source often provides the latest version and optimization capabilities. However, Anaconda and Docker provide powerful ways to manage dependencies and ensure reproducibility. Choose the method that best suits your needs and project requirements to Install OpenCV on AlmaLinux 8.

Hope you enjoyed this guide. You might also find these articles helpful:

Web Browsing on AlmaLinux Terminal

Configure Rsyslog in AlmaLinux

10 Useful ncat Commands on AlmaLinux