I cannot use opencv2 and received ImportError: libgl.so.1 cannot open shared object file no such file or directory

Question:

**env:**ubuntu16.04 anaconda3 python3.7.8 cuda10.0 gcc5.5

command:

conda activate myenv
python
import cv2

error:
Traceback (most recent call last):
File "", line 1, in
File "/home/.conda/envs/myenv/lib/python3.7/site-packages/cv2/__init__.py", line 5, in
from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

I tried:

RUN apt install libgl1-mesa-glx -y
    RUN apt-get install 'ffmpeg'
        'libsm6'
        'libxext6'  -y

but this is already installed and the latest version(libgl1-mesa-glx18.0.5-0ubuntu0~16.04.1).
then i tried:

sudo apt-get install --reinstall libgl1-mesa-glx

it doesn’t work.
finally,I tried to remove the package:

sudo apt-get --purge remove libgl1-mesa-glx

another error occurred:

Reading package list... Done
Analyzing the dependency tree of the package
Reading status information... Done
Some packages cannot be installed. If you are using an unstable distribution, this may be
Because the system cannot reach the state you requested. There may be some software you need in this version
The packages have not been created yet or they have been moved out of the Incoming directory.
The following information may be helpful in solving the problem:

The following packages have unmet dependencies:
 libqt5multimedia5-plugins: Dependency: libqgsttools-p1 (>= 5.5.1) but it will not be installed
E: Error, pkgProblemResolver::Resolve failed. This may be due to a software package being required to maintain the status quo.

Any help would be really helpful.Thanks in advance.

conda list:

# packages in environment at /home/lwy/.conda/envs/mmdet1:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
_openmp_mutex             4.5                       1_gnu    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
addict                    2.3.0                     <pip>
albumentations            0.5.1                     <pip>
appdirs                   1.4.4                     <pip>
asynctest                 0.13.0                    <pip>
attrs                     20.2.0                    <pip>
ca-certificates           2020.6.20            hecda079_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
certifi                   2020.6.20        py37he5f6b98_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
chardet                   3.0.4                     <pip>
cityscapesScripts         2.1.7                     <pip>
codecov                   2.1.10                    <pip>
coloredlogs               14.0                      <pip>
coverage                  5.3                       <pip>
cycler                    0.10.0                    <pip>
Cython                    0.29.21                   <pip>
decorator                 4.4.2                     <pip>
flake8                    3.8.4                     <pip>
future                    0.18.2                    <pip>
humanfriendly             8.2                       <pip>
idna                      2.10                      <pip>
imagecorruptions          1.1.0                     <pip>
imageio                   2.9.0                     <pip>
imgaug                    0.4.0                     <pip>
importlib-metadata        2.0.0                     <pip>
iniconfig                 1.1.1                     <pip>
isort                     5.6.4                     <pip>
kiwisolver                1.3.1                     <pip>
kwarray                   0.5.10                    <pip>
ld_impl_linux-64          2.35                 h769bd43_9    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libffi                    3.2.1                         1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
libgcc-ng                 9.3.0               h5dbcf3e_17    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libgomp                   9.3.0               h5dbcf3e_17    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
libstdcxx-ng              9.3.0               h2ae2ef3_17    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
matplotlib                3.3.2                     <pip>
mccabe                    0.6.1                     <pip>
mmcv                      1.1.6                     <pip>
mmdet                     1.2.0+unknown             <pip>
ncurses                   6.2                  he1b5a44_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
networkx                  2.5                       <pip>
numpy                     1.19.4                    <pip>
opencv-python             4.4.0.46                  <pip>
openssl                   1.1.1h               h516909a_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
ordered-set               4.0.2                     <pip>
packaging                 20.4                      <pip>
Pillow                    6.2.2                     <pip>
pip                       20.2.4                     py_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
pluggy                    0.13.1                    <pip>
py                        1.9.0                     <pip>
pycocotools               2.0                       <pip>
pycodestyle               2.6.0                     <pip>
pyflakes                  2.2.0                     <pip>
pyparsing                 2.4.7                     <pip>
pyquaternion              0.9.9                     <pip>
pytest                    6.1.2                     <pip>
pytest-cov                2.10.1                    <pip>
pytest-runner             5.2                       <pip>
python                    3.7.8           h6f2ec95_1_cpython    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
python-dateutil           2.8.1                     <pip>
python_abi                3.7                     1_cp37m    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
PyWavelets                1.1.1                     <pip>
PyYAML                    5.3.1                     <pip>
readline                  8.0                  he28a2e2_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
requests                  2.24.0                    <pip>
scikit-image              0.17.2                    <pip>
scipy                     1.5.3                     <pip>
setuptools                49.6.0           py37he5f6b98_2    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
Shapely                   1.7.1                     <pip>
six                       1.15.0                    <pip>
sqlite                    3.33.0               h4cf870e_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
tifffile                  2020.10.1                 <pip>
tk                        8.6.10               hed695b0_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
toml                      0.10.2                    <pip>
torch                     1.5.0+cu92                <pip>
torchvision               0.6.0+cu92                <pip>
tqdm                      4.51.0                    <pip>
typing                    3.7.4.3                   <pip>
ubelt                     0.9.3                     <pip>
urllib3                   1.25.11                   <pip>
wheel                     0.35.1             pyh9f0ad1d_0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
xdoctest                  0.15.0                    <pip>
xz                        5.2.5                h516909a_1    https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
yapf                      0.30.0                    <pip>
zipp                      3.4.0                     <pip>
zlib                      1.2.11                        0    https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
Asked By: nobug

||

Answers:

I have solved this problem!
Firstly, find the file:

find /usr -name libGL.so.1

I found /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1.
Then, I created a soft link:

ln -s /usr/lib/x86_64-linux-gnu/mesa/libGL.so.1 /usr/lib/libGL.so.1

Finally, I verified that it is valid:

# python
import cv2
Answered By: nobug

Usually these Pacakges are meant to be installed as System Packages and Not only Python packages. Therefore many times even after successfull installation of such packages like opencv, cmake, dlib they don’t work.

The Best way is to Install them is using.

sudo apt-get install python3-opencv

This is the Preferred Method for the Successfull Installation of opencv on Ubuntu as per the Official Opencv Docs.

Answered By: Abdul Rehman

I was able to solve the issue by

apt-get install libgl1 
Answered By: George Raveen