How to Install PyTorch with GPU Support
To install
PyTorch with GPU support, use the official command from pytorch.org that matches your CUDA version, for example, pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 for CUDA 11.8. This ensures PyTorch uses your GPU for faster computations.Syntax
The basic syntax to install PyTorch with GPU support uses pip or conda with a special URL for CUDA-enabled wheels.
- pip install torch torchvision torchaudio --index-url <URL>: Installs PyTorch and related packages with GPU support.
- --index-url: Points to the PyTorch wheel repository for CUDA versions.
- CUDA version: Must match your system's installed CUDA toolkit version (e.g., cu118 for CUDA 11.8).
bash
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118Example
This example shows how to install PyTorch with GPU support for CUDA 11.8 and verify that PyTorch detects the GPU.
bash
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 python -c "import torch; print('CUDA available:', torch.cuda.is_available()); print('GPU count:', torch.cuda.device_count())"
Output
CUDA available: True
GPU count: 1
Common Pitfalls
- Mismatch CUDA version: Installing a PyTorch version for a CUDA version not installed on your system causes errors or fallback to CPU.
- Missing GPU drivers: Ensure your GPU drivers are up to date and compatible with your CUDA version.
- Using CPU-only command: Avoid installing PyTorch without specifying the CUDA index URL if you want GPU support.
bash
pip install torch torchvision torchaudio # This installs CPU-only version
# Correct way:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118Quick Reference
Choose the correct CUDA version for your system and use the matching install command:
| CUDA Version | Install Command Example |
|---|---|
| CUDA 11.8 | pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 |
| CUDA 11.7 | pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 |
| CUDA 11.6 | pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu116 |
| CPU only | pip install torch torchvision torchaudio |
Key Takeaways
Always match the PyTorch CUDA version with your system's installed CUDA toolkit.
Use the official PyTorch index URL to install GPU-enabled PyTorch packages.
Verify GPU availability in PyTorch with torch.cuda.is_available() after installation.
Keep your GPU drivers updated to avoid compatibility issues.
Installing PyTorch without CUDA URL installs CPU-only version by default.