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

I am trying to deploy my flask app. And here is what i have in my Procfile

web: gunicorn --bind :$PORT app:app

Why am i getting this error when deploying?

ImportError: libGL.so.1: cannot open shared object file: No such file or directory
[2024-07-23 19:48:28 +0000] [18] [INFO] Worker exiting (pid: 18)
[2024-07-23 19:48:28 +0000] [1] [ERROR] Worker (pid:18) exited with code 3
[2024-07-23 19:48:28 +0000] [1] [ERROR] Shutting down: Master
[2024-07-23 19:48:28 +0000] [1] [ERROR] Reason: Worker failed to boot.
Application exited with code 3
Instance stopped
```

> My build was successiful the problem is when i am deploying the app.

Hi @Crispen_Gari, does it work locally? It seems that you should install libgl1

Hi thanks for your respose. How can i install it becuase it seems to be the problem with the opencv-python in my project and i can’t run pip install libgl1 because it doesn’t exists.

To answer your question, yes the project is working properly on my computer. and it is even building successfuly.

It seems that switching from opencv-python to opencv-python-headless should help here. Import stays the same, only package name changes.

You can read more about available options here opencv-python · PyPI

I have tried that, after doing some research but it seems not to work. Maybe i will try the dockerfile deployment instead

What didn’t work? I’ve tried it and it worked for me. I only added opencv-python-headless to requirements.txt.

Here is my requirements.txt file

absl-py==2.1.0
altgraph==0.17.4
astunparse==1.6.3
backports.tarfile==1.2.0
beautifulsoup4==4.12.3
blinker==1.8.2
build==1.2.1
certifi==2024.7.4
charset-normalizer==3.3.2
click==8.1.7
colorama==0.4.6
deepface==0.0.92
docutils==0.21.2
filelock==3.15.4
fire==0.6.0
Flask==3.0.3
Flask-Cors==4.0.1
flatbuffers==24.3.25
gast==0.6.0
gdown==5.2.0
google-pasta==0.2.0
grpcio==1.65.1
gunicorn==22.0.0
h5py==3.11.0
idna==3.7
importlib_metadata==8.1.0
itsdangerous==2.2.0
jaraco.classes==3.4.0
jaraco.context==5.3.0
jaraco.functools==4.0.1
Jinja2==3.1.4
keras==3.4.1
keyring==25.2.1
libclang==18.1.1
Markdown==3.6
markdown-it-py==3.0.0
MarkupSafe==2.1.5
mdurl==0.1.2
ml-dtypes==0.3.2
more-itertools==10.3.0
mtcnn==0.1.1
namex==0.0.8
nh3==0.2.18
numpy==1.26.4
opencv-python-headless==4.10.0.84
opt-einsum==3.3.0
optree==0.12.1
packaging==24.1
pandas==2.2.2
pefile==2023.2.7
pillow==10.4.0
pkginfo==1.10.0
protobuf==4.25.3
Pygments==2.18.0
pyinstaller==6.9.0
pyinstaller-hooks-contrib==2024.7
pyproject_hooks==1.1.0
PySocks==1.7.1
python-dateutil==2.9.0.post0
pytz==2024.1
pywin32-ctypes==0.2.2
readme_renderer==44.0
requests==2.32.3
requests-toolbelt==1.0.0
retina-face==0.0.17
rfc3986==2.0.0
rich==13.7.1
six==1.16.0
soupsieve==2.5
tensorboard==2.16.2
tensorboard-data-server==0.7.2
tensorflow==2.16.1
tensorflow-intel==0.0.1
tensorflow-io-gcs-filesystem==0.37.1
termcolor==2.4.0
tf_keras==2.16.0
tqdm==4.66.4
twine==5.1.1
typing_extensions==4.12.2
tzdata==2024.1
urllib3==2.2.2
Werkzeug==3.0.3
wrapt==1.16.0
zipp==3.19.2

Thx for sharing the requirements.txt. It looks like the deepface package and its dependencies are requiring python-opencv directly. You can see it for example by using pipdeptree tool.

$ pipdeptree -fl 
deepface==0.0.92
  mtcnn==0.1.1
    opencv-python==4.10.0.84
  opencv-python==4.10.0.84
  retina-face==0.0.17
    opencv-python==4.10.0.84
opencv-python-headless==4.10.0.84

I don’t think there is an easy way to overwrite this. I guess the best option for now would be using Dockerfile and installing libgl1 package.

Here is my Dockerfile

FROM python:3-alpine AS builder
 
WORKDIR /app
 
RUN python3 -m venv venv
ENV VIRTUAL_ENV=/app/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
 
COPY requirements.txt .

RUN apt-get update && apt-get install -y libgl1-mesa-glx
RUN pip install --upgrade pip
RUN pip install opencv-python==4.3.0.38
RUN pip install -r requirements.txt

# Stage 2
FROM python:3-alpine AS runner
 
WORKDIR /app
 
COPY --from=builder /app/venv venv
COPY . .

ENV VIRTUAL_ENV=/app/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV FLASK_APP=app/app.py
 
EXPOSE 8080
 
CMD ["gunicorn", "--bind" , ":8080", "--workers", "2", "app:app"]

Now i am getting the error saying:

11 | >>> RUN apt-get update && apt-get install -y libgl1-mesa-glx
  12 |     RUN pip install --upgrade pip
  13 |     RUN pip install opencv-python==4.3.0.38
--------------------
error: failed to solve: process "/bin/sh -c apt-get update && apt-get install -y libgl1-mesa-glx" did not complete successfully: exit code: 127

What am i doing wrong here?

I’m not alpine expert

Here is a part of Debian based Python image which works for me:

FROM python:3.12-slim
 
WORKDIR /app
 
COPY requirements.txt .

RUN apt-get update 
RUN apt-get install -y libgl1 libglib2.0-0
RUN pip install -r requirements.txt

I’m not sure if multistaged build will help here so I’m not using it.

Thank you for the response however i am still getting the same error. When deploying after the build succeded.

ImportError: libGL.so.1: cannot open shared object file: No such file or directory
[2024-07-30 17:52:21 +0000] [6] [INFO] Worker exiting (pid: 6)
[2024-07-30 17:52:23 +0000] [1] [ERROR] Worker (pid:6) exited with code 3
[2024-07-30 17:52:53 +0000] [1] [ERROR] Shutting down: Master
[2024-07-30 17:52:53 +0000] [1] [ERROR] Reason: Worker failed to boot.
Instance stopped

Here is my new Dockerfile:

FROM python:3.12-slim AS builder
 
WORKDIR /app
 
COPY requirements.txt .

RUN python3 -m venv venv
ENV VIRTUAL_ENV=/app/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
 
RUN apt-get update 
RUN apt-get install -y libgl1 libglib2.0-0
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

# Stage 2
FROM python:3.12-slim AS runner
 
WORKDIR /app
 
COPY --from=builder /app/venv venv
COPY . .

ENV VIRTUAL_ENV=/app/venv
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
ENV FLASK_APP=app/app.py
 
EXPOSE 8080
 
CMD ["gunicorn", "--bind" , ":8080", "--workers", "2", "app:app"]

Are you sure that you switched to Dockerfile?

Does it still fail on import or late,r on some other operation?

1 Like

Thanks a lot, did the tick for me. I have the Dockerfile but using wrong builder. I was about to become crazy.

Thanks @Lukasz_Oles

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.