How to Install PyTorch in CI (CPU Wheels)
The default PyTorch wheel can pull multi-GB CUDA payloads you do not need on CPU CI. The fix is the CPU wheel index.
PyTorch publishes platform/accelerator-specific wheels through its own index. On CPU-only CI, installing from the CPU index avoids gigabytes of CUDA libraries and the timeouts they cause.
Why it fails in CI
- The default install pulls CUDA-enabled torch (huge) on a CPU runner → slow/timeout.
- A Python version with no matching torch wheel → no candidate.
- Pinned torch/torchvision versions that are incompatible with each other.
Install it reliably
Install from the CPU index URL with matching torch/torchvision versions. Pin a supported Python first via setup-python.
Terminal
# CPU-only wheels (no CUDA payload)
pip install torch torchvision \
--index-url https://download.pytorch.org/whl/cpu
# verify CPU build
python -c "import torch; print(torch.__version__, torch.cuda.is_available())"Cache & speed
Cache ~/.cache/pip keyed on requirements. Even CPU wheels are large; the download is where faster/larger managed runners help and transient failures auto-retry.
.github/workflows/ci.yml
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: pip-${{ runner.os }}-${{ hashFiles('**/requirements*.txt') }}Common errors
- Multi-GB download / timeout → use
--index-url .../whl/cpufor CPU-only CI. No matching distribution found for torch→ Python too new or wrong index; pin Python.torchvision ... compiled against a different version of torch→ pin a matching pair.
Key takeaways
- Install from the PyTorch CPU index to skip multi-GB CUDA wheels.
- Pin torch and torchvision to a compatible pair.
- Cache pip; even CPU wheels are large.
Related guides
How to Install TensorFlow in CIInstall TensorFlow reliably in CI: use the CPU wheel, pin a Python it supports, silence GPU/CUDA warnings, ca…
How to Install NumPy in CI Without Source BuildsInstall NumPy reliably in CI: use prebuilt manylinux wheels, avoid slow source builds, pin the version, cache…
How to Install scikit-learn in CIInstall scikit-learn reliably in CI: use prebuilt wheels, keep NumPy/SciPy ABI-compatible, set OpenMP threads…
Self-Healing CI: Auto-Retrying Transient Registry and 5xx FailuresRegistry timeouts, 429s, and 5xx errors are transient by definition. See why they happen and how self-healing…