Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 tasks done
LobsterMan opened this issue Aug 10, 2020 · 11 comments

Comments

@LobsterMan
Copy link

LobsterMan commented Aug 10, 2020

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

opencv-python

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
  • This is not a generic OpenCV usage question (looking for help for coding, other usage questions, homework etc.)
  • 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)
  • 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")
  • I'm using the latest version of opencv-python
@skvark
Copy link
Member

skvark commented Aug 10, 2020

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

@LobsterMan
Copy link
Author

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

@CoenraadS
Copy link

CoenraadS commented Aug 10, 2020

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.

@skvark
Copy link
Member

skvark commented Aug 10, 2020

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.

@brsolomon-deloitte
Copy link

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.

@skvark
Copy link
Member

skvark commented Sep 7, 2021

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.

@brsolomon-deloitte
Copy link

brsolomon-deloitte commented Sep 7, 2021

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)

@brsolomon-deloitte
Copy link

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

@skvark
Copy link
Member

skvark commented Sep 12, 2021

If you need to use opencv-python in Docker images then you need to install all the GUI packages that are shipped with the equivalent desktop distribution. For example, in Ubuntu libGL.so can be installed with: sudo apt-get install -y libgl1-mesa-dev. Needed packages (there might be more than one) and install commands vary between distributions so much that it's not possible to provide any universal "fix" to the issue.

@jayantiprasad
Copy link

for me the following solved the isse on azure web app :

opencv-python-headless==4.5.3.56

in place of

opencv-python==4.5.4.60

@x2584179909
Copy link

RUN pip uninstall opencv-python -y
RUN pip install opencv-python-headless

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants