UV Tutorial Part 3: Installing PyTorch With CUDA

Why Installing Torch with CUDA is Challenging

Installing PyTorch (torch) with CUDA can be quite challenging due to fact that CUDA installation often involves installing system-level dependencies, which can vary depending on the operating system. Managing these dependencies in a virtual environment adds another layer of complexity, especially when different projects require different versions. Even with conda, mamba, poetry this process is slow and challenging.

Even with uv, you might face some challenges. While uv simplifies many aspects of project management, dealing with system-level dependencies like CUDA still requires careful handling.

Creating a New Project with UV

First, let’s create a new project using uv:

1
uv init --python 3.10 project_name

Note: I couldn’t install torch using uv for Python 3.13, so we are using Python 3.10 here.

Entering the Project Directory

Navigate to your project directory:

1
cd project_name

Creating and Activating a Virtual Environment

Now, create a virtual environment with uv:

1
uv venv

To activate the virtual environment on Windows:

1
.venv\Scripts\activate

Usually, uv doesn’t require you to activate the virtual environment. However, in this case, we need to activate it to ensure proper installation of torch with CUDA.

Installing Torch with CUDA

Visit the PyTorch official website to get the installation command. We’ll slightly modify the command to install torch with CUDA:

For Windows:

1
uv pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124

For Linux, you can ignore the --index-url option:

1
uv pip install torch torchvision torchaudio

Adding Packages to pyproject.toml

To add these packages to the pyproject.toml file:

1
uv add torch torchvision torchaudio

Verifying the Installation

To check if torch is installed with CUDA, let’s create a new Python file hello.py:

1
2
import torch
print(torch.cuda.is_available())

Run the file to verify:

1
uv run hello.py

If the output is True, then torch is successfully installed with CUDA.

Uninstalling Torch

If you need to uninstall torch, you can do so with:

1
uv pip uninstall torch torchvision torchaudio

To remove it from the pyproject.toml file:

1
uv remove torch torchvision torchaudio

This concludes the third part of the UV tutorial. You’ve learned how to handle the installation of complex packages like PyTorch with CUDA using uv. Happy coding!—


Related Content

0%