412

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
  • How did you install it? pip install opencv-python? Could be another issue. See github.com/matplotlib/matplotlib/issues/9954. Try creating a virtualenv and test it there. Can you share your code snippet where its throwing the exception?
    – HariUserX
    Mar 23, 2019 at 12:19
  • 4
    But, apt install libgl1-mesa-glx helped me. Dec 19, 2022 at 11:09
  • 2
    You should add "Docker" as part of the TITLE & TAGS of your question. The correct answer for the TITLE of question is: "Install apt install libgl1-mesa-glx" The title is misleading if you are NOT using docker. EDIT: I have done this for you.
    – B. Shea
    Jan 30 at 16:27

21 Answers 21

736
+50

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
  • 59
    what does this do and why do I need it for cv2?
    – SaPropper
    Jun 15, 2021 at 11:19
  • 1
    This solution worked for me and then stopped working during Docker build. I changed my base image to python:3.8-slim-buster as suggested by @SuryaTej, which worked at first, but then stopped working as well. I thought maybe it was a network issue on my side, but the build fails more often than not. > #8 254.6 Get:197 deb.debian.org/debian buster/main amd64 xdg-user-dirs amd64 0.17-2 [53.8 kB] > #8 254.6 E: Failed to fetch deb.debian.org/debian/pool/main/libp/libpng1.6/… Hash Sum mismatch > #Last modification reported: Mon, 08 Apr 2019 10:11:25 Nov 10, 2021 at 14:33
  • 3
    the author has mentioned in the comments he is doing this in docker. This command just installs the cv2 dependencies that are present on the local machine normally but might be missing in your docker causing the issue @wovano Dec 6, 2021 at 5:53
  • 10
    Per an answer below can use opencv-python-headless if you don't reuquire GUI functions, see here: github.com/opencv/opencv-python/issues/370
    – markmnl
    Jun 7, 2022 at 7:32
  • 2
    nit: didn't need ffmpeg for me Aug 21, 2022 at 23:13
203

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
  • I had same issue as described above but your solution sadly did not work for me. Jul 28, 2022 at 11:32
  • 9
    This is a more concise solution compared to the accepted answer. Oct 12, 2022 at 15:25
  • This worked for me in WSL Ubuntu, then to downgrade protobuf (slightly unrelated, but was my next bug), finally everything worked Nov 24, 2022 at 1:07
  • This one works for me while the accepted answer does not because of various dependency issues.
    – Litchy
    Mar 15 at 3:29
  • Only solution that worked on Sagemaker AWS Aug 28 at 13:40
112

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
  • 7
    This is best answer!! Works great! No need to fiddle around with apt-get or system install when you don;t have the permission to install system packages. Thanks!
    – HAltos
    Aug 16, 2022 at 19:32
  • 2
    It worked quite well in my case in order to build docker image of python environments in Azure ML. Thanks a million!!
    – Elias
    Aug 24, 2022 at 10:08
  • 2
    Wow! Other answers required me installing >1GB of dependencies, this worked amazingly!
    – mxbi
    Sep 18, 2022 at 15:01
  • this got me past my error but then got another error: module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline' -- any idea what this means?
    – Leigh
    Jun 5 at 20:43
  • @Leigh I haven't seen that error, but this question looks relevant: stackoverflow.com/questions/72706073/… Jun 6 at 2:00
82

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
  • 3
    I'm using python:buster. the solution above didn't work for me: ffmpeg seems to be deprecated and I still had write errors. apt-get install -y python3-opencv did the trick. thanks
    – Eric F
    Mar 27, 2021 at 14:21
  • This gets just about every lib you could want. I didn't try it, but is pip install still needed? Mar 29, 2021 at 23:01
  • 4
    @MarkCarpenterJr - Using pip will help you to keep the packed version in a defined state. If you use the version of the operating system, you might end up with changing dependencies if your distro is changing packages. This is probably not what you want.
    – lippertto
    Jul 8, 2021 at 8:59
  • 3
    This is absolutely the most stable approach. The ffmpeg issue is breaking the leading answer.
    – Joel Teply
    Jan 17, 2022 at 4:48
  • 1
    This is the one that worked for me. the pip install opencv-python-headless still gave me the same error. I'm also working in Docker.
    – rkechols
    Oct 31, 2022 at 20:12
37

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
  • 2
    thanks. And don't forget opencv-contrib-python-headless if you're using contrib
    – Joel Teply
    Feb 5, 2022 at 7:13
32

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

sudo yum install mesa-libGL -y
3
  • 2
    This helps me out. Thank a lot.
    – Fhun
    Jan 18, 2022 at 11:38
  • 1
    This helped me a lot thank you Feb 9, 2022 at 8:05
  • 2
    Thanks. This was especially useful for Amazon Linux.
    – hekimgil
    Jul 4, 2022 at 18:21
18

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
  • 1
    This takes indeed much less space. Works on ubuntu 20 Feb 8, 2022 at 0:44
  • worked on my docker ubuntu 20 image
    – Ali Waqas
    Aug 26, 2022 at 17:04
  • Worked with my Docker base image python:3.10.6-slim-buster, thank you! Jul 15 at 9:46
15

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
  • can this lib libgl1-mesa-glx source code be downloaded and installed without sudo privileges?
    – sAguinaga
    Oct 25 at 18:52
7

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

5

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.

5

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
  • 2
    apt-get upgrade updates all installed packages which is completely overkill Jan 30, 2022 at 20:44
  • this works for me. yeah, please do consider the overkill Dec 7, 2022 at 7:52
4

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
  • typo in the package name, but I can't edit it Jun 21, 2022 at 15:53
  • 2
    At one point, I used opencv-python-headless which worked for my case with FastAPI when I deployed on Heroku once. What's the difference between contrib and headless?
    – C.Tale
    Nov 10, 2022 at 10:58
4

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
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.

2

"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.

1

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

sudo apt-get install ffmpeg libsm6 libxext6  -y
1

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.

1

This command works for me.

apt-get install libgl1 
0

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

0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.