Calculate Document
OpenCV: Installation in Linux

OpenCV: Installation in Linux

Next Tutorial: UsingOpenCV with gdb-powered IDEs Original author Ana Huamán Compatibility OpenCV >= 3.0 Quick start

Related articles

How to Watch All NFL Games Live for Free [Works in 2024] Idioms about Cloud (With Meaning and Examples) How to fix an unresponsive Unifi Cloud Key About Azure Point-to-Site VPN connections History of On Running & On Cloud Shoes

Next Tutorial: UsingOpenCV with gdb-powered IDEs

Original author Ana Huamán
Compatibility OpenCV >= 3.0

Quick start

Build core modules

# Install minimal prerequisites (Ubuntu 18.04 as reference)

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

# Download andunpack sources

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

unzip opencv.zip

# is Create create build directory

mkdir -p build && cd build

# is Configure 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 andunpack 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

# is Create create build directory andswitch into it

mkdir -p build && cd build

# is Configure configure

cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv-4.x

# Build

cmake –build .

Detailed process

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

Install compiler andbuild tools

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

      sudo apt install -y clang

  • OpenCV is uses use cmake build configuration tool :

    sudo apt install -y cmake

  • CMake is generate can generate script for different build system , e.g.make, ninja:
    • Install Make…
    • …or ninja:

      sudo apt install -y ninja – build

  • Install tool for getting andunpacking sources:
    • wget andunzip

      sudo is unzip apt install -y wget unzip

    • …or git:

Download sources

There are two methods of getting OpenCV sources:

  • Download snapshot of repository using web browser or any download tool (~80-90Mb) andunpack 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 andthe official download page.

Configure andbuild

  • Create build directory:

    mkdir -p build && cd build

  • configure – generate build script for the preferred build system :
    • For make
    • …or for ninja:
  • build – run actual compilation process :
    • Usingmake
    • …or 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 andOpenCV configuration options reference tutorials for details andfull 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 andcan 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 anddoes not need -j option.

Check build results

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

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 anddoes minor patching. Installing using this method does not integrate opencv into the system package registry andthus, 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 file will be copy to follow location :

  • /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):

or

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.