Cant Install torch + torchaudio + torchcodec

How to build and install torchcodec with CUDA Support

The Solution ✅

  1. Conda Environment: Created a Conda environment specifically for CUDA 13.0, Python 3.12.
  2. Install PyTorch: Installed the versions of torch, torchvision, and torchaudio built for CUDA 13.0.
  3. Build Custom FFmpeg: Compiled FFmpeg from source, enabling NVIDIA GPU features
  4. Address torchcodec: Since no pre-built torchcodec existed for the exact setup (ARM + CUDA 13.0 Nightly), build torchcodec from source within the activated environment.

** If something’s off try the nightly version for step#2

# TITLE: Setup Conda Environment with RAPIDS and PyTorch

export CUDA_HOME=/usr/local/cuda
export PATH=$CUDA_HOME/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib:$CUDA_HOME/lib:$CUDA_HOME/lib64:$LD_LIBRARY_PATH

# Create Conda environment with RAPIDS and PyTorch
ENV_NAME=torch
conda create -n ${ENV_NAME} -c rapidsai-nightly -c conda-forge -c nvidia rapids=25.10 python=3.12 'cuda-version=13.0' jupyter hdbscan umap-learn ipykernel -y
conda activate ${ENV_NAME}

# Install PyBind11 and PyTorch with CUDA 13.0 support
conda install -y pybind11
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu130
# TITLE: Install FFmpeg with NVIDIA NVENC and NVDEC Support
# - https://docs.nvidia.com/video-technologies/video-codec-sdk/12.0/ffmpeg-with-nvidia-gpu/index.html

# Create a directory for FFmpeg source code
mkdir ffmpeg                                                                                           

# Install NVIDIA Video Codec SDK headers
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git
cd nv-codec-headers && sudo make install && cd ..

# Install dependencies
sudo apt-get install build-essential yasm cmake libtool libc6 libc6-dev unzip wget libnuma1 libnuma-dev

# Install FFmpeg with NVENC and NVDEC support
git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg/
cd ffmpeg
make clean

# Configure FFmpeg with NVIDIA support
./configure \
  --enable-nonfree \
  --enable-cuda-nvcc \
  --enable-nvenc \
  --enable-nvdec \
  --extra-cflags=-I/usr/local/cuda/include \
  --extra-ldflags=-L/usr/local/cuda/lib64 \
  --disable-static \
  --enable-shared

# Build and install FFmpeg
make -j 8
sudo make install

# Update shared library cache
sudo ldconfig

# Verify FFmpeg installation with NVIDIA support
ffmpeg -version
ffmpeg -decoders | grep -i "nvdec"
ffmpeg -encoders | grep -i "nvenc"
 
# Verify FFmpeg linked libraries
ldd $(which ffmpeg)
ffmpeg -hwaccels
# TITLE: Install torchcodec with CUDA Support

# Clone torchcodec repository
git clone https://github.com/meta-pytorch/torchcodec.git
cd torchcodec
# Modify pyproject.toml to include torch and pybind11 as dependencies
sed -i 's/requires *= *\["setuptools>=61.0"\]/requires = ["setuptools>=61.0", "torch", "pybind11"]/g' pyproject.toml

# Install torchcodec with CUDA support
ENABLE_CUDA=1 pip install -e . --no-build-isolation
3 Likes