This article covers the quickest way to install OpenCV on Ubuntu 18.04 using the Aptitude package manager. This will automatically make sure that the prerequisites are met and installed. It’s worth noting that if you require a specific version of OpenCV then you may need to follow our tutorial How To Install OpenCV 4.1.2 for OpenVino and Python 3 on Ubuntu 18.04. Note: If you plan on using OpenCV with OpenVino you will need to follow that tutorial to make sure you have OpenCV 4.1.2 or higher installed.
Install OpenCV without Virtualenv support
If you are NOT using a virtualenv then you can install OpenCV with the Aptitude package manager, which ships with Ubuntu 18.04. Open up a terminal window and type the following command:
$ apt install python3-opencv
If the OpenCV package is already installed then it will inform you of this and you are all set. If not then it will prompt you to make sure you are happy using up an amount of space, specified in the request. It will then install all of the necessary prerequisites and the OpenCV libraries with their associated Python3 bindings. Move on to the section entitled “Testing OpenCV Installed Correctly”.
Install OpenCV within a Virtualenv
If you are using Virtualenv then activate your environment and use the following commands (NOTE: These steps assume you are using Python3 inside the Virtualenv – if you are not sure use
python --versionto check)
$ pip install opencv-python
Once this finishes then continue onto the section below, entitled “Testing OpenCV Installed Correctly”.
Testing OpenCV Installed Correctly
Once it has finished installing run the following commands to verify that it has installed correctly:
$ python3 >>> import cv2 >>> print(cv2.__version__)
You should then see the version number printed to the screen. If you receive an error on the line
import cv2then it is likely that the package hasn’t installed correctly. Try and find any errors that occurred during the installation process in order to reveal more as to why this might have happened. If this is not the case then you are all set and ready to start using OpenCV with Python3.