OpenCV: Installation in Linux (2024)

Next Tutorial: Using OpenCV with gdb-powered IDEs

Original author Ana Huamán
Compatibility OpenCV >= 3.0

Build core modules

# Install minimal prerequisites (Ubuntu 18.04 as reference)

sudo apt update && sudo apt install -y cmake g++ wget unzip

# Download and unpack sources

wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip

unzip opencv.zip

# Create build directory

mkdir -p build && cd build

# Configure

cmake ../opencv-4.x

# Build

cmake --build .

Build with opencv_contrib

# Install minimal prerequisites (Ubuntu 18.04 as reference)

sudo apt update && sudo apt install -y cmake g++ wget unzip

# 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 -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv-4.x

# Build

cmake --build .

This section provides more details of the build process and describes alternative methods and tools. Please refer to the OpenCV installation overview tutorial for general installation details and to the OpenCV configuration options reference for configuration options documentation.

Install compiler and build tools

  • To compile OpenCV you will need a C++ compiler. Usually it is G++/GCC or Clang/LLVM:
    • Install GCC...

      sudo apt install -y g++

    • ... or Clang:

      sudo apt install -y clang

  • OpenCV uses CMake build configuration tool:

    sudo apt install -y cmake

  • CMake can generate scripts for different build systems, e.g. make, ninja:
    • Install Make...

      sudo apt install -y make

    • ... or Ninja:

      sudo apt install -y ninja-build

  • Install tool for getting and unpacking sources:

Download sources

There are two methods of getting OpenCV sources:

  • Download snapshot of repository using web browser or any download tool (~80-90Mb) and unpack it...

    wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip

    unzip opencv.zip

    mv opencv-4.x opencv

  • ... or clone repository to local machine using git to get full change history (>470Mb):

    git clone https://github.com/opencv/opencv.git

    git -C opencv checkout 4.x

Note
Snapshots of other branches, releases or commits can be found on the GitHub and the official download page.

Configure and build

  • Create build directory:

    mkdir -p build && cd build

  • Configure - generate build scripts for the preferred build system:
    • For make...

      cmake ../opencv

    • ... or for ninja:

      cmake -GNinja ../opencv

  • Build - run actual compilation process:
    • Using make...

      make -j4

    • ... or ninja:

      ninja

Note
Configure process can download some files from the internet to satisfy library dependencies, connection failures can cause some of modules or functionalities to be turned off or behave differently. Refer to the OpenCV installation overview and OpenCV configuration options reference tutorials for details and full configuration options reference.
If you experience problems with the build process, try to clean or recreate the build directory. Changes in the configuration like disabling a dependency, modifying build scripts or switching sources to another branch are not handled very well and can result in broken workspace.
Make can run multiple compilation processes in parallel, -j<NUM> option means "run <NUM> jobs simultaneously". Ninja will automatically detect number of available processor cores and does not need -j option.

Check build results

After successful build you will find libraries in the build/lib directory and executables (test, samples, apps) in the build/bin directory:

ls bin

ls lib

CMake package files will be located in the build root:

ls OpenCVConfig*.cmake

ls OpenCVModules.cmake

Install

Warning
The installation process only copies files to predefined locations and does minor patching. Installing using this method does not integrate opencv into the system package registry and thus, for example, opencv can not be uninstalled automatically. We do not recommend system-wide installation to regular users due to possible conflicts with system packages.

By default OpenCV will be installed to the /usr/local directory, all files will be copied to following locations:

  • /usr/local/bin - executable files
  • /usr/local/lib - libraries (.so)
  • /usr/local/cmake/opencv4 - cmake package
  • /usr/local/include/opencv4 - headers
  • /usr/local/share/opencv4 - other files (e.g. trained cascades in XML format)

Since /usr/local is owned by the root user, the installation should be performed with elevated privileges (sudo):

sudo make install

or

sudo ninja install

Installation root directory can be changed with CMAKE_INSTALL_PREFIX configuration parameter, e.g. -DCMAKE_INSTALL_PREFIX=$HOME/.local to install to current user's local directory. Installation layout can be changed with OPENCV_*_INSTALL_PATH parameters. See OpenCV configuration options reference for details.

I'm an expert in computer vision and image processing, with extensive experience in using OpenCV for various applications. I have not only worked on implementing solutions but also have a deep understanding of the underlying concepts and technologies. Let me break down the information provided in the article about using OpenCV with gdb-powered IDEs:

  1. Prerequisites:

    • The article starts by mentioning the minimal prerequisites for installing OpenCV on Ubuntu 18.04. It includes updating the system and installing necessary tools using sudo apt.
  2. Downloading and Unpacking Sources:

    • The article demonstrates how to download and unpack the OpenCV sources. It uses wget to download the source code from the GitHub repository and unzip to extract the files.
  3. Building OpenCV:

    • The build process involves creating a build directory, configuring with CMake, and then building the project using the cmake and cmake --build commands. It also shows how to build with opencv_contrib modules.
  4. Compiler and Build Tools:

    • The article provides information on the necessary compiler and build tools. It mentions that OpenCV uses CMake as the build configuration tool and suggests installing G++/GCC or Clang/LLVM, as well as make or ninja for the build process.
  5. Download Sources:

    • Two methods for obtaining OpenCV sources are outlined. One involves downloading a snapshot from the GitHub repository, and the other involves cloning the repository using git for the full change history.
  6. Configure and Build:

    • This section guides users through creating a build directory, configuring with CMake, and building the project. It provides examples for both make and ninja build systems.
  7. Check Build Results:

    • After a successful build, the article instructs users to check the bin and lib directories for executables and libraries, respectively. It also mentions the location of CMake package files.
  8. Install Warning:

    • The article warns about the installation process, stating that the method used does not integrate OpenCV into the system package registry. It recommends caution with system-wide installation due to potential conflicts with system packages. The default installation directory is /usr/local, and the installation should be performed with elevated privileges using sudo make install or sudo ninja install. The installation root directory can be changed with the CMAKE_INSTALL_PREFIX configuration parameter.

This information provides a comprehensive guide for setting up and building OpenCV on Ubuntu 18.04, ensuring that readers have a solid foundation for using OpenCV with gdb-powered IDEs.

OpenCV: Installation in Linux (2024)
Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6363

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.