Install OpenCV 4 on Ubuntu 18.04 Bionic Beaver

· 4 min read
Install OpenCV 4 on Ubuntu 18.04

In the previous article, we tried the OpenCV 4.1 installation on a Raspberry Pi 3 Model B+ device. Because Raspbian and Ubuntu are both derivatives from Debian, the installation method is not much different. Several parts are removed, and some parts that are added.

Regarding OpenCV, you can see the full description in the previous article, namely, Install OpenCV 4 on the Raspberry Pi.


To follow this article, you must prepare a PC or Server that has installed Ubuntu 18.04. An adequate internet connection is needed to download the source code and packages needed.

In this experiment, we used VPS with 4GB RAM and 4 Core Processors.

Install OpenCV 4.1

Before the installation process, make sure you update the latest system. If you have already updated, please skip this step. If not, please update your Ubuntu with the following command, then restart.

sudo apt update
sudo apt upgrade -y
sudo reboot

Install Python

Because we will use python, we must install python along with the pip package. We will install Python version 2 and version 3 at once. Please use the following command.

sudo apt install python python3 python-dev python3-dev python-pip python3-pip -y

Check the results of your python installation, whether it is running as it should.

python --version
python3 --version
pip --version
pip3 --version

If everything is OK, the output of the command above is something like this.

away@ubuntu:~$ python --version
Python 2.7.15+
away@ubuntu:~$ python3 --version
Python 3.6.8
away@ubuntu:~$ pip --version
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
away@ubuntu:~$ pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)

Install Dependencies that are needed

Next, we will install the required dependencies. Applications like cmake, unzip, and others will be required during the OpenCV compilation process.

sudo apt-get install build-essential cmake unzip pkg-config -y
sudo apt-get install libjpeg-dev libpng-dev libtiff-dev -y
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y
sudo apt-get install libxvidcore-dev libx264-dev -y
sudo apt-get install libgtk-3-dev -y
sudo apt-get install libatlas-base-dev gfortran -y

Download OpenCV

In the next step, we will download and prepare the source from OpenCV. Use the following command.

cd ~
wget -O
wget -O

Extract the downloaded zip file, then delete the zip file and rename the extracted folder to make it easier.

mv opencv-4.1.0 opencv
mv opencv_contrib-4.1.0 opencv_contrib
rm -f

Configuring the Virtual Environment

The Virtual Environment is useful so that all installations performed only work on a virtual system. This is useful if you don't want to use a standard library in a normal environment, and will use another version of the library in the virtual environment.

sudo pip install virtualenv virtualenvwrapper
sudo pip3 install virtualenv virtualenvwrapper
sudo rm -rf ~/ ~/.cache/pip

To complete the installation, we will update the ~/.profile file. Open and modify ~/.profile with the nano command.

nano ~/.profile

Then fill the bottom with the following script.

export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/

Then use the profile with the command.

source ~/.profile

When finished, we will try to create a new virtual environment called cv. You can make any environment name. But in this article, we will use cv to make it easier.

mkvirtualenv cv -p python3

And try to activate the virtual environment.

workon cv

If there are no problems, your terminal will start with a sign (cv) which indicates that you are working in a virtual environment cv. If you want to exit the environment, you can use the deactivate command.


Install NumPY

Next, we will install numpy in our environment. NumPy is a Python library for Scientific Computing. Type the following code, make sure you are in the Virtual Environment cv.

pip install numpy

Compile OpenCV

Next, we will configure cmake for OpenCV. We will work in the build folder. So follow the instructions below to create a build folder and configure our OpenCV.

cd ~/opencv
mkdir build
cd build
	-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
	-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \

Next, we will compile OpenCV using all processor cores. First, check how many of your processor cores are with nproc command.


Because we use four processor cores, the results of the nproc command are 4. Use the make -j<number of cores> command to start the compile process.

make -j4

Wait until the process is complete. The time needed depends on the speed of your computer or server. When finished, continue by make install and ldconfig, then creating a symlink so that OpenCV works in the Virtual Environment.

sudo make install
sudo ldconfig
ln -s  /usr/local/lib/python3.6/site-packages/cv2/python-3.6/ ~/.virtualenvs/cv/lib/python3.6/site-packages/

Delete Installation Folder

To save space, we can delete the installation folder now. Please remove using the command below.

cd ~
rm -fr ~/opencv ~/opencv_contrib

Test your Installation

When finished, please test the installation results using the python command below.

python -c 'import cv2; print(cv2.__version__)'

If there is an output that shows the OpenCV version as below, that's mean your installation process is complete.



You have learned how to install OpenCV on an Ubuntu 18.04 server. The process is almost the same as the installation on the Raspberry Pi. However, on the server that we use, the process is faster because it uses an SSD Disk that has more reliable IO performance.

In the next article, we will try some simple Computer Vision projects that use OpenCV.

Hopefully, this article helps.