ChatGPT解决这个技术问题 Extra ChatGPT

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

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.

How did you install it? pip install opencv-python? Could be another issue. See github.com/matplotlib/matplotlib/issues/9954. Try creating a virtualenv and test it there. Can you share your code snippet where its throwing the exception?

w
wovano

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.


what does this do and why do I need it for cv2?
I got an error: "E: Repository 'deb.debian.org/debian buster-updates InRelease' changed its 'Suite' value from 'stable-updates' to 'oldstable-updates'". I fixed it by using: sudo apt-get update --allow-releaseinfo-change
This solution worked for me and then stopped working during Docker build. I changed my base image to python:3.8-slim-buster as suggested by @SuryaTej, which worked at first, but then stopped working as well. I thought maybe it was a network issue on my side, but the build fails more often than not. > #8 254.6 Get:197 deb.debian.org/debian buster/main amd64 xdg-user-dirs amd64 0.17-2 [53.8 kB] > #8 254.6 E: Failed to fetch deb.debian.org/debian/pool/main/libp/libpng1.6/… Hash Sum mismatch > #Last modification reported: Mon, 08 Apr 2019 10:11:25
the author has mentioned in the comments he is doing this in docker. This command just installs the cv2 dependencies that are present on the local machine normally but might be missing in your docker causing the issue @wovano
Per an answer below can use opencv-python-headless if you don't reuquire GUI functions, see here: github.com/opencv/opencv-python/issues/370
A
Awanish Kumar Golwara

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

A
Andrej Chudy

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

I'm using python:buster. the solution above didn't work for me: ffmpeg seems to be deprecated and I still had write errors. apt-get install -y python3-opencv did the trick. thanks
This gets just about every lib you could want. I didn't try it, but is pip install still needed?
@MarkCarpenterJr - Using pip will help you to keep the packed version in a defined state. If you use the version of the operating system, you might end up with changing dependencies if your distro is changing packages. This is probably not what you want.
This is absolutely the most stable approach. The ffmpeg issue is breaking the leading answer.
L
Lucas Wiman

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

s
soumeng78

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

thanks. And don't forget opencv-contrib-python-headless if you're using contrib
M
Matt Messersmith

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

sudo yum install mesa-libGL -y

This helps me out. Thank a lot.
This helped me a lot thank you
Thanks. This was especially useful for Amazon Linux.
E
Epic Chen

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

......

M
Maxi

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 \

This takes indeed much less space. Works on ubuntu 20
B
BitSpectrum

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.


R
Rui Vieira

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

pip install opencv-contrib-python

install opencv-contrib-python rather than opencv-python.


typo in the package name, but I can't edit it
J
Jens Timmerman

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.


i
ibra ndiaye

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

sudo apt-get install ffmpeg libsm6 libxext6  -y

K
Karel Macek

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.


w
wovano

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

apt-get upgrade updates all installed packages which is completely overkill