How To Uninstall and Install OpenCV Ubuntu 22.04

Updated: May 12, 2022

OpenCV is a programming library that focuses on real-time computer vision with the support of GPU and multi-core processing. It’s an open-source project that can run on almost operating systems nowadays. It is one of the popular projects used by many Computer Vision developers. OpenCV is a cross-platform library so that it can run well on Windows, macOS and Linux. In this tutorial, I will show you how to install OpenCV Ubuntu 22.04 by using Python PIP and Anaconda, this one also helps you to uninstall OpenCV on Ubuntu too.

OpenCV Ubuntu Features

Computer Vision (CV) is a branch of science concerned with how computers can extract high-level information from digital images or movies. With the help of OpenCV, Computer Vision is developing very fast, we will consider some OpenCV features.

  • Open-source library: This means the source code is available to the public. We can customize or write additional code to add additional features to fit your needs.
  • Simple to implement: With library expansion, more and more features have been added, and compatibility with Python and other powerful Python modules like NumPy, … has made OpenCV Python one of the most used modules.
  • Fast building: By combining this module with programming languages ​​like C++, Java, … the OpenCV build will become very fast.

Prerequisites to have Ubuntu install OpenCV

  • Operating System: At mentioned above, OpenCV is a cross-platform library, but in this tutorial, I will use Ubuntu 22.04, you can also use the older Ubuntu versions.
  • Privileages: Make sure that your Ubuntu has sudo permissions.

Install OpenCV Ubuntu 22.04

Installing a popular application like OpenCV is very simple, so we will have a lot of ways to do it, choose the one that you find easiest to do.

Install OpenCV from Ubuntu Repositories

Recently, most of the applications have been added to the Ubuntu Repository because of its simple and quick installation, even for those of you who are not really knowledgeable about the Linux operating system. Therefore, this is one of the top priority ways when you want to install OpenCV Ubuntu. To install this Computer Vision library, we need to run these commands from your terminal.

sudo apt update
sudo apt install python3-opencv libopencv-dev

Install OpenCV from Ubuntu Repositories

Install OpenCV Pip Python on Ubuntu

PIP is a Python package management system that is used to install and manage packages that Python Team does not build. Because OpenCV is also a module that can be used in Python, we can install OpenCV Python through PIP. Before installing, you need to make sure that your Ubuntu installed the PIP, if you don’t know or it is not installed, you should follow this tutorial to get it.

After that, we can now install OpenCV Pip on your Ubuntu 22.04 by running this command:

pip install opencv-python

When the installation finishes, you can check it has been installed correctly by running this command through Python IDLE or terminal:

python3 -c "import cv2"

If it returns any errors, It means the installation has failed, you need to fix it before using it, else, you can now start projects you want.

Ubuntu OpenCV install using Anaconda

Anaconda is a Python programming language distribution for scientific computing that promises to make package management and deployment easier. Data-science packages for any Linux system are included in the distribution. With Anaconda, we can easily install OpenCV Conda with some simple commands. You can install Anaconda by following its guide. After installing, you can now use Conda to install OpenCV by executing these commands:

conda install -c conda-forge opencv

Install OpenCV Conda on Ubuntu 22.04

Install OpenCV Ubuntu from source code

At I mentioned above, OpenCV is an open-source library, so you can install OpenCV ubuntu through source code. This is an advanced method, which you should consider before following this option. But, installing from source code, means you can get the latest versions of OpenCV. To do it, you will need to perform these commands:

sudo apt-get install cmake gcc g++ python3-dev python3-numpy libavcodec-dev libavformat-dev libswscale-dev libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev libgtk2.0-dev libgtk-3-dev

After installing the build tools, you now need to download the OpenCV source:

git clone https://github.com/opencv/opencv.git

In the current working directory, it will create the subdirectory opencv Now, navigate to the opencv folder you downloaded earlier. Make a new folder called build and go to it.

From the build directory, we can now use ubuntu OpenCV install with these commands:

sudo cmake ../
sudo make

Uninstall OpenCV Ubuntu

After using OpenCV, you now think that you need to use SimpleCV or TouchCV instead of it, you will need to uninstall OpenCV on your Ubuntu 22.04 through these commands:

  • If you installed this CV application through Ubuntu Repository or APT, you should run this:
sudo apt remove python-opencv; sudo apt purge python-opencv
  • If you installed it with the help of Python PIP, you can run this command:
pip uninstall opencv-python
  • If you used conda install OpenCV, this Linux command can help you:
conda remove opencv

Conclusion

In this tutorial, I showed you some ways to install OpenCV Ubuntu 22.04, this one also helps you to uninstall OpenCV on Linux too. If you need any help, don’t hesitate to contact me by commenting below this tutorial.