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

ImportError: libGL.so.1: cannot open shared object file: No such file or directory is an error which occurs when you try to import and run cv2 because your cv2 dependencies are missing.

This post is my attempt to explain to you why this error occurs and how you can solve it, I will also include multiple solutions that could be considered as alternative fixes to the error.

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

This error occurs when you try to import and run cv2 because your cv2 dependencies are missing.

The error message should be the exact as this one or at least very similar in order to avoid all types of confusion.

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

In the sections bellow we will explain the root of the error more and propose some possible fixes.

Solution 1 : install python3-opencv

The simplest and easiest solution is to install python3-opencv. This package already contains all the OpenCV dependencies.

You can do that by using the commands bellow.

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

The solution should have solved the issue and the error should be gone. If that is not the case, please consider using or trying the solution bellow.

Solution 2 : edit your Dockerfile.

The solution is for people who use docker.

Sometimes, your Docker container does not have the cv2 dependencies by default. You can use the lines bellow in order to add those dependencies on docker, just by adding the lines to your Dockerfile.

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

I hope the fix above fixed your problem, good luck with the scripts to come.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

This is the end of our article, I hope the solutions I presented worked for you, Learning Python is a fun journey, do not let the errors discourage you. Keep coding and cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/