Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arthurbeggs/06df46af94af7f261513934e56103b30 to your computer and use it in GitHub Desktop.
Save arthurbeggs/06df46af94af7f261513934e56103b30 to your computer and use it in GitHub Desktop.
Install OpenCV2 in Ubuntu
#!/bin/bash
################################################################################
### OpenCV2 Installation Script ###
################################################################################
# Source code at https://github.com/arthurbeggs/scripts #
################################################################################
# #
# Feel free to copy and modify this file. Giving me credit for it is your #
# choice, but please keep references to other people's work, which I don't #
# have ownership and thus cannot decide what to do with the licenses. #
# #
################################################################################
### Single line script will download and run this script automatically:
# curl -s "https://raw.githubusercontent.com/arthurbeggs/scripts/master/install_apps/install_opencv2.sh" | bash
### Dependencies
sudo apt-get update
sudo apt-get install -y build-essential cmake libgtk2.0-dev pkg-config \
python-numpy python-dev libavcodec-dev libavformat-dev \
libswscale-dev libjpeg-dev libpng12-dev libtiff5-dev \
libjasper-dev libopencv-dev checkinstall pkg-config \
yasm libjpeg-dev libjasper-dev libavcodec-dev \
libavformat-dev libswscale-dev libdc1394-22-dev \
libxine2 libgstreamer0.10-dev libv4l-dev \
libgstreamer-plugins-base0.10-dev python-dev \
python-numpy libtbb-dev libqt4-dev libgtk2.0-dev \
libmp3lame-dev libopencore-amrnb-dev \
libopencore-amrwb-dev libtheora-dev libvorbis-dev \
libxvidcore-dev x264 v4l-utils unzip
### Download opencv-2.4.13.5
wget https://github.com/opencv/opencv/archive/2.4.13.5.zip -O opencv-2.4.13.5.zip
unzip opencv-2.4.13.5.zip
cd opencv-2.4.13.5
mkdir release
cd release
### Compile and install
cmake -G "Unix Makefiles" -DCMAKE_CXX_COMPILER=/usr/bin/g++ CMAKE_C_COMPILER=/usr/bin/gcc -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DWITH_TBB=ON -DBUILD_NEW_PYTHON_SUPPORT=ON -DWITH_V4L=ON -DINSTALL_C_EXAMPLES=ON -DINSTALL_PYTHON_EXAMPLES=ON -DBUILD_EXAMPLES=ON -DWITH_QT=ON -DWITH_OPENGL=ON -DBUILD_FAT_JAVA_LIB=ON -DINSTALL_TO_MANGLED_PATHS=ON -DINSTALL_CREATE_DISTRIB=ON -DINSTALL_TESTS=ON -DENABLE_FAST_MATH=ON -DWITH_IMAGEIO=ON -DBUILD_SHARED_LIBS=OFF -DWITH_GSTREAMER=ON ..
make all -j$(nproc) # Uses all machine cores
sudo make install
cd ../../
rm -rf ./opencv-2.4.13.5
sudo apt-get install python-opencv -y
### Echoes OpenCV installed version if installation process was successful
echo -e "OpenCV version:"
pkg-config --modversion opencv
@ppineda32
Copy link

ppineda32 commented Sep 28, 2017

Thanks
If someone looking to install it in Ubuntu 17 use another version of the compiler
For example:
https://gist.github.com/ibogun/ec0a4005c25df57a1b9d#file-gistfile1-txt
and change complier
-DCMAKE_CXX_COMPILER=/usr/bin/g++-4.9 CMAKE_C_COMPILER=/usr/bin/gcc-4.9

@SportsMUST
Copy link

sudo: ./install-opencv-2.4.13-in-ubuntu.sh: command not found
image

what can i do?

thank you very much

@SportsMUST
Copy link

image
thank you very much!

@bharathkotari
Copy link

hi i followed your instructions , but SIFT and SURF are not installed . how to access them . they are present in modules/nonfree folder in the zip . but when i compile and install it and tried to access SURF and SIFT it is showing a error .that those modules are not present . help please

@Piusha
Copy link

Piusha commented Oct 12, 2017

Hi When I run your script I am getting following error what would be the issue and how to install it smoothly

qmake: could not exec '/usr/lib/x86_64-linux-gnu/qt4/bin/qmake': No such file or directory
CMake Error at /usr/share/cmake-3.7/Modules/FindQt4.cmake:1318 (message):
Found unsuitable Qt version "" from NOTFOUND, this code requires Qt 4.x
Call Stack (most recent call first):
cmake/OpenCVFindLibsGUI.cmake:34 (find_package)
CMakeLists.txt:487 (include)

-- Configuring incomplete, errors occurred!
See also "/home/scit/Desktop/opencv-2.4.13/release/CMakeFiles/CMakeOutput.log".
See also "/home/scit/Desktop/opencv-2.4.13/release/CMakeFiles/CMakeError.log".
make: *** No rule to make target 'all'. Stop.
make: *** No rule to make target 'install'. Stop.

