Resolving PyTorch CUDA SDK Installation Mismatches and CUDA Not Available Errors (Error 902)
Is your Python machine learning script returning 'AssertionError: CUDA is not available'? Learn how to check your exact Nvidia driver version, locate your NVCC path, and install the matching compiled wheel.
Fixing 'AssertionError: CUDA is not available' in PyTorch
One of the most common issues among local AI researchers starting custom training or testing is finding that torch.cuda.is_available() returns False, even though a powerful NVIDIA graphics processor is installed.
# Step 1: Diagnose Installed NVCC and CUDA Toolkit Alignment
Open a command prompt and check which CUDA runtime version you have installed locally near the system environment paths:
``
nvcc --version
`
Next, check your NVIDIA GPU Driver capability via CLI:
`
nvidia-smi
`
Your driver's supported CUDA version (displayed at the top right of nvidia-smi) must be equal to or greater than the CUDA Toolkit version used to compile your PyTorch binary.
# Step 2: Purge CPU-Only Python Packages
Very often, running a standard pip install torch defaults to downloading the CPU-only version of PyTorch, particularly on Windows. You must purge these binaries to resolve installation overlaps:
`
pip uninstall torch torchvision torchaudio -y
`
# Step 3: Install the Explicitly Compiled CUDA Wheels
You must target the exact precompiled compiled package corresponding to your CUDA setup. Visit PyTorch's official index page and run the explicit wheel directory command.
For CUDA 12.1:
`
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
`
For CUDA 12.4:
`
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
`
After installation, execute python -c "import torch; print(torch.cuda.is_available())" to verify the diagnostic returns True`.