Installation of OpenCV using the Ubuntu repository

Please refer to [1].

Install OpenCV through the source

Step 1: Install build tools

sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev \
libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

Step 2: Clone OpenCV’s repositories

mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
# optional
git clone https://github.com/opencv/opencv_contrib.git

Step 3: Setup OpenCV build

cd ~/opencv_build/opencv
mkdir -p build && cd build
# remove some options according to your personal favor
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_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..

Step 4: Start a compilation

make -j 8

Step 5: Install OpenCV

# install
sudo make install
# verify if success
pkg-config --modversion opencv4

Reference