Compilation and installation of opencv in Ubuntu

Keywords: OpenCV Virtual Machine

1.1. First download opencv4.1.2 in Ubuntu (download the source code version, that is, the source version)

Since most of the downloaded virtual machines are in English, it is not very good-looking. The author suggests changing the language to simplified Chinese

Specific information can be viewed  

(25 messages) ubuntu 16.04 change the system language to simplified Chinese_ Silence CC blog - CSDN blog_ ubuntu Sinicization

1.2. After downloading the source code of OpenCV, create a new folder in the home directory. The name can be opencv randomly

Then extract the downloaded format into the opencv folder (extracted to mean decompression)

As shown in the figure below

1.3. Key step: right click opencv.4.1.2 and click open at the terminal

Installing cmake

sudo apt-get install cmake  

It prompts you to enter the password of the virtual machine, and you will find that it is pressed but not displayed

Here, the author thinks it's a Ubuntu bug, but in fact, it's still input. As long as you continue, there's no problem

After installing cmake, install the dependent libraries

sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libswscale-dev libjasper-dev

After installing the dependency library, the compilation is half done

1.4. Create a folder

mkdir my_build_dir

  1.5. Enter folder

cd my_build_dir

1.6.cmake (you can also enter cmake.) there is a space between cmake and

This step is actually creating a makefile. Without this step, the next step cannot be carried out

cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

1.7. Compilation (I have failed many times in this step. Because this step takes a long time, it is recommended to finish the game directly after inputting the code  

I spent an hour and a half making a reference)


sudo make

If 29% or 40% of the errors occur directly during compilation, I suggest restarting or starting over. (for example, the following error)

sudo make Post error:
$:sudo make
[sudo] password for :
[ 0%] Built target gen-pkgconfig
[ 3%] Built target libjpeg-turbo
[ 6%] Built target libtiff
[ 14%] Built target libwebp
[ 17%] Built target libjasper
[ 22%] Built target IlmImf
[ 24%] Built target ippiw
[ 30%] Built target libprotobuf
[ 30%] Built target ittnotify
[ 30%] Built target opencv_test_core_pch_dephelp
[ 30%] Built target opencv_core_pch_dephelp
[ 30%] Built target opencv_ts_pch_dephelp
[ 30%] Built target pch_Generate_opencv_ts
[ 30%] Built target pch_Generate_opencv_core
[ 36%] Built target opencv_core
[ 36%] Built target opencv_imgproc_pch_dephelp
[ 36%] Built target pch_Generate_opencv_imgproc
[ 41%] Built target opencv_imgproc
[ 41%] Built target opencv_imgcodecs_pch_dephelp
[ 41%] Built target pch_Generate_opencv_imgcodecs
[ 43%] Built target opencv_imgcodecs
[ 43%] Built target opencv_videoio_pch_dephelp
[ 43%] Built target pch_Generate_opencv_videoio
[ 43%] Linking CXX shared library .../.../lib/
/usr/bin/ld: cannot find -lgstbase-1.0
/usr/bin/ld: cannot find -lgstreamer-1.0
/usr/bin/ld: cannot find -lgstvideo-1.0
/usr/bin/ld: cannot find -lgstapp-1.0
/usr/bin/ld: cannot find -lgstriff-1.0
/usr/bin/ld: cannot find -lgstpbutils-1.0
/usr/bin/ld: cannot find -lavresample
collect2: error: ld returned 1 exit status
modules/videoio/CMakeFiles/opencv_videoio.dir/build.make:333: recipe for target 'lib/' failed
make[2]: *** [lib/] Error 1
CMakeFiles/Makefile2:5205: recipe for target 'modules/videoio/CMakeFiles/opencv_videoio.dir/all' failed
make[1]: *** [modules/videoio/CMakeFiles/opencv_videoio.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2
Copyright notice: This article is CSDN Blogger「hanhung」Original articles, follow CC 4.0 BY-SA Copyright agreement, please attach the original source link and this statement.
Original link:

1.8. Installing opencv

sudo make install

The compilation task is over. The following is the opencv configuration

2.1. First, add the OpenCV library to the path so that the system can find

sudo gedit /etc/

2.2. After executing this command, you may open a blank file. Don't worry, just add it at the end of the file


2.3. Execute the following command to make the path just configured effective

sudo ldconfig

2.. 4 configuring the bush

sudo gedit /etc/bash.bashrc

2.5 add at the end


2.6 save and execute the following commands to make the configuration effective

source /etc/bash.bashrc

Posted by nicephotog on Fri, 19 Nov 2021 04:05:11 -0800