How to Install OpenCV on Ubuntu 18.04. In this guide you are going to learn how to setup OpenCV (Open Source Computer Vision Library). OpenCV is an open-source highly optimized library with focus on real-time applications and has a wide range of support for C++, Python and Java interfaces support Linux, MacOS, Windows, iOS, and Android.
The library has more than 2500 optimized algorithms for computer vision and machine learning to detect and recognize faces, identify objects, classify human actions in videos, track camera movements and many more.
This setup is tested on Google Cloud, it will work on other cloud services like AWS, Azure and on any VPS or dedicated servers running Ubuntu 18.04.
Start by updating the package index and upgrading the packages to the latest available version.
sudo apt update sudo apt upgrade
Install OpenCV from Repository
OpenCV is available by default in the Ubuntu repositories. If you are just using OpenCV for programming and development purposes you can follow this method. Otherwise you need to install OpenCV from source. You can see this guide just below this method.
To install OpenCV with Python3 you can execute the following command in your terminal.
sudo apt install python3-opencv
To install OpenCV with Python2 you can execute this command instead of the previous one.
sudo apt install python-opencv
Just wait for the installation to complete and verify the installation using the following command.
python3 -c "import cv2; print(cv2.>version)"
This command will output the installed version of OpenCV. At the time of writing the OpenCV version installed is 3.2.0.
Install OpenCV from Source
Installing OpenCV from source is the recommended method to install OpenCV to get control over the full installation.
Follow to below mentioned steps to install OpenCV from source.
Start by installing
cmake for configuring and
gcc for compiling.
sudo apt install git cmake gcc g++
Install Python-devel and Numpy for building Python bindings.
sudo apt install python3-dev python3-numpy
If you wish to install with Python2 you can try use the following command instead of the previous one.
sudo apt install python-dev python-numpy
Now you need GTK support for GUI features, Camera, Media and others.
sudo apt install libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
For GTK 3 support use the following command.
sudo apt install libgtk-3-dev
For GTK 2 support, follow this instead the one before.
sudo apt install libgtk2.0-dev
These are the required packages to install OpenCV from source.
Now you can download OpenCV from using
git clone https://github.com/opencv/opencv.git
This command will download the latest stable version of OpenCV inside a opencv folder.
Navigate inside the opencv folder.
Create a new
build directory and navigate inside the directory.
mkdir build && cd build
Configure OpenCV library using cmake command.
Now you can build the files using
make command and install it using
make install command
make sudo make install
Once the installation is completed, you can verify it using the following command.
pkg-config --modversion opencv4
Prepare yourself for a role working as an Information Technology Professional with Linux operating system
Now you have learned how to install OpenCV on Ubuntu 18.04 from repository and also build from source.
Thanks for your time. If you face any problem or any feedback, please leave a comment below.