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

I am trying to run cv2, but when I try to import it, I get the following error: The suggested solution online is installing but this is already installed and the latest version. NB: I am actually running this on Docker, and I am not able to check the OpenCV version. I tried importing matplotlib

I am trying to run cv2, but when I try to import it, I get the following error:

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

The suggested solution online is installing

apt install libgl1-mesa-glx 

but this is already installed and the latest version.

NB: I am actually running this on Docker, and I am not able to check the OpenCV version. I tried importing matplotlib and that imports fine.

3

22 Answers

Add the following lines to your Dockerfile:

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

These commands install the cv2 dependencies that are normally present on the local machine, but might be missing in your Docker container causing the issue.

[minor update on 20 Jan 2022: as Docker recommends, never put RUN apt-get update alone, causing cache issue]

8

Even though the above solutions work. But their package sizes are quite big. libGL.so.1 is provided by package libgl1. So the following code is sufficient.

apt-get update && apt-get install libgl1 
6

Try installing opencv-python-headless python dependency instead of opencv-python. That includes a precompiled binary wheel with no external dependencies (other than numpy), and is intended for headless environments like Docker. This saved almost 700mb in my docker image compared with using the python3-opencv Debian package (with all its dependencies).

The package documentation discusses this and the related (more expansive) opencv-contrib-python-headless pypi package.

Example reproducing the ImportError in the question

# 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 # docker run -it python:3.9-slim bash -c "pip -q install opencv-python-headless; 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 
7

This is a little bit better solution in my opinion. Package python3-opencv includes all system dependencies of OpenCV.

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

For me, the only WA that worked is following:

# These are for libGL.so issues # RUN apt-get update # RUN apt install libgl1-mesa-glx # RUN apt-get install -y python3-opencv # RUN pip3 install opencv-python RUN pip3 install opencv-python-headless==4.5.3.56 
1

If you're on CentOS, RHEL, Fedora, or other linux distros that use yum, you'll want:

sudo yum install mesa-libGL -y 
3

In my case it was enough to do the following which also saves space in comparison to above solutions

RUN apt-get update && apt-get install -y --no-install-recommends \ libgl1 \ libglib2.0-0 \ 
3

Put this in the Dockerfile

RUN apt-get update RUN apt install -y libgl1-mesa-glx 

Before the line

COPY requirements.txt requirements.txt 

For example

...... RUN apt-get update RUN apt install -y libgl1-mesa-glx COPY requirements.txt requirements.txt ...... 
1

Use opencv-python-headless if you're using docker or in server environment.

I was getting the same error when I was trying to use OpenCV in the GCP Appengine Flex server environment. Replacing "opencv-python" by "opencv-python-headless" in the requirements.txt solved the problem.

The OpenCV documentation talks about different packages for desktop vs. Server (headless) environments.

Here is the solution you need:

pip install -U opencv-python apt update && apt install -y libsm6 libxext6 ffmpeg libfontconfig1 libxrender1 libgl1-mesa-glx 
2

Watch the error message carefully

I got error:

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

which is very similar to:

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

Basically libGL become libEGL (extra E)

In this case you must install libegl1 and not libgl1:

apt update && apt install libegl1 -y 

I met this problem while using cv2 in a docker container. I fixed it by:

pip install opencv-contrib-python 

install opencv-contrib-python rather than opencv-python.

2

had the same issue on centos 8 after using pip3 install opencv on a non gui server which is lacking all sorts of graphics libraries.

dnf install opencv

pulls in all needed dependencies.

"installing opencv-python-headless instead of opencv-python" this works in my case! I was deploying my website to Azure and pop up this exception: ImportError: libGL.so.1: cannot open shared object file: No such file or directory then I uninstall the opencv-python package, install the later one, freeze the requirements and then deploy it again, then problem solved.

For a raspberry pi, put this , work for me :

sudo apt-get install ffmpeg libsm6 libxext6 -y 

For me, the problem was related to proxy setting. For pypi, I was using nexus mirror to pypi, for opencv nothing worked. Until I connected to a different network.

This command works for me.

apt-get install libgl1 

I am also installing OpenCV through pip, in a Docker container. I use a Python3.11 base image (Ubuntu host on Jetson). After installing libgl1-mesa-glx, like you, I was prompted with another dependency error. The suggested solution from @Tushar Kolhe worked for me.

Though if you don't want to install the other packages suggested therein, I found installing libopencv-dev also works. Optionally, you might want to include two other lines: one to clean up the package manager's cache, and one to remove package lists, possibly reducing the resulting Docker image size, depending on what other packages you hope to install:

RUN apt update && \ apt install -y libopencv-dev && \ apt clean && \ rm -rf /var/lib/apt/lists/* 

In rocky linux 9 i resolved the error using command dnf install mesa-libGLU

I got the same issue on Ubuntu desktop, and none of the other solutions worked for me.

libGL.so.1 was correctly installed but for some reason Python wasn’t able to see it:

$ ldconfig -p | grep libGL.so.1 libGL.so.1 (libc6,x86-64) => /lib/x86_64-linux-gnu/libGL.so.1 

The only solution that worked was to force it in LD_LIBRARY_PATH. Add the following in your ~/.bashrc then run source ~/.bashrc or restart your shell:

export LD_LIBRARY_PATH="/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH" 

I understand that LD_LIBRARY_PATH is bad but for me this is the only solution that works.

This is how your docker files should like for building image on Linux 64

# Use Python 3.10 from the respective version as a parent image FROM python:3.10 # Set the working directory to /app WORKDIR /app # Copy the contents of the your_code_directory into the container at /app COPY your_code_directory /app # Copy the requirements.txt file into the container at /app/requirements.txt COPY requirements.txt /app/requirements.txt # These are folder outside app if there is requirement for your application COPY config /config COPY your_folder /your_folder # For fixing ImportError: libGL.so.1: cannot open shared object file: No such file or directory RUN apt-get update RUN apt install -y libgl1-mesa-glx # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Make port 8080 available to the world outside this container EXPOSE 8080 # Run app.py when the container launches CMD ["python", "app.py"] 

You should successfully be able to build the docker image

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJobG5rYWiDcnyOoqSpp6KpsrO%2BzqtkpaGSnLluv85maGabkaO7sMCMqKeepl2otaK%2BxJ1kqJqamrC1ecWio55lnqR6tMHCoWSfoZyaerC%2BjJ2gq52Tqbw%3D

 Share!