This opening note of the hands-on PyTorch category covers getting a working PyTorch environment set up correctly — the one step every code example throughout this entire hub has assumed was already done.
Standard Installation
# CPU-only (works everywhere, but slow for real training)
pip install torch torchvision torchaudio
# GPU (NVIDIA CUDA) -- check pytorch.org for the exact command matching your CUDA version
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
Verifying the Installation
import torch
print(torch.__version__) # confirm PyTorch installed correctly
print(torch.cuda.is_available()) # True if a GPU is detected and usable
print(torch.cuda.device_count()) # how many GPUs are available
if torch.cuda.is_available():
print(torch.cuda.get_device_name(0))
Why the CUDA Version Must Match
PyTorch's GPU support is compiled against a specific CUDA toolkit version — installing a PyTorch build compiled for CUDA 12.1 on a system with an incompatible driver/CUDA version can cause torch.cuda.is_available() to silently return False, or cause cryptic runtime errors. This is one of the single most common environment-setup issues in practice.
Common Mistakes
- Installing the default CPU-only PyTorch build on a machine with a GPU, then being confused why training is unexpectedly slow — the GPU build must be installed explicitly, matching the system's CUDA version.
- Mixing PyTorch versions with incompatible
torchvision/torchaudioversions — these companion packages have specific version compatibility requirements with the core PyTorch version.
Interview Relevance
Q: "Your code calls torch.cuda.is_available() and it returns False on a machine you know has a GPU. What would you check first?" Whether PyTorch was installed with GPU (CUDA) support at all — the default pip install torch command on some systems installs a CPU-only build — and whether the installed PyTorch build's expected CUDA version matches what's actually installed on the system (driver and toolkit version compatibility).
Practice Question
Why might a machine with a properly functioning NVIDIA GPU still show torch.cuda.is_available() returning False?