Overview
Setting up PyTorch often involves more than running a single command, especially on Windows. Many users waste hours due to version mismatches among Python, CUDA, and PyTorch itself. This guide focuses on practical steps to avoid those pitfalls, covering both CPU and GPU setups with Anaconda.
Prerequisites
- Anaconda – Recommended for managing virtual environments and avoiding package conflicts. Create a dedicated environment (e.g.,
pytorch_env) and specify a Python version that matches you're PyTorch build. - NVIDIA GPU drivers – Even if you plan to use only CPU, having drivers installed is harmless. For GPU usage, check your driver version via
nvidia-smiin Command Prompt.
Version Matching Strategy
The key is to choose three interdependent versions in order:
- CUDA runtime version – Determined by your driver’s
CUDA Version(max supported). Use a CUDA Toolkit version ≤ your driver version. Verify the installed toolkit withnvcc --version. - PyTorch release – Pick a PyTorch build that targets your CUDA version (e.g.,
cu118for CUDA 11.8). Official PyTorch install commands include the--extra-index-urlflag specifying the CUDA variant. - Python interpreter – After CUDA and PyTorch are chosen, select a Python version supported by that PyTorch build (typically 3.8–3.11). Use Anaconda to create an environment with the exact version.
Installation Methods
Method 1: Direct pip from PyTorch's index
conda create -n pytorch_env python=3.10
conda activate pytorch_env
pip install torch==2.0.1+cu118 torchvision==0.15.2+cu118 torchaudio==2.0.2 --extra-index-url https://download.pytorch.org/whl/cu118
This works if you're network can reach the PyTorch repository reliably. Otherwise, use the offline approach.
Method 2: Offline installation via downloaded wheels
- Visit PyTorch's wheel index.
- Download the
.whlfiles fortorch,torchvision, andtorchaudiomatching your Python version (e.g.,cp310for Python 3.10) and CUDA variant (e.g.,cu118). - Install them sequentially: ```
pip install C:\path\to\torch-2.0.1+cu118-cp310-cp310-win_amd64.whl
pip install C:\path\to\torchvision-0.15.2+cu118-cp310-cp310-win_amd64.whl
pip install C:\path\to\torchaudio-2.0.2+cu118-cp310-cp310-win_amd64.whl
Verification
After installation, run a quick sanity check:
python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())"
If cuda.is_available() returns True, your GPU setup is complete.
Common Pitfalls
- Ignoring driver vs. runtime CUDA: The
nvidia-smioutput shows the driver version, not the toolkit. Install a toolkit ≤ that version. - Mixing python minor versions: PyTorch wheels often only exist for specific CPython builds (e.g.,
cp38,cp39,cp310). Always matchcpXexactly. - Forgetting cuDNN: While PyTorch bundles its own cuDNN, some older workflows require manual cuDNN installation. For modern PyTorch (≥1.13), the included cuDNN is sufficient.
Alternative: CPU-only Installation
If you lack a compatible GPU, use the CPU variant:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
This avoids CUDA dependencies entirely.