Set up OpenCV on Rocky Linux 8 with 2 Easy Methods – OrcaCore
In this guide, we’ll show you how to Set up OpenCV on Rocky Linux 8 From Rocky Linux Repository and Source. OpenCV is an open-source library incredibly valuable for computer vision applications like video analysis, CCTV footage analysis, and image analysis. Written in C++, OpenCV boasts over 2,500 optimized algorithms. When developing computer vision applications, leveraging this library allows us to focus on solving real-world problems instead of building everything from scratch.
You can now proceed to the following steps on the Orcacore website to finish the OpenCV setup on Rocky Linux 8.
To complete this guide, log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide on Initial Server Setup with Rocky Linux 8.
Method 1. Install OpenCV from the Rocky Linux Repository
Installing OpenCV from the Rocky Linux 8 repository is straightforward.
First, update your local package index with the command below:
sudo dnf update
Next, install the EPEL (Extra Packages for Enterprise Linux) repository on your server:
sudo dnf install epel-release -y
Enable PowerTools by using the command below:
sudo dnf config-manager --set-enabled powertools
Then, use the command below to install OpenCV:
sudo dnf install opencv opencv-devel -y
This command will install all packages necessary to run OpenCV.
You can verify your installation with the following command:
pkg-config --modversion opencv
**Output**
3.4.6
Method 2. Build OpenCV from Source on Rocky Linux 8
Building OpenCV from source is often recommended. It optimizes the library for your specific system and gives you complete control over the build options. This method ensures you have the latest features and can tailor OpenCV to your exact needs. Let’s delve into this way to Set up OpenCV on Rocky Linux 8.
To install the latest and stable version of OpenCV from the source, follow the steps below.
First, update your local package index with the following command:
sudo dnf update
Then, you need to install the dependencies with the following command:
sudo dnf 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 directory for OpenCV on Rocky Linux 8 and switch to it with the following command:
# sudo mkdir ~/opencv_build
# sudo cd ~/opencv_build
Clone OpenCV Repositories
Next, you need to clone the OpenCV and OpenCV contrib repositories with the following commands:
# sudo git clone https://github.com/opencv/opencv.git
# sudo git clone https://github.com/opencv/opencv_contrib.git
When your download is completed, create a temporary build directory and switch to it with the following command:
# sudo cd ~/opencv_build/opencv
# sudo mkdir build
# sudo cd build
Compile and Build OpenCV from source
Here you need to set up OpenCV build with CMake on Rocky Linux 8:
sudo cmake ../
In your output you will see:
Now start the compilation process:
make -j2
Note: Modify the -j flag according to your processor. If you do not know the number of cores in your processor, you can find it by typing nproc
.
This will take some minutes or more to complete.
In your output you will see:
Now you can install OpenCV with the following command:
sudo make install
Finally, create a symlink file opencv4.pc to the directory /usr/share/pkgconfig
and run ldconfig
to rebuild the library’s cache:
# sudo ln -s /usr/local/lib64/pkgconfig/opencv4.pc /usr/share/pkgconfig/
#sudo ldconfig
To enable Python modules cv2 run the following command below:
python3 -c "import cv2; print(cv2.__version__)"
**Output**
4.7.0-dev
That’s it you have successfully installed OpenCV on Rocky Linux 8.
Conclusion
At this point, you have learned to install OpenCV from the source, and also you can install it from the Rocky Linux repository. This gives you flexibility in choosing the installation method that best suits your needs and expertise.
Hope you enjoy it. You may also be interested in these articles:
Install and Configure OpenNMS on Rocky Linux 8
Set up Python 3.11 on Rocky Linux 8
Alternative Methods to Install OpenCV on Rocky Linux 8
While the methods described above are reliable, there are alternative approaches to installing OpenCV on Rocky Linux 8. Here are two alternative ways to consider:
1. Using Anaconda Environment
Anaconda is a popular open-source distribution of Python and R for scientific computing and data science. It simplifies package management and deployment. Using Anaconda allows you to create isolated environments for your projects, avoiding dependency conflicts.
Steps:
-
Install Anaconda: If you don’t have Anaconda installed, download it from the official Anaconda website and follow the installation instructions for Linux.
-
Create a New Environment: Create a new Anaconda environment specifically for your OpenCV project:
conda create -n opencv_env python=3.8 # Or your desired Python version conda activate opencv_env
-
Install OpenCV: Install OpenCV using conda:
conda install -c conda-forge opencv
-
Verify Installation: Verify the installation by running Python and importing the
cv2
module:python import cv2 print(cv2.__version__)
Explanation:
Anaconda manages dependencies effectively, ensuring a clean and isolated environment for your OpenCV projects. The conda-forge
channel is a community-driven repository providing up-to-date packages, including OpenCV. This method is especially beneficial when working on multiple projects with different dependency requirements. The Anaconda approach is another way to Set up OpenCV on Rocky Linux 8.
2. Using a Docker Container
Docker provides a containerization platform that allows you to package an application and its dependencies into a standardized unit for software development. Using Docker to install OpenCV ensures consistency across different environments and simplifies deployment.
Steps:
-
Install Docker: If you don’t have Docker installed, follow the instructions on the official Docker website for installing Docker Engine on Rocky Linux.
-
Create a Dockerfile: Create a Dockerfile with instructions to install OpenCV. Here’s an example:
FROM rockylinux:8 RUN dnf update -y && dnf install -y epel-release && dnf install -y python3 python3-pip opencv-python3 WORKDIR /app COPY . /app CMD ["python3", "your_script.py"] # Replace with your script
-
Build the Docker Image: Build the Docker image using the Dockerfile:
docker build -t opencv-rocky .
-
Run the Docker Container: Run the Docker container:
docker run -it --name opencv-container opencv-rocky
Explanation:
The Dockerfile starts from a Rocky Linux 8 base image. It updates the package repositories, installs Python, pip, and the opencv-python3
package. By containerizing the application, you ensure that it runs consistently regardless of the host environment. This is particularly useful in production deployments where environment consistency is critical. Here is a simple Python code example:
# your_script.py
import cv2
# Load an image
img = cv2.imread('image.jpg')
# Check if image loaded successfully
if img is not None:
# Convert to grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Display the grayscale image
cv2.imshow('Grayscale Image', gray_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
else:
print("Could not open or find the image")
To run this script within the docker container, you would copy both the your_script.py
and image.jpg
files to the /app
directory (specified by WORKDIR
in the Dockerfile) before building the image. This ensures the script and image are available within the container’s file system. Using a Docker container is a very useful way to Set up OpenCV on Rocky Linux 8.