I needed to have Python torchaudio
library installed for my application which is packaged into a Docker image.
I am able to do this easily on my EC2 instance easily:
pip3 install torchaudio
python3
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torchaudio
>>> torchaudio.__version__
'2.2.1+cu121'
But not through my Dockerfile, here's what I have in my Dockerfile:
RUN pip3 install --target=/opt/prod/lib/python3.8/site-packages torchaudio
but when I entered into the docker container started from this image:
>>> import torchaudio
/opt/prod/lib/python3.8/site-packages/torchaudio/_internal/module_utils.py:99: UserWarning: Failed to import soundfile. 'soundfile' backend is not available.
warnings.warn("Failed to import soundfile. 'soundfile' backend is not available.")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/prod/lib/python3.8/site-packages/torchaudio/__init__.py", line 1, in <module>
from torchaudio import ( # noqa: F401
File "/opt/prod/lib/python3.8/site-packages/torchaudio/_extension.py", line 135, in <module>
_init_extension()
File "/opt/prod/lib/python3.8/site-packages/torchaudio/_extension.py", line 105, in _init_extension
_load_lib("libtorchaudio")
File "/opt/prod/lib/python3.8/site-packages/torchaudio/_extension.py", line 52, in _load_lib
torch.ops.load_library(path)
File "/opt/prod/lib/python3.8/site-packages/torch/_ops.py", line 852, in load_library
ctypes.CDLL(path)
File "/opt/prod/python3.8/lib/python3.8/ctypes/__init__.py", line 373, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libtorch_cuda_cpp.so: cannot open shared object file: No such file or directory
I prefer to have a separate requirements.txt
for installing Python packages rather than specific pip install
commands in the Dockerfile
.
🗎 Dockerfile
(Upgrading pip
is not necessary but it silences a warning message.)
FROM python:3.10.12
COPY requirements.txt .
RUN pip install --upgrade pip && \
pip install -r requirements.txt
🗎 requirements.txt
torch==2.2.1
torchaudio==2.2.1
numpy==1.26.4