ImportError: libGL.so.1: cannot open shared object file: No such file or directory
Fixes ImportError: libGL.so.1: cannot open shared object file: No such file or directory Use when `import cv2` fails on libGL.so.1 (or libgthread) in Docker/CI/headless Linux Not for: you actually need cv2.imshow (then install the libs, do not go headless)
How to fix ImportError: libGL.so.1: cannot open shared object file: No such file or directory
You are on a headless box and opencv-python wants GUI system libs. Two fixes: install the lib (sudo apt-get install libgl1, often plus libglib2.0-0), or switch to the headless wheel: pip install opencv-python-headless. The headless wheel is the right call for servers and Docker; it has no GUI deps at all.
The exact error
ImportError: libGL.so.1: cannot open shared object file: No such file or directoryFix it
- Headless route (recommended):
pip uninstall -y opencv-python && pip install opencv-python-headless.
Expected: import cv2 works
- Or install system libs:
sudo apt-get install -y libgl1 libglib2.0-0.
Expected: import cv2 works with the GUI wheel
- Docker: add the apt line to your Dockerfile before pip install.
Expected: container imports cv2 cleanly
When this applies
import cv2 fails on libGL.so.1 (or libgthread) in Docker/CI/headless Linux
When this does NOT apply
you actually need cv2.imshow (then install the libs, do not go headless)
Versions
Linux Docker / headless servers, opencv-python
Why it happens
opencv-python links Qt/OpenGL system libraries that slim images do not ship.
Edge cases
Never install both opencv-python and opencv-python-headless; they conflict.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.