377

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.

Lenny4
  • 1,215
  • 1
  • 14
  • 33
Ryan
  • 8,459
  • 14
  • 40
  • 66
  • How did you install it? `pip install opencv-python`? Could be another issue. See https://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 '19 at 12:19
  • 2
    But, `apt install libgl1-mesa-glx` helped me. – BarzanHayati Dec 19 '22 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 '23 at 16:27

20 Answers20

681

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]

STerliakov
  • 4,983
  • 3
  • 15
  • 37
Tushar Kolhe
  • 7,375
  • 1
  • 7
  • 13
  • 51
    what does this do and why do I need it for cv2? – SaPropper Jun 15 '21 at 11:19
  • I got an error: "E: Repository 'http://deb.debian.org/debian buster-updates InRelease' changed its 'Suite' value from 'stable-updates' to 'oldstable-updates'". I fixed it by using: `sudo apt-get update --allow-releaseinfo-change` – Pieter Aug 17 '21 at 10:01
  • 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 http://deb.debian.org/debian buster/main amd64 xdg-user-dirs amd64 0.17-2 [53.8 kB] > #8 254.6 E: Failed to fetch http://deb.debian.org/debian/pool/main/libp/libpng1.6/libpng16-16_1.6.36-6_amd64.deb Hash Sum mismatch > #Last modification reported: Mon, 08 Apr 2019 10:11:25 – SocraticDatum Nov 10 '21 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 – Tushar Kolhe Dec 06 '21 at 05:53
  • This does not work for me. But the answer of Awanish Kumar Golware does – svebert Jun 01 '22 at 08:58
  • 9
    Per an answer below can use opencv-python-headless if you don't reuquire GUI functions, see here: https://github.com/opencv/opencv-python/issues/370 – markmnl Jun 07 '22 at 07:32
  • 2
    nit: didn't need ffmpeg for me – lee penkman Aug 21 '22 at 23:13
  • For anyone working in Deepnote the instructions for extending the Dockerfile are [here](https://deepnote.com/docs/custom-environments#local-dockerfile) – Raisin Apr 14 '23 at 20:30
186

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
Awanish Kumar Golwara
  • 1,891
  • 1
  • 7
  • 5
98

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
Lucas Wiman
  • 10,021
  • 2
  • 37
  • 41
  • 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 '22 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 '22 at 10:08
  • 2
    Wow! Other answers required me installing >1GB of dependencies, this worked amazingly! – mxbi Sep 18 '22 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 05 '23 at 20:43
  • @Leigh I haven't seen that error, but this question looks relevant: https://stackoverflow.com/questions/72706073/attributeerror-partially-initialized-module-cv2-has-no-attribute-gapi-wip-gs – Lucas Wiman Jun 06 '23 at 02:00
  • yep, it worked for me on **visual studio code github codespaces** too. recommend this. thanks a lot. – user8395964 Jul 10 '23 at 16:51
  • this should be the top answer! – Ilya Lasy Jul 19 '23 at 12:26
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
Andrej Chudy
  • 859
  • 6
  • 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 '21 at 14:21
  • This gets just about every lib you could want. I didn't try it, but is pip install still needed? – Mark Carpenter Jr Mar 29 '21 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. – Haplo Jul 08 '21 at 08:59
  • 3
    This is absolutely the most stable approach. The ffmpeg issue is breaking the leading answer. – Joel Teply Jan 17 '22 at 04: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 '22 at 20:12
34

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
soumeng78
  • 600
  • 7
  • 12
30

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

sudo yum install mesa-libGL -y
Matt Messersmith
  • 12,939
  • 6
  • 51
  • 52
16

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 \
Maxi
  • 421
  • 4
  • 4
13

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

......
Epic Chen
  • 1,282
  • 12
  • 17
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
bfontaine
  • 18,169
  • 13
  • 73
  • 107
Ankur
  • 95
  • 1
  • 3
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.

BitSpectrum
  • 51
  • 1
  • 2
5

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

Chukwuma Nwaugha
  • 575
  • 8
  • 17
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.

Rui Vieira
  • 5,253
  • 5
  • 42
  • 55
Yellow
  • 65
  • 3
  • typo in the package name, but I can't edit it – WiringHarness Jun 21 '22 at 15:53
  • 2
    At one point, I used [opencv-python-headless](https://pypi.org/project/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 '22 at 10:58
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.

Jens Timmerman
  • 9,316
  • 1
  • 42
  • 48
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.

peachcyz
  • 21
  • 4
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
Lenny4
  • 1,215
  • 1
  • 14
  • 33
1

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

sudo apt-get install ffmpeg libsm6 libxext6  -y
ibra ndiaye
  • 315
  • 4
  • 7
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.

Karel Macek
  • 1,119
  • 2
  • 11
  • 24
1

This command works for me.

apt-get install libgl1 
Dong Xie
  • 21
  • 1
0

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

noiissyboy
  • 61
  • 2
  • 5
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.

bfontaine
  • 18,169
  • 13
  • 73
  • 107