@SutanuNandigrami
Copy link

SutanuNandigrami commented Nov 5, 2017

I have solved the problem of "sudo make all "--error


Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2


run these commands if you encounter the above error

cmake -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_PERF_TESTS=OFF -DBUILD_opencv_gpu=OFF -DBUILD_opencv_ocl=OFF

make

sudo make install

@arthurbeggs
Copy link
Author

arthurbeggs commented Nov 15, 2017

Hello everybody. I'm sorry for taking so long to answer you all. I didn't receive any notification of your comments here, so I had no idea this script wasn't working.

I've updated the script and now it works flawlessly on Ubuntu 17.04 (probably works on 16.04, 16.10 and 17.10 too, but I can't confirm). There were 2 problems:
1st - OpenCV2.4.13 makefile isn't compatible with GCC 6.x. OpenCV2.4.13.4 corrects it;
2nd - Python needs python-opencv package, and now I've included it in this script.


Now answering your questions:

@vasu1765 - Sorry, I don't know the origin of your problem nor how to correct it. You probably already solved it (at least that's what I hope for), but I suggest you and everybody with a similar problem to uninstall opencv with make uninstall and rebuild it paying attention to warnings and error messages.

@manish988 @soumyadeep1997 @wfs @MrNocTV @suresh88 - If your OpenCV2 build was successfull, sudo apt-get install python-opencv should fix it as @drsagitn pointed out (but avoid running this script and installing the opencv-python pip package).

@tkotha @TeixeiraRafael @bharathkotari - Unfortunately I use OpenCV only to build other people's code and thus my OpenCV knowledge is scarse, so I have no idea :c

@jyothi2958 - The -fPIC build parameter is already in OpenCV2.4.13.4 makefile. I suggest that you remove your already built libs running make uninstall on your installation folder and rebuild opencv with the updated script.

@eran-misi and @omar-JHA - " # 2 cores" is a comment and thus anything after the hash is ignored by bash. Anyway, thanks for trying to help @jyothi2958 out :)

