Since apt doesn’t support 3-party components and might not install the latest version of OpenCV sometimes, installing OpenCV by building yourself might be helpful in some case.
Firstly, install all the packages that essential for building. The following packages contain libraries that OpenCV depends on, but do not include GPU acceleration and ML parts.
apt-get update && apt-get upgrade add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main" apt-get install -y build-essential cmake git unzip zip\ pkg-config libavcodec-dev libavformat-dev libswscale-dev \ libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev \ libsnappy-dev libboost-all-dev libjasper-dev libdc1394-22-dev\ python3-dev python3-numpy python3-pip ffmpeg libopenblas-dev \ tesseract-ocr libtesseract-dev libprotobuf-dev libleveldb-dev \ libhdf5-serial-dev protobuf-compiler libatlas-base-dev \ libgflags-dev libgoogle-glog-dev liblmdb-dev libfaac-dev \ gfortran libgstreamer1.0-dev libatlas-base-dev libxvidcore-dev \ libpng-dev libopenexr-dev libwebp-dev \ libmp3lame-dev libtheora-dev libvorbis-dev \ libopencore-amrwb-dev x264 v4l-utils libgdk-pixbuf2.0-dev \ manpages-dev libopencore-amrnb-dev libgstreamer-plugins-base1.0-dev libavresample-dev pip3 install --upgrade pip apt-get install -f
Then, download and unzip OpenCV source code form GitHub. In order not to mess the working directory up, it is recommended to create a new folder dedicated for compilation.
Note: OpenCV may have been updated since the blog was written, so don’t just simply copy & paste the following command.
mkdir opencv_build && cd opencv_build wget https://github.com/opencv/opencv/archive/refs/tags/4.5.3.zip unzip 4.5.3.zip && rm 4.5.3.zip wget https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.3.zip unzip 4.5.3.zip && rm 4.5.3.zip cd opencv-4.5.3
Now start building. The last line indicates the number of threads used for compilation, which should be determined according to the number of CPU cores of the machine.
mkdir build && cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local/ \ -D OPENCV_ENABLE_NONFREE=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib-4.5.3/modules \ -D OPENCV_GENERATE_PKGCONFIG=YES \ -D WITH_QT=OFF \ -D WITH_OPENGL=OFF \ -D WITH_CUDA=OFF \ -D BUILD_EXAMPLES=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_C_EXAMPLES=ON .. make -j6
If there is no error in the compilation process, then start the installation. Just running a single line of code:
After all of that, the OpenCV lib files is now can be included(or imported) by CMake. But if you are trying to use an IDE like VSCode, there might be an error indicate that the lib files could not be found.
So the very last step is needed.
sh -c "echo '/usr/local/opencv4.5.2/lib' >> /etc/ld.so.conf.d/opencv.conf" sh -c "echo 'export PKG_CONFIG_PATH=/usr/local/opencv4.5.3/lib/pkgconfig:$PKG_CONFIG_PATH' >> /etc/profile.d/pkgconfig.sh" source /etc/profile ln -s /usr/local/opencv4.5.3/lib/python3.8/dist-packages/cv2/python-3.8/cv2.cpython-38-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/cv2.so
To facilitate future deployment, I wrote the above process into a script, and run it in bash will automatically start the installation(Rename before using!).
mv opencv4.5.3.jpg opencv4.5.3.sh chmod +x opencv4.5.3.sh sudo ./opencv.4.5.3.sh
In fact, OpenCV offers a pre-build package of the latest version, but without including opencv_contrib.
If you are using OpenCV for just studying, you can just download a copy here.