Install OpenCv4.0 Problem

I want to install opencv4.0 when I finished installing the nano ubuntu18 dev.
So I uninstalled the opencv3.4.1 which the nano has the opencv.
And I follow the github:
to install opencv4.0 and meet this problem:

errorE: unable to locate libjasper-dev

I try to sudo gedit /etc/apt/sources.list
to append the list:
deb http:// xenial main
deb xenial-security main

But appear the error again.
Can anyone solve this problem?
Many thanks.

Anyway, I try to install without installing the libjasper-dev and continue to install step by step and finally I get this error:

CMake Error at (message):
Error generating file

modules/cudaoptflow/CMakeFiles/opencv_cudaoptflow.dir/build.make:675: recipe for target ‘modules/cudaoptflow/CMakeFiles/cuda_compile_1.dir/src/cuda/’ failed
make[2]: *** [modules/cudaoptflow/CMakeFiles/cuda_compile_1.dir/src/cuda/] Error 1
CMakeFiles/Makefile2:9900: recipe for target ‘modules/cudaoptflow/CMakeFiles/opencv_cudaoptflow.dir/all’ failed
make[1]: *** [modules/cudaoptflow/CMakeFiles/opencv_cudaoptflow.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs…

[ 94%] Built target opencv_stitching
Makefile:162: recipe for target ‘all’ failed
make: *** [all] Error 2

Maybe without libjasper-dev would cost this problem.


OpenCV can be successfully build without the libjasper-dev library.
It is used in our previous version. I will check if it is still necessary for Ubuntu-18.04.

The error you meet looks related to the multi-threading.
Have you executed other application at the same time?

Or could you try to build the sample with $make -j1 rather than make -j3.

I also use this command to succeed to install opencv4.0 as follow:
sudo fallocate -l 4.0G /swapfile # this is the difference sudo chmod 600 /swapfile
sudo mkswap /swapfile sudo swapon /swapfile

you need to pass the parameter as :

-D BUILD_opencv_cudacodec=OFF

Upd: it is not the case as it is rather applicable to desktop installation with cuda 10+

I had the same problem building. I second AastaLLL in that it seems related to a lack of ram.

For me, make -j1 wasn’t enough. I had to turn off x11 as well (sudo systemctl isolate

That works too. To add to this: to make it faster and reduce wear on your sdcard, you can put the swapfile on an external USB 3 drive (preferably SSD).

But when I want to build the opencv project using this command:
g++ pkg-config opencv --cflags test.cpp -o test pkg-config opencv --libs

It would show this error:fatal error: opencv2/oepncv.hpp: No such file or directory
How to solve this problem?

when 4+ version is used pkg-config rather needs argument as opencv4 than opencv.
However you need it be first built and installed into the system.
You may try

g++ `pkg-config opencv --cflags` test.cpp -o test `pkg-config opencv4 --libs`

or what worked for me was

g++ -o simple_opencv -Wall -std=c++11 simple_opencv.cpp $(pkg-config --cflags --libs opencv4)

Hi Andrey1984, I try to use your two methods to build my test.cpp project. But still errors as follow:

Package opencv4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv4.pc’
to the PKG_CONFIG_PATH environment variable
No package ‘opencv4’ found
In file included from readphoto.cpp:3:0:
/usr/include/opencv2/tracking.hpp:281:10: fatal error: opencv2/tracking/tracker.hpp: No such file or directory
#include <opencv2/tracking/tracker.hpp>
compilation terminated.

my test.cpp as follow:
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>

using namespace std;
using namespace cv;

int main()
Mat srcImage = imread("/home/kirito/procedure/tld/1.png");


return 0;


I do not know why this code will include the old path:/usr/include/opencv2 (there is no tracking.cpp)
I want to include the new path: /usr/local/include/opencv4/opencv2 (there is a tracking.cpp)

probably you did not install the opencv4, or did not include pkg-config option to it.
You may try to build it again and with the argument below:


And do not forget to run the “install” sequence after the make has been made.

Actually I add this argument when I cmake as follow:


$sudo make install

But still appear this error again.
Should I delete the release file before cmake?
Since I cmake this would cost about 4-5 hours

In my opinion,
you need to delete the build folder, and run cmake again.[ at least it worked for me in similar case].
You also will need to make sure that numpy and python paths are correct at outputs of the cmake. And you may locate the cpython or with mlocate if you can not find them after the make & make install

Hi Andrey1984, thanks for always helping me.
I try to create the CMakeLists.txt to build the project and CMakeLists.txt as below:

cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS “-std=c++11”)
find_package(OpenCV 4.1 REQUIRED)
include_directories({OpenCV_INCLUDE_DIRS}) add_executable(test test.cpp) target_link_libraries(test {OpenCV_LIBS})

and successfully.
This method can solve this error.
But I will delete the release file and cmake again because I want to know whether can solve this error another way.
Thanks a lot!

Worked for me!