Install opencv 3.2.0 on Ubuntu 16.04 for python 2.7

If you want to play computer vision, opencv is the most popular tool. However, installing opencv is not like install a normal software, it is more complete, and you may need hours to finish the installation. Adrian Rosebrock wrote an excellent tutorial on how to install opencv 3.1.0 on Ubuntu 16.04 with explanation for each step. I did not success by following the steps, however, I succeed with installing a newer version: 3.2.0. This post presents three code blocks to help you install opencv with a couple of copy&paste.

Step 1: update & upgrade Linux system, install libraries/tools, download opencv/opencv_contrib

echo -e "\nUpdate and Upgrade system..." && sleep 3
sudo apt update -y && sudo apt upgrade -y

echo -e "\nInstall developer tools..." && sleep 3
sudo apt install -y build-essential cmake pkg-config

echo -e "\nInstall tools for image process..." && sleep 3
sudo apt install -y libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev

echo -e "\nInstall tools for video process..." && sleep 3
sudo apt install -y libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev

echo -e "\nInstall tools for GUI & optimization..." && sleep 3
sudo apt install -y libgtk-3-dev libatlas-base-dev gfortran

echo -e "\nInstall python development library & pip..." && sleep 3
sudo apt install -y python2.7-dev python-pip

echo "\nDownloading opencv & opencv_contrib version:..." && sleep 3
cd && wget -O && unzip 
cd && wget -O && unzip 

Step 2: Install & setup python virtual environment

echo -e "\nSetup virtual environment..." && sleep 3
if [[ $(grep 'virtualenvwrapper' ~/.bashrc) ]]; then
  echo ".bashrc has been updated!"
    sudo pip install virtualenv virtualenvwrapper
    echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
    echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
    echo "source /usr/local/bin/" >> ~/.bashrc
    source ~/.bashrc
test -d "~/.virtualenvs/cv" && mkvirtualenv cv -p python2 || workon cv

Step 3: Install numpy, configure, compile & install opencv

echo -e "\nInstall opencv ..." && sleep 3
pip install numpy
cd ~/opencv-3.2.0/
mkdir build
cd build
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.2.0/modules \
    -D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
sudo make -j4
sudo make install

cd ~/.virtualenvs/cv/lib/python2.7/site-packages/
sudo ln -s /usr/local/lib/python2.7/site-packages/
Written on February 2, 2017