FedoraRHEL Based

How To Install OpenCV on Fedora 40

Install OpenCV on Fedora 40

In this tutorial, we will show you how to install OpenCV on Fedora 40. OpenCV, or Open Source Computer Vision Library, is a pivotal tool in the realms of computer vision and machine learning. It provides a vast array of functionalities that are crucial for developing applications related to image and video analysis. With the release of Fedora 40, enthusiasts and professionals alike have a robust platform for deploying advanced vision-based applications.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step installation of the OpenCV on Fedora 40.

Prerequisites

Before we dive into the installation process, ensure that you have the following prerequisites in place:

  • A server running one of the following operating systems: Fedora 40.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • You will need access to the terminal to execute commands. Fedora provides the Terminal application for this purpose. It can be found in your Applications menu.
  • A stable internet connection to download the necessary packages.
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install OpenCV on Fedora 40

Step 1. Update the System.

To begin, it’s crucial to update your Fedora 40 system to ensure that all existing packages are up to date. This step helps prevent potential compatibility issues and ensures that you have the latest security patches and bug fixes installed. To update your system, open a terminal and run the following command:

sudo dnf clean all
sudo dnf update

The package manager will retrieve the latest package information and prompt you to confirm the update. Press “y” and hit Enter to proceed with the update process. Depending on the number of updates available, this step may take a few minutes to complete.

Step 2. Installing Required Dependencies.

OpenCV relies on various dependencies that need to be installed beforehand. These include development tools and libraries essential for building software from source. Execute the following commands to install the necessary dependencies:

sudo dnf groupinstall "Development Tools"
sudo dnf install cmake gcc gcc-c++ git libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel

Each package plays a specific role, such as handling different image formats and providing the necessary tools for compiling the OpenCV source code.

Step 3. Installing OpenCV on Fedora 40.

With the dependencies installed, we can now proceed to download the OpenCV source code. The official OpenCV repository is hosted on GitHub, and we will clone it using the git command. Run the following commands in your terminal:

cd ~
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

These commands will create two directories in your home folder: “opencv” and “opencv_contrib“. The “opencv” directory contains the main OpenCV source code, while the “opencv_contrib” directory contains additional modules and features that are not part of the core library.

By default, the git clone command will fetch the latest version of OpenCV from the repository. If you need a specific version of OpenCV for your project, you can navigate to the respective directory and check out the desired version using the git checkout command followed by the version tag.

Now that we have the OpenCV source code, we can proceed to build it from source on Fedora 40. This process involves configuring the build options and compiling the library. Follow these steps:

cd ~/opencv
mkdir build
cd build

Configure the build using cmake:

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 OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
      -D BUILD_EXAMPLES=ON ..

Compile OpenCV using make:

make -j$(nproc)

Once the build process completes successfully, we can install OpenCV on our Fedora 40 system. Run the following command:

sudo make install

This command will install OpenCV to the “/usr/local” directory, as specified during the cmake configuration step. It will copy the necessary libraries, header files, and other resources to the appropriate locations.

To verify that OpenCV is installed correctly, you can run the following command:

pkg-config --modversion opencv4

Step 4. Configuring Environment Variables.

To ensure that your system can find the OpenCV libraries and include files, you need to configure the necessary environment variables. Open the “/etc/ld.so.conf.d/opencv.conf” file using a text editor with sudo privileges:

sudo nano /etc/ld.so.conf.d/opencv.conf

Add the following line to the file:

/usr/local/lib

Save the file and exit the text editor. Then, run the following command to update the dynamic linker cache:

sudo ldconfig

Next, open the “/etc/bash.bashrc” file using a text editor with sudo privileges:

sudo nano /etc/bash.bashrc

Add the following lines at the end of the file:

export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

Save the file and exit the text editor. These environment variables ensure that your system can locate the OpenCV libraries and pkg-config files.

Step 5. Testing the Installation.

To verify that OpenCV is installed correctly and functioning as expected, let’s create a simple Python script that utilizes OpenCV. Open a new file named “test_opencv.py” using a text editor:

nano test_opencv.py

Add the following code to the file:

import cv2

print("OpenCV version:", cv2.__version__)

image = cv2.imread("image.jpg")
cv2.imshow("Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

Save the file and exit the text editor. This script imports the OpenCV library, prints the version number, loads an image file named “image.jpg”, and displays it in a window.

To run the script, make sure you have an image file named “image.jpg” in the same directory as the script. Then, execute the following command:

python test_opencv.py

If OpenCV is installed correctly, you should see the version number printed in the terminal, and a window displaying the loaded image should appear. Press any key to close the window.

Congratulations! You have successfully installed OpenCV. Thanks for using this tutorial for installing the OpenCV on Fedora 40. system. For additional help or useful information, we recommend you check the 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 a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button