Ubuntu 18.04: How to Install & Configure Opencv 4.2.0

This paper mainly introduces how to install and configure opencv from the source code in Ubuntu 18.04, and uses a simple example to verify whether the installation is successful

For specific installation and configuration steps, please refer to the following article: https://cv-tricks.com/installation/opencv-4-1-ubuntu18-04/

Different from the tutorial provided in the above link:

Some dependent package installations may need to be modified

In this paper, opencv version 4.2 is installed and configured to support CUDA to accelerate the calculation of DNN module

This article does not deal with Python interface configuration, only for C + +, so steps 2 ~ 5 will be skipped

Step 1: install opencv dependency package

Step by step, install all the following dependent packages:

sudo apt-get update -y # Update the list of packages
sudo apt-get remove -y x264 libx264-dev # Remove the older version of libx264-dev and x264
sudo apt-get install -y build-essential checkinstall cmake pkg-config yasm
sudo apt-get install -y git gfortran
sudo add-apt-repository -y "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get install -y libjpeg8-dev libjasper-dev libpng12-dev
sudo apt-get install -y libtiff5-dev
sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install -y libxine2-dev libv4l-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y qt5-default libgtk2.0-dev libtbb-dev
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install -y libvorbis-dev libxvidcore-dev
sudo apt-get install -y libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install -y x264 v4l-utils
# Some Optional Dependencies
sudo apt-get install -y libprotobuf-dev protobuf-compiler
sudo apt-get install -y libgoogle-glog-dev libgflags-dev
sudo apt-get install -y libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

In the process of installing the above dependent package, there may be some errors in . Here I will list the problems I encountered and give the solutions error 1:

E: Unable to locate package libjasper-dev


sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt-get update

Just install the dependency package again

error 2:

E: Unable to locate package libgstreamer0.10-dev


sudo apt install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev

Step 2: Install OpenCV 4.2.0 & OpenCV Contrib 4.2.0

OpenCV 4.2.0:

OpenCV Contib 4.2.0:

If you save the two compressed packages to /home/user name/opencv4.2/, unzip them

In addition, you need ippicv when compiling_ 2019_ lnx_ intel64_ general_ 20180723. Tgz this file is very slow to download. Here is a link to download and configure by referring to steps 1 and 2

For example, I put the downloaded file in the opencv4.2 folder and changed it to "/ home/username/opencv4.2/

Finally, the directory structure is as follows:


Step 3: building libraries with cmake


cd /home/username/opencv4.2/opencv-4.2.0
mkdir build
cd build


      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D CUDA_ARCH_BIN='7.5'
      -D WITH_TBB=ON \
      -D WITH_V4L=ON \
      -D WITH_QT=ON \
      -D OPENCV_EXTRA_MODULES_PATH=/home/username/opencv4.2/opencv_contrib-4.2.0/modules \

There are two aspects that need to be modified in the above steps


Because opencv 4.2 supports CUDA to accelerate the calculation of DNN module, CUDA is configured here; Before that, you need to configure the driver and CUDA of NVIDIA graphics card

Among them, 7.5 refers to the computing power of the graphics card, my GTX 1660ti, and the corresponding computing power is 7.5

Here is a link for reference: nvida CUDA graphics card computing capacity table

The second place that needs to be modified is:


This is modified to your own OpenCV_ Location of contrib-4.2.0/modules

Step 4: building libraries with make

To view the number of CPU cores:


For example, my CPU core number is 12, and the execution time is 12

cd /home/username/opencv4.2/build
make -j12

Wait for a while, and then configuration done appears


sudo make install

After waiting for a while again, execute:

sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

Step 5: modify opencv4. PC file

If the above configuration is successful, a pkgconfig folder will appear in the /usr/local/lib/ folder, in which there is a OpenCV. PC file. The contents are as follows:

# Package Information for pkg-config


Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.2.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cudabgsegm -lopencv_cudafeatures2d -lopencv_cudaobjdetect -lopencv_cudastereo -lopencv_cvv -lopencv_dnn_objdetect -lopencv_dnn_superres -lopencv_dpm -lopencv_highgui -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_quality -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_superres -lopencv_cudacodec -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_dnn -lopencv_plot -lopencv_videostab -lopencv_cudaoptflow -lopencv_optflow -lopencv_cudalegacy -lopencv_videoio -lopencv_cudawarping -lopencv_xfeatures2d -lopencv_shape -lopencv_ml -lopencv_ximgproc -lopencv_video -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_cudaimgproc -lopencv_cudafilters -lopencv_imgproc -lopencv_cudaarithm -lopencv_core -lopencv_cudev
Libs.private: -lm -lpthread -L/usr/lib/x86_64-linux-gnu -lGL -lGLU -lcudart_static -ldl -lrt -lnppc -lnppial -lnppicc -lnppicom -lnppidei -lnppif -lnppig -lnppim -lnppist -lnppisu -lnppitc -lnpps -lcublas -lcudnn -lcufft -L-L/usr/local/cuda -llib64 -L-L/usr/lib -lx86_64-linux-gnu
Cflags: -I${includedir_old} -I${includedir_new}

note: the sixth line needs to be modified to:


If it is not generated automatically, you can try to create a new file, copy the above content into it, and continue to the next step

Step 6: add PKG to . Bashrc file_ CONFIG_ PATH


sudo gedit ~/.bashrc

Add at the end of the file:


After exiting, execute

source ~/.bashrc

# Determine when the path is added successfully and return :/usr/local/lib/pkgconfig

Step 7: verification with C + + code

Create a test. CPP file in any directory and add the following content:

#include "opencv.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
  cout << "OpenCV version : " << CV_VERSION << endl;
  cout << "Major version : " << CV_MAJOR_VERSION << endl;
  cout << "Minor version : " << CV_MINOR_VERSION << endl;
  cout << "Subminor version : " << CV_SUBMINOR_VERSION << endl;

Use the command line to execute under its folder:

# Compile the test.cpp program and generate the executable file
g++ -std=c++11 test.cpp `pkg-config --libs --cflags opencv4` -o result

# Execute executable files

The output of the following indicates that the configuration is successful

OpenCV version : 4.2.0
Major version : 4
Minor version : 2
Subminor version : 0

Similar Posts: