Mind Chasers Inc.

Build and test OpenCV 4 from Git on Ubuntu 18.04

This article reviews building a simple OpenCV 4 video playback application using the OpenCV Git repos on Ubuntu Linux 18.04.


This article reviews building a simple OpenCV 4 video playback application using the OpenCV Git repos on Ubuntu Linux 18.04. This may prove helpful to others since some tweaking was required even after following other resources. If you are having problems getting simple applications to work, maybe this article will help.

First opencv, opencv_contrib, and opencv_extra were all cloned at the same directory level using git:

$ mkdir /build/opencv
$ cd /build/opencv

$ git clone https://github.com/opencv/opencv.git
$ git clone https://github.com/opencv/opencv_contrib.git
$ git clone https://github.com/opencv/opencv_extra.git

$ ls
opencv  opencv_contrib  opencv_extra

Listed below are the packages we installed for OpenCV. Of course, other standard build related packages are required (e.g., build-essential, python, etc.). However, the tricky part seems to be installing the right video support libraries. And make sure to notice that the -dev packages are required; the standard (non dev) package will be installed by default.

$ sudo apt install cmake libtbb2 ffmpeg libgtk2.0-dev libavformat-dev libswscale-dev libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev

Optionally, use dpkg to verify packages were properly installed. An example is shown below:

$ dpkg -l | grep ffmpeg
ii  chromium-codecs-ffmpeg-extra               70.0.3538.77-0ubuntu0.18.04.1               amd64        Extra ffmpeg codecs for the Chromium Browser
ii  ffmpeg                                     7:3.4.4-0ubuntu0.18.04.1                    amd64        Tools for transcoding, streaming and playing of multimedia files

Next we build the opencv library following the guidance of the O'Reilly Learning OpenCV book:

$ mkdir /build/opencv/opencv/release
$ cd /build/opencv/opencv/release

$ cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local –DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules ..
$ make
$ sudo make install

Below is the configuration summary output from cmake. Note the availability of the ffmpeg video I/O support. Without this, calls to cap.open() produce the following error: "Unable to stop the stream: Inappropriate ioctl for device"

-- General configuration for OpenCV 4.0.0-pre =====================================
--   Version control:               4.0.0-beta-95-gca5598266
--   Platform:
--     Timestamp:                   2018-11-01T14:52:04Z
--     Host:                        Linux 4.15.0-38-generic x86_64
--     CMake:                       3.10.2
--     CMake generator:             Unix Makefiles
--     CMake build tool:            /usr/bin/make
--     Configuration:               RELEASE
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
--       SSE4_1 (5 files):          + SSSE3 SSE4_1
--       SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (6 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (11 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--       AVX512_SKX (1 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_SKX
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++ Compiler:                /usr/bin/c++  (ver 7.3.0)
--     C++ flags (Release):         -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
--     C++ flags (Debug):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
--     C Compiler:                  /usr/bin/cc
--     C flags (Release):           -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -O3 -DNDEBUG  -DNDEBUG
--     C flags (Debug):             -fsigned-char -W -Wall -Werror=return-type -Werror=non-virtual-dtor -Werror=address -Werror=sequence-point -Wformat -Werror=format-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wuninitialized -Winit-self -Wno-narrowing -Wno-comment -Wimplicit-fallthrough=3 -Wno-strict-overflow -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -g  -O0 -DDEBUG -D_DEBUG
--     Linker flags (Release):      
--     Linker flags (Debug):        
--     ccache:                      NO
--     Precompiled headers:         YES
--     Extra dependencies:          dl m pthread rt
--     3rdparty dependencies:
--   OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java_bindings_generator ml objdetect photo python_bindings_generator shape stitching superres ts video videoio videostab
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 java js python2 python3 viz
--     Applications:                tests perf_tests apps
--     Documentation:               NO
--     Non-free algorithms:         NO
--   GUI: 
--     GTK+:                        YES (ver 2.24.32)
--       GThread :                  YES (ver 2.56.2)
--       GtkGlExt:                  NO
--     VTK support:                 NO
--   Media I/O: 
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.2.11)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         /usr/lib/x86_64-linux-gnu/libpng.so (ver 1.6.34)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.0.9)
--     JPEG 2000:                   build (ver 1.900.1)
--     OpenEXR:                     build (ver 1.7.1)
--     HDR:                         YES
--     SUNRASTER:                   YES
--     PXM:                         YES
--     PFM:                         YES
--   Video I/O:
--     DC1394:                      YES (ver 2.2.5)
--     FFMPEG:                      YES
--       avcodec:                   YES (ver 57.107.100)
--       avformat:                  YES (ver 57.83.100)
--       avutil:                    YES (ver 55.78.100)
--       swscale:                   YES (ver 4.8.100)
--       avresample:                NO
--     GStreamer:                   NO
--     libv4l/libv4l2:              NO
--     v4l/v4l2:                    linux/videodev2.h
--   Parallel framework:            pthreads
--   Trace:                         YES (with Intel ITT)
--   Other third-party libraries:
--     Intel IPP:                   2019.0.0 Gold [2019.0.0]
--            at:                   /build/opencv/opencv/release/3rdparty/ippicv/ippicv_lnx/icv
--     Intel IPP IW:                sources (2019.0.0)
--               at:                /build/opencv/opencv/release/3rdparty/ippicv/ippicv_lnx/iw
--     Lapack:                      NO
--     Eigen:                       NO
--     Custom HAL:                  NO
--     Protobuf:                    build (3.5.1)
--   OpenCL:                        YES (no extra features)
--     Include path:                /build/opencv/opencv/3rdparty/include/opencl/1.2
--     Link libraries:              Dynamic load
--   Python (for build):            /usr/bin/python2.7
--   Java:                          
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
--   Install to:                    /usr/local

Make sure your libraries are being found by the dynamic linker. The output below assumes you installed your libraries to /usr/local/lib and that this directory is listed in one of the conf files (e.g., libc.conf) in /etc/ld.so.conf.d

$ sudo ldconfig

$ ldconfig -p | grep opencv
	libopencv_videostab.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_videostab.so.4.0
	libopencv_videostab.so (libc6,x86-64) => /usr/local/lib/libopencv_videostab.so
	libopencv_videoio.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_videoio.so.4.0
	libopencv_videoio.so (libc6,x86-64) => /usr/local/lib/libopencv_videoio.so
	libopencv_video.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_video.so.4.0
	libopencv_video.so (libc6,x86-64) => /usr/local/lib/libopencv_video.so
	libopencv_superres.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_superres.so.4.0
	libopencv_superres.so (libc6,x86-64) => /usr/local/lib/libopencv_superres.so
	libopencv_stitching.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_stitching.so.4.0
	libopencv_stitching.so (libc6,x86-64) => /usr/local/lib/libopencv_stitching.so
	libopencv_shape.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_shape.so.4.0
	libopencv_shape.so (libc6,x86-64) => /usr/local/lib/libopencv_shape.so
	libopencv_photo.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_photo.so.4.0
	libopencv_photo.so (libc6,x86-64) => /usr/local/lib/libopencv_photo.so
	libopencv_objdetect.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_objdetect.so.4.0
	libopencv_objdetect.so (libc6,x86-64) => /usr/local/lib/libopencv_objdetect.so
	libopencv_ml.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_ml.so.4.0
	libopencv_ml.so (libc6,x86-64) => /usr/local/lib/libopencv_ml.so
	libopencv_imgproc.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_imgproc.so.4.0
	libopencv_imgproc.so (libc6,x86-64) => /usr/local/lib/libopencv_imgproc.so
	libopencv_imgcodecs.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_imgcodecs.so.4.0
	libopencv_imgcodecs.so (libc6,x86-64) => /usr/local/lib/libopencv_imgcodecs.so
	libopencv_highgui.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_highgui.so.4.0
	libopencv_highgui.so (libc6,x86-64) => /usr/local/lib/libopencv_highgui.so
	libopencv_gapi.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_gapi.so.4.0
	libopencv_gapi.so (libc6,x86-64) => /usr/local/lib/libopencv_gapi.so
	libopencv_flann.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_flann.so.4.0
	libopencv_flann.so (libc6,x86-64) => /usr/local/lib/libopencv_flann.so
	libopencv_features2d.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_features2d.so.4.0
	libopencv_features2d.so (libc6,x86-64) => /usr/local/lib/libopencv_features2d.so
	libopencv_dnn.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_dnn.so.4.0
	libopencv_dnn.so (libc6,x86-64) => /usr/local/lib/libopencv_dnn.so
	libopencv_core.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_core.so.4.0
	libopencv_core.so (libc6,x86-64) => /usr/local/lib/libopencv_core.so
	libopencv_calib3d.so.4.0 (libc6,x86-64) => /usr/local/lib/libopencv_calib3d.so.4.0
	libopencv_calib3d.so (libc6,x86-64) => /usr/local/lib/libopencv_calib3d.so

Finally, we build a simple video playback application found on Learning OpenCV github page: example_02-03.cpp.

$ cd /build
$ git clone https://github.com/oreillymedia/Learning-OpenCV-3_examples.git
$ cd /build/Learning-OpenCV-3_examples 

We test it with the big_buck_bunny.mpg found in opencv_extra/testdata/highgui/video/.

$ cp /build/opencv/opencv_extra/testdata/highgui/video/big_buck_bunny.mpg .
$ ls *.mpg

$ gcc -g example_02-03.cpp -I/usr/local/include/opencv4 -lstdc++ -lopencv_imgcodecs -lopencv_highgui \
-lopencv_core -lopencv_imgproc -lopencv_videoio -lopencv_video -lopencv_videostab \
-o example_02-03

$ ./example_02-03 big_buck_bunny.mpg
bunny from mpg
Screenshot from running example_02_03 with OpenCV

We update our site daily with new content related to our open source approach to network security and system design. If you would like to be notified about these changes, then please follow us on Twitter and join our mailing list.

Related articles on this site:

mailing list:

Date: Sept. 12, 2017

Author: HenryC


Absolutely fantastic explanation.. it was the missing information that I needed to enable compiling opencv with video support. Using code::blocks also. super thanks

Date: Nov. 30, 2018

Author: Erdem Tuna


Thank you a lot for the tutorial. This is the only tutorial that made the opencv workspace work for me. Maybe could you add some more details about the "cmake" building options?

Add a new comment here or reply to one above:

For enhanced features and capabilities, please sign in or authenticate using a popular third party

your email address will be kept private
previous month
next month