Beginner
Installing OpenCV
Get OpenCV up and running on your machine in minutes with pip, conda, or a custom build from source.
Quick Install with pip
The easiest way to install OpenCV is using pip. There are two packages available:
Terminal - pip install
# Main package (most users need this)
pip install opencv-python
# Full package with contrib modules (extra algorithms)
pip install opencv-contrib-python
Which package? Use
opencv-python for standard functionality. Use opencv-contrib-python if you need extra algorithms like SIFT, SURF, or advanced tracking. Don't install both - they conflict.Install with Conda
Terminal - conda install
# From conda-forge (recommended)
conda install -c conda-forge opencv
# Create a dedicated environment
conda create -n cv python=3.11 opencv -c conda-forge
conda activate cv
Verify Installation
Python - Verify OpenCV
import cv2
print(f"OpenCV version: {cv2.__version__}")
print(f"Build info: {cv2.getBuildInformation()[:200]}")
# Quick test: create a blank image
import numpy as np
img = np.zeros((100, 100, 3), dtype=np.uint8)
cv2.rectangle(img, (10, 10), (90, 90), (0, 255, 0), 2)
print("OpenCV is working correctly!")
Build from Source (Advanced)
Building from source gives you full control over which modules are included and enables CUDA GPU acceleration:
Terminal - Build from source (Linux)
# Install dependencies
sudo apt update
sudo apt install cmake g++ wget unzip libgtk-3-dev
# Download and build
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
unzip opencv.zip
mkdir -p build && cd build
cmake ../opencv-4.x -DCMAKE_BUILD_TYPE=Release \
-DWITH_CUDA=ON -DOPENCV_DNN_CUDA=ON
cmake --build . --parallel $(nproc)
sudo make install
Common Installation Issues
- ImportError: libGL.so.1: Install
libgl1-mesa-glxon Ubuntu or useopencv-python-headlessfor server environments. - Package conflicts: Never install both
opencv-pythonandopencv-contrib-python. Uninstall one first. - No GUI on servers: Use
pip install opencv-python-headlessfor Docker containers and headless servers.
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX