OpenCV 4.x build instructions on Ubuntu 24.04
Installation is more or less described in the docs, but I had some problems, noting the working steps here:
# need libgtk2.0-dev pkg-config for OpenCV GUI
sudo apt update && sudo apt install -y cmake ninja-build g++ wget unzip libgtk2.0-dev pkg-config
mkdir BuildOpenCV && cd BuildOpenCV
# Download and unpack sources
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.x.zip
unzip opencv.zip
unzip opencv_contrib.zip
# Create build directory and switch into it
mkdir -p build && cd build
# Configure
cmake -GNinja -DCMAKE_INSTALL_PREFIX=$HOME/.local -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv-4.x
# Build
cmake --build .
# Install
sudo cmake --build . --target install
Now to let Cmake see it, set variable CMAKE_PREFIX_PATH=$HOME/.local/lib/cmake/.
I also had a warning:
Gtk-Message: 23:52:07.251: Failed to load module "canberra-gtk-module"
Fixed with:
sudo apt install libcanberra-gtk-module