[Solved] Importerror: libgl.so.1: cannot open shared object file: no such file or directory

If you are using cv2 or opencv-python and then building the application through docker you will get an importerror: libgl.so.1: cannot open shared object file: no such file or directory.

In this tutorial we will check what is importerror: libgl.so.1: cannot open shared object file: no such file or directory and how to fix this error with examples.

Importerror: libgl.so.1: cannot open shared object file: no such file or directory

If your application has a dependency on cv2 or opencv-python and if you are trying to build docker containers using the images such as python:3.9-slim, python:buster, etc, and if you import cv2 you will get the below error.

 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

How to fix Importerror: libgl.so.1: cannot open shared object file: no such file or directory?

There are multiple ways to fix this issue, let us look into each of these in detail.

Solution 1: Install cv2 dependencies 

The easier way to fix the issue is you can update the packages and install the additional dependencies that are required for cv2 to run properly.

These dependencies will mostly be present in your local machine and hence the application runs without any issue when you perform a docker build using python based images you will get this error.

Just add the below lines into your DockerFile to fix the issue. This will ensure to update the packages and install the additional packages which are required to run cv2.

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

Solution 2: Install python3-opencv package

If you don’t want to manually install the dependency, the better way to resolve the issue is to install the python3-opencv

This will ensure that all the related system dependencies are installed correctly while building the docker containers.

Add the below lines into your DockerFile to install the python3-opencv and then install the other packages which are there in requirements.txt.

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

Solution 3: Install opencv-python-headless

Instead of opencv-python, you could install opencv-python-headless which includes a a precompiled binary wheel with no external dependencies (other than numpy), and is intended for headless environments like Docker.

When compared to python3-opencv this is a much more lightweight package and reduces the docker image size by 700MB.

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

Solution 4: Install only the dependency (libgl1)

All the above solutions will install the cv2 dependencies and thus increasing the image size. 

If you do not want to increase the image size then you can resolve the issue by installing the libgl1 dependency as shown below. This is not a recommended solution but it does work if you are just getting the Importerror: libgl.so.1

apt-get update && apt-get install libgl1

Conclusion

The importerror: libgl.so.1: cannot open shared object file: no such file or directory mainly occurs because of the missing dependencies of cv2 when you build the application with Docker. These dependencies will be present in the local instance and thus you will not get this error.

We can resolve this error by installing the additional dependencies which are required by cv2 or we can just use the one of the packages such as python3-opencv, opencv-python-headless which will install all the related dependencies and resolving the error.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like