Compiling & installing the latest OpenCV

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.

On Ubuntu

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 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
unzip && rm
unzip && rm
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
        -D CMAKE_INSTALL_PREFIX=/usr/local/ \
        -D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib-4.5.3/modules \
        -D WITH_QT=OFF \
        -D WITH_OPENGL=OFF \
        -D WITH_CUDA=OFF \
make -j6

If there is no error in the compilation process, then start the installation. Just running a single line of code:

make install

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/"
sh -c "echo 'export PKG_CONFIG_PATH=/usr/local/opencv4.5.3/lib/pkgconfig:$PKG_CONFIG_PATH' >> /etc/profile.d/"
source /etc/profile
ln -s /usr/local/opencv4.5.3/lib/python3.8/dist-packages/cv2/python-3.8/ /usr/lib/python3/dist-packages/

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
chmod +x
sudo ./

On Windows

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.

Otherwise, download the source code of OpenCV here and opencv_contrib here.

Leave a Comment