Ryan
Ryan

Reputation: 10179

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:

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

My suggested solution online is to install

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.

Upvotes: 519

Views: 827762

Answers (30)

marcfreir
marcfreir

Reputation: 65

Despite what was said, let's consider the following scenario:

Let's say you created a Docker Container, with the following setup:

Inside the .devcontainer/Dockerfile:

# Install common apt packages
RUN export DEBIAN_FRONTEND=noninteractive && \
    apt-get update &&  \
    apt install -y  build-essential \
        bzip2 \
        ca-certificates \
        cmake \
        curl \
        curl \
        git \
        htop \
        libssl-dev \
        nvtop \
        net-tools \
        pandoc \
        python3-sphinx \
        tmux \
        tree \
        unrar \
        unzip \
        vim \
        wget \
        xdot \
        python3-opencv

Just add this last line python3-opencv

And, probably, you need to update Numba, something like:

# ***************** Specific packages ********************

# System-wide python packages
RUN python -m pip install --upgrade pip setuptools pytest && \
    python -m pip install \
        black \
        ipykernel \
        kaleido>=0.2 \
        lightning>=2.1.9 \
        matplotlib>=3.7 \
        nbformat \
        numpy>=1.22 \
        pandas>=2.0 \
        plotly>=5.18 \
        seaborn>=0.11 \
        scipy>=1.10 \
        torchmetrics>=1.3.0 \
        torchvision>=0.15 \
        -U numba

For this last part, the -U numba may do the trick.

Brief explanation:

Numba will be set up along with libgl1, in this case.

Now, just rebuild the container without cache.

Keep in mind that some dependencies can be updated, so it is important to fix a version for each dependency, especially for reproducibility purposes. In this case, you can avoid future issues.

Upvotes: -1

Ahmet Ekiz
Ahmet Ekiz

Reputation: 189

I got the same error with WSL(Ubuntu 22.04) on Windows 11 in my newly installed Conda environment, while trying to train a YOLO model with Ultralytics. (I didn't get this error with Ubuntu 20.04 WSL on Windows 11)

This solution worked for me:

apt-get install -y libglib2.0-0

Solution Reference: https://github.com/ultralytics/ultralytics/issues/1270#issuecomment-1454994858

Upvotes: 0

Peter Miller
Peter Miller

Reputation: 11

Solution for conda environment

I wanted a solution that would work in a conda environment, so could not use apt-get for local installs. I tried all of the 'pip install / conda install' answers but I either had them already, or they couldn't be found, or they indicated incompatibilities.

So here's my solution: it's a bit hacky but after a wasted hour I was happy to move on. Assuming the GL libraries are working locally, or you have installed them using other answers, then you can copy them into the 'lib' directory of your conda environment.

conda env list    # Get the path of the conda environment
cd /lib64         # Or wherever the libGL.* files are held
cp -av libGL* libOpenGL* <path of conda env>/lib/

This enabled the conda environment to be used from other computers that did not have those libraries installed locally. Note that the cp -a option is critical, to preserve the symbolic links used in /lib.

Upvotes: 0

Aditya
Aditya

Reputation: 21

I faced the same issue when tried running YOLO V8 model. The issue is that, there is some missing file in your docker. Either you have to add the missing file in the docker or for temporary purposes go to terminal and paste this :

apt-get update && apt-get install libgl1

Your issue will be sorted in second. Thanks!

Upvotes: 0

mohamed alfateh
mohamed alfateh

Reputation: 31

I had the same error message , I have installed libgl1-mesa-glx using the command below and it just works fine.

sudo apt install libgl1-mesa-glx

Upvotes: 3

ZKS
ZKS

Reputation: 2876

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.

Upvotes: 5

Lenny4
Lenny4

Reputation: 1676

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

Upvotes: 8

Maxi
Maxi

Reputation: 519

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 \

Upvotes: 23

Agile Bean
Agile Bean

Reputation: 7201

Solution with conda

If you don't want to install with pip (pip install opencv-python-headless) but use conda instead, you can do so with:

conda install fastai::opencv-python-headless

Upvotes: 1

Ivan_ug
Ivan_ug

Reputation: 2625

Add the following lines to your Dockerfile:

RUN apt-get update && apt-get install -y libgl1 libglib2.0-0

These commands installed system dependencies that might be missing in your Docker container causing the issue.

My problem was caused by the imageai library, which in turn forced a dependency on opencv-python>=4.1.2 (at the time of writing this).

Upvotes: 1

peachcyz
peachcyz

Reputation: 41

"installing opencv-python-headless instead of opencv-python" this works in my case!

I was deploying my website to Azure and popped 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.

Upvotes: 4

Lucas Wiman
Lucas Wiman

Reputation: 11317

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

Upvotes: 179

Palguna Gopireddy
Palguna Gopireddy

Reputation: 81

For WSL2 use the following

sudo apt-get update 
sudo apt-get install ffmpeg
sudo apt-get install libgl1 
sudo apt-get install libsm6 
sudo apt-get install libxext6

Upvotes: 3

mucasliranda
mucasliranda

Reputation: 39

On Jupyter codespace, pip install opencv-python-headless worked for me.

Upvotes: 1

Matt Messersmith
Matt Messersmith

Reputation: 13767

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

sudo yum install mesa-libGL -y

In Arch this maps to extra/mesa:

sudo pacman -S mesa-libgl

Upvotes: 42

SetUp
SetUp

Reputation: 9

I also had this issue. I am not even using docker, I used requirements.txt and even after adding open-cv-headless, it didn't worked for me. I changed my hosting platform to render.com and it worked.

NB: if you are using Docker and wants to switch to headless version of oprn cv, use version 4.8.1.78 or lower. The updated one is not working as of January 2024.

Upvotes: 1

Arn
Arn

Reputation: 69

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/*

Upvotes: 3

Dong Xie
Dong Xie

Reputation: 31

This command works for me.

apt-get install libgl1 

Upvotes: 2

Tushar Kolhe
Tushar Kolhe

Reputation: 9503

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]

Upvotes: 889

bfontaine
bfontaine

Reputation: 19869

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.

Upvotes: 0

Chukwuma Nwaugha
Chukwuma Nwaugha

Reputation: 715

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

Upvotes: 12

noiissyboy
noiissyboy

Reputation: 71

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

Upvotes: 0

Ankur
Ankur

Reputation: 95

Here is the solution you need:

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

Upvotes: 5

Yellow
Yellow

Reputation: 95

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.

Upvotes: 7

Karel Macek
Karel Macek

Reputation: 1189

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.

Upvotes: 1

soumeng78
soumeng78

Reputation: 888

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

Upvotes: 51

BitSpectrum
BitSpectrum

Reputation: 61

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.

Upvotes: 6

Awanish Kumar Golwara
Awanish Kumar Golwara

Reputation: 2871

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

Upvotes: 284

ibra ndiaye
ibra ndiaye

Reputation: 365

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

sudo apt-get install ffmpeg libsm6 libxext6  -y

Upvotes: 1

Epic Chen
Epic Chen

Reputation: 1382

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

......

Upvotes: 18

Related Questions