How do I install opencv using pip?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
OpenCV for Python is installed via pip with pip install opencv-python. There are four PyPI packages available: opencv-python (main modules only), opencv-python-headless (no GUI, for servers), opencv-contrib-python (main + extra modules), and opencv-contrib-python-headless (extra modules, no GUI). For most use cases, pip install opencv-python is sufficient. For server environments or Docker containers without a display, use the -headless variant to avoid unnecessary GUI dependencies.
Basic Installation
This installs the main OpenCV modules including cv2.imread(), cv2.VideoCapture(), cv2.resize(), and most commonly used functions.
Choosing the Right Package
| Package | GUI Support | Extra Modules | Use Case |
opencv-python | Yes | No | Desktop development |
opencv-python-headless | No | No | Servers, Docker |
opencv-contrib-python | Yes | Yes | Research, SIFT/SURF |
opencv-contrib-python-headless | No | Yes | Server-side research |
Only install one of these four packages. Installing multiple causes conflicts.
Virtual Environment (Recommended)
Installing a Specific Version
Verify Installation
Troubleshooting Common Installation Issues
ImportError: libGL.so.1
Conflict Between Multiple OpenCV Packages
Permission Errors
Python Version Compatibility
Docker Installation
Using with Jupyter Notebook
Common Pitfalls
- Installing multiple OpenCV packages simultaneously: Having both
opencv-pythonandopencv-contrib-pythoninstalled causes import errors or unpredictable behavior. Uninstall all variants before installing the one you need. - Using
opencv-pythonon a headless server: The full package requires GUI libraries (libGL,libSM, etc.) that are not available on servers. Useopencv-python-headlessto avoidImportError: libGL.so.1errors. - Importing as
opencvinstead ofcv2: The Python module name iscv2, notopencv. The pip package nameopencv-pythondoes not match the import name. - Missing NumPy dependency: OpenCV depends on NumPy. While pip usually installs it automatically, some environments (like minimal Docker images) may need
pip install numpyseparately if the dependency resolution fails. - Building from source unnecessarily: Pre-built wheels from PyPI work on most platforms (Windows, macOS, Linux x86_64). Building from source is only needed for ARM architectures, custom build flags, or CUDA GPU support.
Summary
- Install with
pip install opencv-pythonfor most use cases - Use
opencv-python-headlessfor servers, Docker, and CI/CD environments - Use
opencv-contrib-pythonwhen you need extra modules like SIFT, SURF, or ArUco markers - Only install one OpenCV package at a time — uninstall others first
- The Python import name is
cv2, notopencv - Fix
libGL.so.1errors by installing system libraries or switching to the headless package
Related reading
- How do I install Python OpenCV through Conda?
- How do I pass an OpenCV Mat into a C Tensorflow graph?
- How do I read image data from a URL in Python?
- How do I resize the UIImage to reduce upload image size
- How do I install pip on macOS or OS X?
- How do I install pip on macOS or OS X?
- How do I sample a line across a blob at a perpendicular angle? in Python/OpenCV unless you suggest switching to something else
- How do you load, label, and feed jpeg data into Tensorflow?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.