97
votes

i am trying to run cv2, and when i try to import it i get

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.Any help would be really helpful.Thanks in advance.

7
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
I am actually running this on docker, and i am not able to check the open cv version.I tried importing matplotlib and it imports fine. - Ryan

7 Answers

194
votes
RUN apt-get update ##[edited]
RUN apt-get install ffmpeg libsm6 libxext6  -y

Add these lines to your Dockerfile

14
votes

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
votes

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
votes

Please run the following commands

sudo apt update


sudo apt install libgl1-mesa-glx

Link- https://github.com/conda-forge/pygridgen-feedstock/issues/10

0
votes

Here is the solution you need:

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

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.

0
votes

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

sudo apt-get install ffmpeg libsm6 libxext6  -y