Opencv-python: Latest version (4.3.0.38) fails on import - libGL.so.1: cannot open shared object file: No such file or directory

Created on 10 Aug 2020  路  8Comments  路  Source: opencv/opencv-python

Expected behaviour

Since the update last night to version 4.3.0.38, our builds are breaking. On 4.3.0.36 all runs fine

Actual behaviour

On python:3.8 docker images (and also python:3.7), we are getting this

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import cv2
  File "/usr/local/lib/python3.8/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

Steps to reproduce

Dockerfile

FROM python:3.8

WORKDIR /app
COPY . .

RUN pip install -r requirements.txt

CMD ["python", "test.py"]

requirements.txt


test.py

import cv2
print('success')

Run docker build -t opencv-test:latest .
And then docker run opencv-test:latest

You will get the error written above.

If the version is set to 4.3.0.38 you will get:
success

Issue submission checklist
  • [x] This is not a generic OpenCV usage question (looking for help for coding, other usage questions, homework etc.)

  • [x] I have read the README of this repository and understand that this repository provides only an automated build toolchain for OpenCV Python packages (there is no actual OpenCV code here)


  • [x] The issue is related to the build scripts in this repository, to the pre-built binaries or is a feature request (such as "please enable this additional dependency")

  • [x] I'm using the latest version of opencv-python

Most helpful comment

Are you running in headless environment (most likely you are since you are using Docker)? Asking because I have no idea what's in your requirements.txt or what's the environment (I can't run your Dockerfile). If you are running in headless (server) environment, then always install opencv-python-headless to avoid pulling in the whole X11 stack.

If you need GUI functions (cv2.imshow etc.), then install opencv-python and install also libgl1-mesa-glx. The new release uses Qt 5.15 which depends on some system libraries which are whitelisted by auditwheel manylinux2014 policy: https://github.com/pypa/auditwheel/blob/master/auditwheel/policy/policy.json

All 8 comments

Are you running in headless environment (most likely you are since you are using Docker)? Asking because I have no idea what's in your requirements.txt or what's the environment (I can't run your Dockerfile). If you are running in headless (server) environment, then always install opencv-python-headless to avoid pulling in the whole X11 stack.

If you need GUI functions (cv2.imshow etc.), then install opencv-python and install also libgl1-mesa-glx. The new release uses Qt 5.15 which depends on some system libraries which are whitelisted by auditwheel manylinux2014 policy: https://github.com/pypa/auditwheel/blob/master/auditwheel/policy/policy.json

Are you running in headless environment (most likely you are since you are using Docker)? Asking because I have no idea what's in your requirements.txt or what's the environment (I can't run your Dockerfile). If you are running in headless (server) environment, then always install opencv-python-headless to avoid pulling in the whole X11 stack.

If you need GUI functions (cv2.imshow etc.), then install opencv-python and install also libgl1-mesa-glx. The new release uses Qt 5.15 which depends on some system libraries which are whitelisted by auditwheel manylinux2014 policy: https://github.com/pypa/auditwheel/blob/master/auditwheel/policy/policy.json

Thanks, opencv headless does the trick. Yes there's no gui

This issue has broken all our docker builds.
FROM python:3.7.4-slim-stretch

While opencv-python-headless is a good fix, I would expect a change like this to come with a larger version bump then just 'revision'. opencv-python is often pulled as a sub-dependency anyway (and requirements.txt doesn't support easily uninstalling opencv to replace with opencv-headless)

cc @skvark, because I think you are about to receive many message about this, perhaps something can be done with the versions.

Thanks for the suggestion and I understand your frustration. The packages follow OpenCV versioning and I'm not planning to change that. I don't guarantee compatibility between different releases (nor does OpenCV afaik). The latest release was made to fix major problem specifically with the GUI methods.

The headless packages are same as in previous release. Therefore this issue affects mostly server environments where opencv-python has been used instead of opencv-python-headless. Desktops have usually all X11 related dependencies already available. It's quite surprising for me to see that opencv-python is used in Dockerfiles so much despite the fact that it requires a huge stack of X11 dependencies to work which make the resulting containers/images much larger than they need to be. Maybe I need to explain this more clearly in the readme.

I realize that the maintainers are correctly following manylinux recommendations, but it looks like, in reality, common docker images such as python:3.9 (based on debian, I think) are missing this dynamic library file. Hence from an installing user's perspective, trying to use these wheels on most stock Python docker images doesn't work out of the box, and the fix isn't mentioned in the readme.

You should use the -headless variants in Docker as explained in the readme since the -headless packages do not depend on graphical user interface components such as Qt / libGL.so.

You should use the -headless variants in Docker as explained in the readme since the -headless packages do not depend on graphical user interface components such as Qt / libGL.so.

That's not really a viable option given that pip does not support 'near equivalent' package requirements.

A real-world example:

camelot-py depends on opencv-python, which pip is not smart enough to substitute with opencv-python-headless. So, installing the headless version does not stop pip from attempting to install the non-headless version if it's an implicit dependency.

This is hinted at by CoenraadS above.

While opencv-python-headless is a good fix, I would expect a change like this to come with a larger version bump then just 'revision'. opencv-python is often pulled as a sub-dependency anyway (and requirements.txt doesn't support easily uninstalling opencv to replace with opencv-headless)

Minimal and reproducible example:

$ docker container run -it --rm --entrypoint=bash python:3.9
root@79a5d5e0f6e1:/# echo 'opencv-python' > requirements.txt
root@79a5d5e0f6e1:/# python3 -m pip install opencv-python-headless
Collecting opencv-python-headless
  Downloading opencv_python_headless-4.5.3.56-cp39-cp39-manylinux2014_x86_64.whl (37.1 MB)
     |鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 37.1 MB 7.0 MB/s 
Collecting numpy>=1.19.3
  Downloading numpy-1.21.2-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (15.8 MB)
     |鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 15.8 MB 6.3 MB/s 
Installing collected packages: numpy, opencv-python-headless
Successfully installed numpy-1.21.2 opencv-python-headless-4.5.3.56

root@79a5d5e0f6e1:/# python3 -m pip install -r requirements.txt 
Collecting opencv-python
  Downloading opencv_python-4.5.3.56-cp39-cp39-manylinux2014_x86_64.whl (49.9 MB)
     |鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅鈻堚枅| 49.9 MB 6.5 MB/s 
Requirement already satisfied: numpy>=1.19.3 in /usr/local/lib/python3.9/site-packages (from opencv-python->-r requirements.txt (line 1)) (1.21.2)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.5.3.56

root@79a5d5e0f6e1:/# python3 -c 'import cv2'
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
Was this page helpful?
0 / 5 - 0 ratings