ImportError: libGL.so.1: cannot open shared object file: No such file or directory

PythonUbuntu 14.04

Python Problem Overview


I am trying to run cv2, but when I try to import it, I get the following error:

ImportError: libGL.so.1: cannot open shared object file: No such file or directory

The suggested solution online is installing

apt install libgl1-mesa-glx

but this is already installed and the latest version.

NB: I am actually running this on Docker, and I am not able to check the OpenCV version. I tried importing matplotlib and that imports fine.

Python Solutions


Solution 1 - Python

Add the following lines to your Dockerfile:

RUN apt-get update
RUN apt-get install ffmpeg libsm6 libxext6  -y

These commands install the cv2 dependencies that are normally present on the local machine, but might be missing in your Docker container causing the issue.

Solution 2 - Python

This is a little bit better solution in my opinion. Package python3-opencv includes all system dependencies of OpenCV.

RUN apt-get update && apt-get install -y python3-opencv
RUN pip install opencv-python

Solution 3 - Python

Even though the above solutions work. But their package sizes are quite big. libGL.so.1 is provided by package libgl1. So the following code is sufficient.

apt-get update && apt-get install libgl1

Solution 4 - Python

Try installing opencv-python-headless python dependency instead of opencv-python. That includes a precompiled binary wheel with no external dependencies (other than numpy), and is intended for headless environments like Docker. This saved almost 700mb in my docker image compared with using the python3-opencv Debian package (with all its dependencies).

The package documentation discusses this and the related (more expansive) opencv-contrib-python-headless pypi package.

Example reproducing the ImportError in the question
# docker run -it python:3.9-slim bash -c "pip -q install opencv-python; python -c 'import cv2'"
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/cv2/__init__.py", line 5, in <module>
    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
# docker run -it python:3.9-slim bash -c "pip -q install opencv-python-headless; python -c 'import cv2'"
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

Solution 5 - Python

Put this in the Dockerfile

RUN apt-get update
RUN apt install -y libgl1-mesa-glx

Before the line

COPY requirements.txt requirements.txt

For example

......

RUN apt-get update
RUN apt install -y libgl1-mesa-glx

COPY requirements.txt requirements.txt

......

Solution 6 - Python

For me, the only WA that worked is following:

# These are for libGL.so issues
# RUN apt-get update
# RUN apt install libgl1-mesa-glx
# RUN apt-get install -y python3-opencv
# RUN pip3 install opencv-python
RUN pip3 install opencv-python-headless==4.5.3.56

Solution 7 - Python

If you're on CentOS, RHEL, Fedora, or other linux distros that use yum, you'll want:

sudo yum install mesa-libGL -y

Solution 8 - Python

In my case it was enough to do the following which also saves space in comparison to above solutions

RUN apt-get update && apt-get install -y --no-install-recommends \
        libgl1 \
        libglib2.0-0 \

Solution 9 - Python

I was getting the same error when I was trying to use OpenCV in the GCP Appengine Flex server environment. Replacing "opencv-python" by "opencv-python-headless" in the requirements.txt solved the problem.

The OpenCV documentation talks about different packages for desktop vs. Server (headless) environments.

Solution 10 - Python

had the same issue on centos 8 after using pip3 install opencv on a non gui server which is lacking all sorts of graphics libraries.

dnf install opencv

pulls in all needed dependencies.

Solution 11 - Python

For a raspberry pi, put this , work for me :

sudo apt-get install ffmpeg libsm6 libxext6  -y

Solution 12 - Python

Here is the solution you need:

pip install -U opencv-python
apt-get upgrade
apt update && apt install -y libsm6 libxext6 ffmpeg libfontconfig1 libxrender1 libgl1-mesa-glx

Solution 13 - Python

For me, the problem was related to proxy setting. For pypi, I was using nexus mirror to pypi, for opencv nothing worked. Until I connected to a different network.

Solution 14 - Python

I met this problem while using cv2 in a docker container. I fixed it by:

pip install opencv-contirb-python

install opencv-contirb-python rather than opencv-python.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRyan View Question on Stackoverflow
Solution 1 - PythonTushar KolheView Answer on Stackoverflow
Solution 2 - PythonAndrej ChudyView Answer on Stackoverflow
Solution 3 - PythonAwanish Kumar GolwaraView Answer on Stackoverflow
Solution 4 - PythonLucas WimanView Answer on Stackoverflow
Solution 5 - PythonEpic ChenView Answer on Stackoverflow
Solution 6 - Pythonsoumeng78View Answer on Stackoverflow
Solution 7 - PythonMatt MessersmithView Answer on Stackoverflow
Solution 8 - PythonMaxiView Answer on Stackoverflow
Solution 9 - PythonBitSpectrumView Answer on Stackoverflow
Solution 10 - PythonJens TimmermanView Answer on Stackoverflow
Solution 11 - Pythonibra ndiayeView Answer on Stackoverflow
Solution 12 - PythonAnkurView Answer on Stackoverflow
Solution 13 - PythonKarel MacekView Answer on Stackoverflow
Solution 14 - PythonYellowView Answer on Stackoverflow