ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory when importing cv2 using Docker container
Asked Answered
M

5

21

I was builting a web app with streamlit, OpenCV and Torch on local machine. The whole project went well until I built a Docker file and was about to transport it to my Google Cloud Platform. Can anyone tell me what is really going wrong here? enter image description here

Here is my Dockerfile:

FROM pytorch/pytorch:latest
RUN pip install virtualenv
ENV VIRTUAL_ENV=/venv
RUN virtualenv venv -p python3
ENV PATH="VIRTUAL_ENV/bin:$PATH"

WORKDIR /app
ADD . /app

# Install dependencies
RUN pip install -r requirements.txt

# copying all files over
COPY . /app

# Expose port 
ENV PORT 8501

# cmd to launch app when container is run
CMD streamlit run app.py

# streamlit-specific commands for config
ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8
RUN mkdir -p /root/.streamlit
RUN bash -c 'echo -e "\
[general]\n\
email = \"\"\n\
" > /root/.streamlit/credentials.toml'

RUN bash -c 'echo -e "\
[server]\n\
enableCORS = false\n\
" > /root/.streamlit/config.toml'

And requirements.txt:

albumentations==0.4.5
matplotlib==3.2.2
numpy==1.19.0
opencv-python==4.1.0.25
# opencv-python-headless==4.2.0.34
pandas==1.0.5
Pillow==7.1.2
scipy==1.5.0
streamlit==0.62.0
Mcgehee answered 8/7, 2020 at 1:2 Comment(0)
K
39

Maybe, you should run following command before pip.

apt update
apt-get install -y libglib2.0-0 libsm6 libxrender1 libxext6
Kemp answered 8/7, 2020 at 2:14 Comment(1)
Do you mean that I should do it separately or do it with dockerfile?Mcgehee
K
6

Anyone encountering this issue and wanting to install a minimum number of external packages, this solved using open-cv and torch in one docker image for me:

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install\
    libgl1\
    libgl1-mesa-glx \ 
    libglib2.0-0 -y && \
    rm -rf /var/lib/apt/lists/*

P.S.: Notice how the combined apt-get update and apt-get install safe one layer.

P.S.S.: As huyu mentioned: include this in your Dockerfile before the pip command.

Edit: Thanks @gardner-bickford for suggesting to remove apt-cache to further decrease image size.

Kerouac answered 6/6, 2023 at 13:27 Comment(1)
You might want to remove the apt cache files in that same layer: rm -rf /var/lib/apt/lists/* From: docs.docker.com/develop/develop-images/instructions/#apt-getJacki
I
0

I had similar error when trying to run the fresh install of Anki flashcard program. Searched/installed missing "libgthread-2" via YaST2 Package Manager.

Instructive answered 1/7, 2023 at 19:43 Comment(0)
I
0

In my case (on NixOS) it was enough to install the headless version:

pip uninstall opencv-python
pip install opencv-python-headless
Iodine answered 1/5 at 12:3 Comment(0)
P
0

On openSUSE, this fixed the issue:

sudo zypper install libgthread-2_0-0 libgthread-2_0-0-32bit
Portland answered 29/7 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.