Building OpenCV 4.8 from Source on Ubuntu Systems

Downloading OpenCV 4.8 Source

Execute the following commands in a terminal to download and extract the source code:

wget -O opencv-4.8.zip https://github.com/opencv/opencv/archive/4.8.0.zip
unzip opencv-4.8.zip -d opencv-4.8

Installing Build Dependencies

Install required compliers and libraries using apt:

sudo apt update
sudo apt install -y build-essential cmake git libgtk-3-dev pkg-config

Compilation and Installasion

Navigate to the source directory and create a build folder:

cd opencv-4.8/opencv-4.8.0
mkdir build
cd build

Configure the build using CMake:

cmake -D CMAKE_BUILD_TYPE=Release ..

Compile the source code using multiple cores:

make -j$(nproc)

Install the compiled libraries system-wide:

sudo make install

Verification Test

Create a test project to verify the installation:

mkdir opencv_test
cd opencv_test

Create a CMakeLists.txt file with the following content:

cmake_minimum_required(VERSION 3.10)
project(OpenCVTest)
find_package(OpenCV REQUIRED)
add_executable(opencv_test test_opencv.cpp)
target_link_libraries(opencv_test ${OpenCV_LIBS})

Create a test source file test_opencv.cpp:

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    cv::Mat test_image = cv::imread("test_image.png");
    if(test_image.empty()) {
        std::cout << "Could not load image" << std::endl;
        return -1;
    }
    cv::namedWindow("OpenCV Test", cv::WINDOW_NORMAL);
    cv::imshow("OpenCV Test", test_image);
    cv::waitKey(0);
    return 0;
}

Build and run the test application:

cmake .
make
./opencv_test

Tags: OpenCV Ubuntu C++ Computer Vision CMake

Posted on Fri, 31 Jul 2026 16:01:36 +0000 by Rithiur