Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@cagnulein

This comment has been minimized.

Copy link

@cagnulein cagnulein commented Jul 27, 2016

thanks

@vasu1765

This comment has been minimized.

Copy link

@vasu1765 vasu1765 commented Aug 9, 2016

ImportError: /usr/local/lib/python2.7/site-packages/cv2.so: undefined symbol: _ZNK2cv9Algorithm9paramHelpERKSs

@sedovolosiy

This comment has been minimized.

Copy link

@sedovolosiy sedovolosiy commented Aug 9, 2016

thanks!

@pyratin

This comment has been minimized.

Copy link

@pyratin pyratin commented Nov 3, 2016

thanks! :D

@manish988

This comment has been minimized.

Copy link

@manish988 manish988 commented Nov 12, 2016

i m unable to import cv2. plz help me out

@soumyadeep1997

This comment has been minimized.

Copy link

@soumyadeep1997 soumyadeep1997 commented Dec 12, 2016

i am also unable to import cv2. plz help

@wfs

This comment has been minimized.

Copy link

@wfs wfs commented Dec 14, 2016

Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import cv2
Traceback (most recent call last):
File "", line 1, in
ImportError: libcudart.so.7.5: cannot open shared object file: No such file or directory

Screw it. Going to use anaconda instead.

@tkotha

This comment has been minimized.

Copy link

@tkotha tkotha commented Dec 21, 2016

how do i integrate cuda 7.5 into this installation?

@Jayhello

This comment has been minimized.

Copy link

@Jayhello Jayhello commented Jan 4, 2017

thank you==========

@MrNocTV

This comment has been minimized.

Copy link

@MrNocTV MrNocTV commented Mar 13, 2017

It didn't work, I still got "No module named cv2".

@jyothi2958

This comment has been minimized.

Copy link

@jyothi2958 jyothi2958 commented May 6, 2017

what can i do?
screenshot from 2017-05-06 15-02-45

@jyothi2958

This comment has been minimized.

Copy link

@jyothi2958 jyothi2958 commented May 6, 2017

make all -j2 # 2 cores
error plz plz help me

@eran-misi

This comment has been minimized.

Copy link

@eran-misi eran-misi commented Jun 15, 2017

You should just ---> make all -j2

Without the ---> # 2 cores

@omar-JHA

This comment has been minimized.

Copy link

@omar-JHA omar-JHA commented Jun 20, 2017

Write sudo make -j2

@Napoleon-BlownApart

This comment has been minimized.

Copy link

@Napoleon-BlownApart Napoleon-BlownApart commented Jul 12, 2017

Thanks, worked fine for me.

Suggestion:
add checkinstall to the installed packages list, and then make line 23: sudo make install && sudo checkinstall

Also, I've seen another install guide that includes libtbb2 and libpng-dev in the installed packages list. Do you think it's worthwhile including these?

@root-sudip

This comment has been minimized.

Copy link

@root-sudip root-sudip commented Jul 15, 2017

this is for python2.7, how to install for python3.5??

@TeixeiraRafael

This comment has been minimized.

Copy link

@TeixeiraRafael TeixeiraRafael commented Aug 7, 2017

Is there a way to integrate OpenCV-2.4.13 and cuda-8.0? Can't use both.

@suresh88

This comment has been minimized.

Copy link

@suresh88 suresh88 commented Sep 6, 2017

Good post.
Installation went smooth for me, but while importing cv2 I am getting No module named 'cv2' error.

Can someone help me in this.

@drsagitn

This comment has been minimized.

Copy link

@drsagitn drsagitn commented Sep 11, 2017

I had the same problem "No module named cv2".
Please help!

@drsagitn

This comment has been minimized.

Copy link

@drsagitn drsagitn commented Sep 12, 2017

I installed opencv-python and problem solved

sudo apt-get install python-opencv
pip install opencv-python

@DiwakarVartharajan

This comment has been minimized.

Copy link

@DiwakarVartharajan DiwakarVartharajan commented Sep 20, 2017

@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.

@ppineda32

This comment has been minimized.

Copy link

@ppineda32 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

This comment has been minimized.

Copy link

@SportsMUST SportsMUST commented Sep 29, 2017

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

what can i do?

thank you very much

@SportsMUST

This comment has been minimized.

Copy link

@SportsMUST SportsMUST commented Sep 29, 2017

image
thank you very much!

@bharathkotari

This comment has been minimized.

Copy link

@bharathkotari bharathkotari commented Oct 10, 2017

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

This comment has been minimized.

Copy link

@Piusha 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

This comment has been minimized.

Copy link

@SutanuNandigrami 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

This comment has been minimized.

Copy link
Owner Author

@arthurbeggs 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

This comment has been minimized.

Copy link

@sammilei sammilei commented Dec 10, 2017

./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'

@JohnTian

This comment has been minimized.

Copy link

@JohnTian JohnTian commented Dec 11, 2017

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

@arthurbeggs

This comment has been minimized.

Copy link
Owner Author

@arthurbeggs arthurbeggs commented Dec 22, 2017

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

This comment has been minimized.

Copy link

@walacetalesfs walacetalesfs commented Dec 26, 2017

thanks

@FrankPerM

This comment has been minimized.

Copy link

@FrankPerM FrankPerM commented Dec 28, 2017

img_20171227_190344004
How do I solve this problem?

@wkentdag

This comment has been minimized.

Copy link

@wkentdag wkentdag commented Feb 14, 2018

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

This comment has been minimized.

Copy link

@IcyW 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

This comment has been minimized.

Copy link

@HARISH222 HARISH222 commented Mar 21, 2018

Thanks

@pedro-abreu

This comment has been minimized.

Copy link

@pedro-abreu pedro-abreu 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

This comment has been minimized.

Copy link

@Jayhello Jayhello commented Apr 10, 2018

As to me , it works.

@hancerli

This comment has been minimized.

Copy link

@hancerli hancerli commented Apr 11, 2018

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

This comment has been minimized.

Copy link

@aasharma90 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

This comment has been minimized.

Copy link

@fernando79513 fernando79513 commented May 22, 2018

Thanks!!

@YJonmo

This comment has been minimized.

Copy link

@YJonmo 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

This comment has been minimized.

Copy link

@kharek 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

This comment has been minimized.

Copy link

@JaySurplus JaySurplus commented Sep 18, 2018

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

This comment has been minimized.

Copy link

@GuruMech GuruMech commented Nov 13, 2018

@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

This comment has been minimized.

Copy link

@cgbahk 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

This comment has been minimized.

Copy link

@jimitshah77 jimitshah77 commented Dec 23, 2019

Awesome!
Thank you!

@StephenJau

This comment has been minimized.

Copy link

@StephenJau StephenJau commented Apr 19, 2020

Thank you very much

@xolanindlovugatsheni

This comment has been minimized.

Copy link

@xolanindlovugatsheni xolanindlovugatsheni commented Feb 7, 2021

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