Importerror: libgl.so.1: cannot open shared object file: no such file or direct

1,063 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

 问题:Importerror: libgl.so.1: cannot open shared object file: no such file or directory

[Solved] Importerror: libgl.so.1: cannot open shared object file: no such file or directory - ItsMyCode

这个问题是在安装opencv后使用时出现的,上述提供了四种解决方法,这里进行翻译及简要概况:

1. 安装CV2依赖包

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

Solution 1: Install cv2 dependencies 

The easier way to fix the issue is you can update the packages and install the additional dependencies that are required for cv2 to run properly.

These dependencies will mostly be present in your local machine and hence the application runs without any issue when you perform a docker build using python based images you will get this error.

Just add the below lines into your DockerFile to fix the issue. This will ensure to update the packages and install the additional packages which are required to run cv2.

2. 安装opencv-python包

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

Solution 2: Install python3-opencv package

If you don’t want to manually install the dependency, the better way to resolve the issue is to install the python3-opencv

This will ensure that all the related system dependencies are installed correctly while building the docker containers.

Add the below lines into your DockerFile to install the python3-opencv and then install the other packages which are there in requirements.txt.

3. opencv-python-headless

apt-get update && apt-get install -y opencv-python-headless
pip install opencv-python-headless

Solution 3: Install opencv-python-headless

Instead of opencv-python, you could install opencv-python-headless which includes a a precompiled binary wheel with no external dependencies (other than numpy), and is intended for headless environments like Docker.

When compared to python3-opencv this is a much more lightweight package and reduces the docker image size by 700MB.

4. 安装libgl1

apt-get update && apt-get install libgl1

Solution 4: Install only the dependency (libgl1)

All the above solutions will install the cv2 dependencies and thus increasing the image size. 

If you do not want to increase the image size then you can resolve the issue by installing the libgl1 dependency as shown below. This is not a recommended solution but it does work if you are just getting the Importerror: libgl.so.1

总结

我们可以通过安装cv2所需的附加依赖项来解决这个错误,或者我们可以只使用其中一个包,如python3-opencv, opencv-python-headless,它将安装所有相关的依赖项并解决错误。

We can resolve this error by installing the additional dependencies which are required by cv2 or we can just use the one of the packages such as python3-opencvopencv-python-headless which will install all the related dependencies and resolving the error.