Troubleshooting torch.cuda.is_available Returns False After Installing PyTorch with uv

Run uv cache clean torch followed by uv sync --extra cu124 to force uv to install CUDA-enabled wheels instead of CPU-only packages.

When torch.cuda.is_available() returns False despite having an NVIDIA GPU, the installation likely pulled CPU-only PyTorch wheels rather than CUDA-enabled builds. The repository baonguyen6742/uv-install-torch demonstrates how uv's dependency resolution can inadvertently select CPU packages when the CUDA extra is not explicitly requested, as configured in the pyproject.toml conflict rules.

Why CUDA Detection Fails with uv

The failure occurs across several layers, from hardware compatibility to uv's dependency resolution strategy.

Hardware and Driver Requirements

First, verify your NVIDIA driver supports the CUDA version specified in your PyTorch wheels. Run nvidia-smi --query-gpu driver_version --format=csv to check your driver version against the CUDA compatibility matrix. For the cu124 extra used in this repository, your driver must support CUDA 12.4.

The CPU vs CUDA Extra Conflict

In pyproject.toml (lines 30-33), the repository defines mutually exclusive extras: cpu and cu124. The conflict rule prevents both from being installed simultaneously. If you run uv sync without the --extra cu124 flag, uv resolves the dependency graph to the CPU-only wheels, causing torch.cuda.is_available() to return False even on GPU hardware.

Stale Cache Issues

The uv cache can retain previously resolved CPU wheels. According to the repository's README, stale cache entries may cause uv to fallback to CPU packages even when requesting CUDA extras. The pyproject.toml (lines 24-50) configures the index URLs for CUDA wheels, but cached metadata can override these settings.

Step-by-Step Fix Workflow

Follow these commands in your repository root to resolve the detection failure:

  1. Verify driver compatibility

    nvidia-smi --query-gpu driver_version,name --format=csv
  2. Clean uv caches

    uv cache prune
    uv cache clean torch
  3. Remove lockfile and environment

    rm -rf .venv uv.lock
  4. Reinstall with CUDA extra

    uv sync --extra cu124
  5. Run the verification script

    uv run main.py

The main.py script in the repository imports the configured libraries and prints CUDA availability status. Expected output shows torch.cuda.is_available: True and device properties.

Verifying Correct Wheel Installation

Confirm uv installed the CUDA-enabled wheels by checking the package metadata:

uv pip list | grep torch

You must see the CUDA suffix in the version string:

torch          2.4.1+cu124
torchvision    0.19.1+cu124
torchaudio     2.4.1+cu124

If the +cu124 suffix is missing, the CPU-only wheel is installed.

Use this diagnostic script to verify the runtime environment:


# basic_check.py

import torch

print("torch version:", torch.__version__)
print("CUDA available?:", torch.cuda.is_available())
print("CUDA device count:", torch.cuda.device_count())
if torch.cuda.is_available():
    print("Device properties:", torch.cuda.get_device_properties(0))

Run with uv run basic_check.py to confirm the CUDA runtime is accessible.

Summary

  • The conflict rule in pyproject.toml (lines 30-33) ensures cpu and cu124 extras are mutually exclusive, requiring explicit selection
  • Always invoke uv sync --extra cu124 to resolve CUDA-enabled wheels from the index URLs defined at lines 24-50
  • Clear the uv cache with uv cache clean torch to eliminate stale CPU wheel references
  • Verify installation using uv pip list and check for the +cu124 version suffix
  • The repository's main.py provides a reference implementation for testing CUDA availability after installation

Frequently Asked Questions

Why does uv install the CPU version of PyTorch by default?

According to the baonguyen6742/uv-install-torch source code, the pyproject.toml defines cpu and cu124 as conflicting extras. Without the --extra cu124 flag, uv's resolver selects the CPU variant to satisfy dependency constraints, as the CPU extra requires fewer system-specific libraries.

How do I check if my GPU supports the CUDA version in the wheels?

Run nvidia-smi and compare the driver version against NVIDIA's CUDA driver compatibility matrix. The repository uses cu124 (CUDA 12.4), which requires a specific minimum driver version. If your driver is older, modify the extra in pyproject.toml to use a lower CUDA version like cu118.

What should I do if clearing the cache doesn't fix the detection?

Delete both the .venv directory and uv.lock file completely, then run uv sync --extra cu124 again. This forces uv to re-resolve the entire dependency graph from scratch, ensuring no stale lockfile entries reference CPU wheels.

Can I install PyTorch with uv for a different CUDA version?

Yes. Edit the pyproject.toml file to change the index URL (lines 24-50) and the extra name from cu124 to your target version (e.g., cu121 for CUDA 12.1). Ensure the extra definition and conflict rules are updated to match the new version identifier.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →