![Complete Guide to Install OpenCV in Ubuntu](https://operavps.com/wp-content/uploads/2024/08/Complete-Guide-to-Install-OpenCV-in-Ubuntu.png)
How to Install OpenCV in Ubuntu for Python and C++
OpenCV is a powerful open-source library for real-time computer vision tasks, ideal for image and video processing, object detection, facial recognition, and more in Ubuntu.
It’s used when you need to develop applications that understand and interact with the visual world, from basic image manipulation to complex AI-driven systems.
There are two Quick and Customizing methods to Install OpenCV in Ubuntu for beginners and developers:
- Install OpenCV from the Ubuntu Repository:
sudo apt install libopencv-dev python3-opencv
- Install OpenCV from Source: Install dependencies and clone OpenCV/OpenCV Contrib repositories. Configure CMake, build, install, and verify installation for both C++ and Python.
Prerequisites to Install OpenCV in Ubuntu 20.04 & 22.04
To Install OpenCV in Ubuntu for Python and C++, your machine needs to meet all the below specifications:
- A Linux VPS running Ubuntu.
- A non-root user with
sudo
privileges. - Access to Terminal.
- A text editor (e.g., Vim or Nano, or a graphical editor like Gedit)
Method 1: Install OpenCV from Ubuntu Repositories (Quick Setup)
This method is the quickest way to get started but might not provide the latest OpenCV version or specific build options.
Step 1: Update package lists
OpenCV is available for installation from the default Ubuntu 20.04 repositories. To install OpenCV Ubuntu, run the commands below:
sudo apt update
sudo apt install libopencv-dev python3-opencv
This will install OpenCV packages.
Step 2: Verify installation
To make sure the OpenCV installation Ubuntu process was successful, run:
python3 -c "import cv2; print(cv2.__version__)"
Once the output prints the latest version, you are done here.
Method 2: Install OpenCV in Ubuntu from the Source
This method is recommended if you consider customization and the latest features.
Follow the below steps to install OpenCV in Ubuntu for Python and C++ through this method:
Step 1: Install dependencies
To install dependencies and build tools, run:
sudo apt install build-essential cmake git pkg-config libgtk-3-dev \
libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
libxvidcore-dev libx264-dev libjpeg-dev libpng-dev libtiff-dev \
gfortran openexr libatlas-base-dev python3-dev python3-numpy \
libtbb2 libtbb-dev libdc1394-22-dev libopenexr-dev \
libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev
Step 2: Clone OpenCV and OpenCV Contrib repositories:
To pass this step, type:
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
Step 3: Create build directory and configure CMake:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..
For CMake errors, refer to the official CMake documentation. Ensure correct library paths and consider using environment variables if necessary.
For GPU acceleration, consider installing CUDA and cuDNN and enabling relevant options during CMake configuration.
To speed up the compilation process, you can use the following command:
make -j<number_of_cores>
Replace <number_of_cores> with the actual number of cores on your system.
After installing OpenCV from source, you need to update the shared library cache:
sudo ldconfig
This command ensures that the system can find the newly installed OpenCV libraries.
Step 4: Build and install OpenCV:
Use the below command to let the compilation process start which may take several minutes (About an hour) depending on your system configuration:
make -j8
The -j8
flag in the make
command specifies the number of parallel jobs for building. Adjust it based on your system’s core count.
Step 5: Install OpenCV
sudo make install
Step 6: Verify installation:
To verify installation, run the commands below and check the OpenCV version:
For C++:
pkg-config --modversion opencv4
For Python:
python3 -c "import cv2; print(cv2.__version__)"
How to Update OpenCV to a Newer Version?
If installed using apt
(Method 1), update the package lists and upgrade OpenCV.
For source-built versions (Method 2), rebuild from the latest source code.
How to Uninstall OpenCV in Ubuntu
After OpenCV Ubuntu Setup is finished, you can remove it anytime you no longer need it.
According to your installation method, you can uninstall OpenCV from your Ubuntu system:
Uninstalling OpenCV Installed via apt
If you installed OpenCV using the apt package manager, you can uninstall it using the following command:
sudo apt purge libopencv-dev python3-opencv
The purge
option removes the package and its configuration files.
Uninstalling OpenCV Installed from Source
If you built OpenCV from source and installed it to /usr/local
, you can try the following steps:
Remove the build directory:
rm -rf ~/opencv_build
Uninstall using make Uninstall:
cd ~/opencv/build
sudo make uninstall
Note: This might not remove all files.
Removing Remaining Files Manually
Manually removing files can be risky. If you’re unsure, consider using a package manager like apt
for installation and removal.
However, the following command removes OpenCV manually from Ubuntu:
sudo find / -name "*opencv*" -exec rm -rf {} \;
Note: This command will remove all files containing “opencv” in their name. Use it with extreme care.
What are the best Advantages of OpenCV in Ubuntu?
Rich ecosystem: Ubuntu provides a strong foundation with essential tools and libraries for computer vision development.
Hardware acceleration: Utilize powerful GPUs for faster image processing and deep learning tasks.
Active community: Benefit from a large and supportive community for troubleshooting and knowledge sharing.
Open-source flexibility: Customize and extend OpenCV to fit specific project requirements.
Why compilation errors are shown during OpenCV installation?
Common causes include missing dependencies, incorrect CMake configuration, insufficient system resources, or compiler issues.
Check the error messages carefully for clues.
How to resolve OpenCV library path issues?
Ensure the OpenCV libraries are in the system’s library path. You might need to set environment variables or update LD_LIBRARY_PATH
.
Conclusion
This article covered two methods of Installing OpenCV in Ubuntu for Python and C++ including Install OpenCV using apt
Ubuntu and Install OpenCV from source Ubuntu.
As you reviewed, installing the packaged version from the Ubuntu repository is a quick and easy method and the second method requires more steps to install OpenCV from the source.
OpenCV empowers a wide range of applications, including object detection in images and videos, facial recognition, augmented reality, image stitching, and more.
You can now start processing images, detect objects, analyze video, and create augmented reality experiences using OpenCV in Ubuntu.