Create a gist now

Instantly share code, notes, and snippets.

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
### 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

thanks

vasu1765 commented Aug 9, 2016

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

thanks!

pyratin commented Nov 3, 2016

thanks! :D

i m unable to import cv2. plz help me out

i am also unable to import cv2. plz help

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 commented Dec 21, 2016

how do i integrate cuda 7.5 into this installation?

Jayhello commented Jan 4, 2017

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

MrNocTV commented Mar 13, 2017

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

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

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

You should just ---> make all -j2

Without the ---> # 2 cores

Write sudo make -j2

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?

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

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

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.

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

I installed opencv-python and problem solved

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

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

pab12play 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

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

what can i do?

thank you very much

image
thank you very much!

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

Owner

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 :)

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

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

Owner

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.

thanks

img_20171227_190344004
How do I solve this problem?

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/

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