How to Install and Run PyTorch in CI Without Failures
Installing plain torch in CI silently pulls the CUDA build - gigabytes of GPU libraries you do not need - making jobs slow and prone to timeouts and OOM.
PyTorch ships separate CPU and CUDA wheels served from its own package index. The default pip install torch often grabs the CUDA variant, dragging in gigabytes of NVIDIA libraries that a CPU-only runner cannot use. The fix is to install the CPU build from the correct index URL.
Why it fails in CI
PyTorch wheels are large and version/Python-specific, and the CUDA build adds gigabytes of cuDNN/cuBLAS. On a CPU runner those are dead weight that slow installs, blow disk and memory budgets, and can time out. A too-new Python or musl image has no wheel at all.
- Multi-GB download and
No space left on devicefrom CUDA libraries on a CPU runner. Could not find a version that satisfies the requirement torchon a new Python/Alpine.ReadTimeoutErrorpulling the wheel; exit 137 OOM during model load.
Install it reliably
Install the CPU build explicitly from PyTorch’s CPU index unless you are on a GPU runner. Pin a Python version PyTorch supports and stay on a glibc image.
# CPU-only: pull the CPU wheel, not the multi-GB CUDA one
pip install torch --index-url https://download.pytorch.org/whl/cpu
# Pin a supported interpreter
- uses: actions/setup-python@v5
with:
python-version: '3.11'Cache & speed
Cache ~/.cache/pip so the wheel is fetched once, raise the pip timeout for the large download, and prefer a runner with enough disk/RAM for model jobs over retrying OOM/disk failures.
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-torch-cpu-${{ hashFiles('requirements*.txt') }}
- run: pip install --timeout 120 torch --index-url https://download.pytorch.org/whl/cpuCommon errors
| Error | Cause | Fix |
|---|---|---|
| No space left on device | CUDA build pulled on CPU runner | Use the CPU index URL |
| Could not find a version that satisfies torch | No wheel for Python/platform | Pin a supported Python; use glibc |
| ReadTimeoutError pulling wheel | Huge download timed out | Raise --timeout; cache pip |
| Exit 137 on model load | OOM-killed | Bigger runner; CPU build |
Key takeaways
- Install the CPU build via --index-url .../whl/cpu on non-GPU runners.
- Pin a PyTorch-supported Python on a glibc image.
- Cache the wheel, raise the timeout, and watch disk/OOM.