Two methods to install CUDA Torch with UV
Approx. 1 min read
Persist the settings in pyproject.toml
Append this snippet to pyproject.toml , and runuv add torch torchvision to install torch with configurated version.
This is better for co-development, as the others can install torch cuda with uv sync only.
[tool.uv.sources]
torch = [
{ index = "pytorch-cu128", marker = "sys_platform == 'linux' or sys_platform == 'win32'" },
]
torchvision = [
{ index = "pytorch-cu128", marker = "sys_platform == 'linux' or sys_platform == 'win32'" },
]
[[tool.uv.index]]
name = "pytorch-cu128"
url = "https://download.pytorch.org/whl/cu128"
explicit = trueQuick installation
Quick install CUDA torch; this won't be saved to pyproject.toml. You can replace cu128 with auto to detect the CUDA version automatically.
uv pip install torch torchvision --torch-backend=cu128Refs: https://docs.astral.sh/uv/guides/integration/pytorch/#the-uv-pip-interface