@Napoleon-BlownApart - checkinstall is included in the long dependency list. Maybe it's a good idea to use checkinstall but I don't know how to properly work with it. I've tried to do it in my last clean build (yesterday) of opencv and I believe I've made a f***-up running it. For an automated script, it's ideal that checkinstall command receives the needed parameters to create a .deb so the user isn't prompted to make decisions (it's error prune). When I learn how to properly work with it, I'll try to remember to come here and update the script.
As for libpng_dev, as far as I remember there was some incompatibility with OpenCV2.4.13 thus this script uses libpng12_dev. Maybe I'm mistaken, I really can't remember. And I have no idea about the impact of using or not libbb2. It probably won't hurt to include it.

@root-sudip - I'm not sure if it's possible to use OpenCV 2.4 wity python3. Every python3 build I've seen uses OpenCV3.

@DiwakarVartharajan - The pip package is not necessary. sudo apt-get install python-opencv only installs bindings for the OpenCV lib this script builds while pip install opencv-python has bindings AND binaries (opencv core dynamic libs). If you run this script and install the pip package, it's possible that your build will have conflicting libs.

@pab12play - OpenCV2.4.13.4 fixes it, so you don't need to downgrade your GCC anymore :)

@SportsMUST - Don't run it with sudo. It's possible (but not probable) that you'll need to chmod the script file to be able to run it. As for fatal error: opencl_kernels.hpp: No such file or directory error, I suggest you to use the updated script and make a clean build.

@Piusha - The problem is that your machine has QT, but it's a version prior to 4.x and OpenCV2.4 requires QT 4.x. I believe running apt-get install libqt4-core libqt4-dev libqt4-gui qt4-dev-tools before running the updated script will solve your problem.

@SutanuNandigrami - I believe your solution isn't necessary anymore as OpenCV2.4.13.4 corrects build bugs :)


If anyone else wants to reach me to discuss this script, please use @mentions. I believe that way I'll be notified of your messages :)

@sammilei
Copy link

./install-opencv-2.4.13.4-in-ubuntu.sh: line 24: syntax error near unexpected token (' ./install-opencv-2.4.13.4-in-ubuntu.sh: line 24: make all -($(nproc)) # Uses all machine cores'

@Digital2Slave
Copy link

@sammilei change make all -($(nproc)) to make all -j$(nproc)

@arthurbeggs
Copy link
Author

I guess I've changed make all -j4 to make all -($(nproc)) after running the script and forgot to look for typos. My bad.

Changed to make all -j$(nproc) as @johntian pointed out.

@walacetalesfs
Copy link

thanks

@FrankPerM
Copy link

img_20171227_190344004
How do I solve this problem?

@wkentdag
Copy link

for anybody else getting CUDA-related errors when running the cmake command, you can add another flag to skip the CUDA stuff (and then install it later on, which is what I did):

-D WITH_CUDA=OFF

https://codeyarns.com/2013/07/23/how-to-compile-opencv-without-cuda-support/

@IcyW
Copy link

IcyW commented Mar 19, 2018

I've met the following errors and failed to find the solution:

image

Does anyone know the reason why "recipe for target 'XXX' failed" happens all the time?

@HARISH222
Copy link

Thanks

@pfabreu
Copy link

pfabreu commented Mar 27, 2018

Not sure how, but does anyone know how to add more recent GPU architectures for it to build so the annoying nvcc warning stops appearing? I know you can even specify like "Kepler" or "Fermi" (not sure about Maxwell). Either that or add -D WITH_CUDA=OFF like @wkentdag said.

@Jayhello
Copy link

As to me , it works.

@hancerli
Copy link

Notes for Ubuntu 17.10.

  • libgstreamer is upgraded to v1.0 so it's not possible to install v0.10 via apt.
    Unable to locate package libgstreamer0.10-dev
  • And also every command related to libgstreamer is broken since the version is changed.
  • libjasper-dev is removed from the repository. I couldn't find the replacement at the moment.
    Unable to locate package libjasper-dev
  • libpng is upgraded to v16. So you should use "libpng-dev" instead. This should install all development headers and the current version from the repository.

@aasharma90
Copy link

aasharma90 commented May 16, 2018

Hello,

I get a strange error during the cmake process itself (running on Ubuntu16.04). It says -

/home/aashishsharma/Desktop/opencv-2.4.13.5/release/CMakeFiles/CMakeTmp/CheckSymbolExists.cxx:8:19: error: ‘Q_WS_WIN’ was not declared in this scope
return ((int*)(&Q_WS_WIN))[argc];

Can anybody please help me out? I can share the entire log if needed. Thanks!

@fernando79513
Copy link

Thanks!!

@YJonmo
Copy link

YJonmo commented Jun 28, 2018

[ 18%] Linking CXX executable ../../bin/opencv_annotation
/home/yjon701/anaconda2/lib/libQt5Test.so.5.6.2: undefined reference to operator delete[](void*, unsigned long)@CXXABI_1.3.9' /home/yjon701/anaconda2/lib/libpng16.so.16: undefined reference to inflateValidate@ZLIB_1.2.9'
/home/yjon701/anaconda2/lib/libQt5Test.so.5.6.2: undefined reference to __cxa_throw_bad_array_new_length@CXXABI_1.3.8' /home/yjon701/anaconda2/lib/libQt5Test.so.5.6.2: undefined reference to operator delete(void*, unsigned long)@CXXABI_1.3.9'
collect2: error: ld returned 1 exit status

[ 18%] Linking CXX executable ../../bin/opencv_visualisation
/home/yjon701/anaconda2/lib/libQt5Test.so.5.6.2: undefined reference to operator delete[](void*, unsigned long)@CXXABI_1.3.9' /home/yjon701/anaconda2/lib/libpng16.so.16: undefined reference to inflateValidate@ZLIB_1.2.9'
/home/yjon701/anaconda2/lib/libQt5Test.so.5.6.2: undefined reference to __cxa_throw_bad_array_new_length@CXXABI_1.3.8' /home/yjon701/anaconda2/lib/libQt5Test.so.5.6.2: undefined reference to operator delete(void*, unsigned long)@CXXABI_1.3.9'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/opencv_visualisation] Error 1
make[1]: *** [apps/visualisation/CMakeFiles/opencv_visualisation.dir/all] Error 2

@kharek
Copy link

kharek commented Jul 12, 2018

Thanks!!!
Instructions works well on ubuntu 18 as well just by using the newest set of packages available for the distro

@JaySurplus
Copy link

sudo: ./install-opencv-2.4.13-in-ubuntu.sh: command not found
image

what can i do?

thank you very much

you can try with chmod +x install_opencv2_ubuntu.sh before you run the shell script.

@GuruMech
Copy link

@drsagitn 'sudo apt-get install python-opencv
pip install opencv-python' installs opencv 3.3.0. What is the way to install opencv 2.4.13?
Thanks in advance.

Hi,
were you able to install python binding via apt-get install python-opencv for the above OpenCV installation?

@cgbahk
Copy link

cgbahk commented Aug 11, 2019

Thanks, but in my case script not worked as intended at first time... since... I had no unzip :)

For someone like me, it seems good to add script doing sudo apt install -y unzip

Also good to have set -e at the first line to stop procedure for the case like me

@jimitshah77
Copy link

jimitshah77 commented Dec 23, 2019

Awesome!
Thank you!

@StephenJau
Copy link

Thank you very much

@xolanindlovugatsheni
Copy link

Has anyone come across this error. How do i solve this.
Screenshot from 2021-02-07 10-57-26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment