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
@wkentdag
Copy link

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

HARISH222 commented Mar 21, 2018

Thanks

@pedro-abreu
Copy link

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

Jayhello commented Apr 10, 2018

As to me , it works.

@hancerli
Copy link

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

fernando79513 commented May 22, 2018

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

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

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

StephenJau commented Apr 19, 2020

Thank you very much

@xolanindlovugatsheni
Copy link

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