Installing OpenCV 3.3.0 on Ubuntu 16.04 LTS

Linh Nguyen
Aug 29, 2017 · 6 min read

Step 1: Get ubuntu 16.04 running.

$ sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove

Step 2: Installing all the recommended packages, which are:

$ sudo apt-get install build-essential
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev$ sudo apt-get install libxvidcore-dev libx264-dev$ sudo apt-get install libgtk-3-dev$ sudo apt-get install libatlas-base-dev gfortran$ sudo apt-get install python2.7-dev python3.5-dev

Step 3: Download OpenCV source:

$ cd ~$ wget -O opencv.zip https://github.com/opencv/opencv/archive/3.3.0.zip$ unzip opencv.zip
$ cd ~$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.3.0.zip$ unzip opencv_contrib.zip

Step 4: Setup your environment

$ sudo apt-get install python-pip && pip install --upgrade pip
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip
$ cd ~
$ nano .bashrc
# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
$ source ~/.bashrc
$ mkvirtualenv cv -p python3
Look at the beginning of the line, it said (cv), which is what/where we want to be in
$ workon cv
$ pip install numpy

Step 5: Configuring and compiling

$ workon cv
$ cd ~/opencv-3.3.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D BUILD_EXAMPLES=ON ..
$ make -j4
$ sudo make install
$ sudo ldconfig

Step 6: Finish your OpenCV install (oh yeah, it is not over yet)

$ cd /usr/local/lib/python3.5/site-packages/
$ ls -l
- dist-packages in python 3 folder
or dist-packages in python 2 folder if you use python 2.7
$ sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so
$ cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
$ ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so

Step 7: Test what you have done:

$ cd ~
$ rm -rf opencv-3.3.0 opencv_contrib-3.3.0 opencv.zip opencv_contrib.zip

Linh Nguyen

Written by

A banker trying to handle tech

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade