PyTorch Installation

PyTorch installation is a single pip command, but picking the right build matters if you want GPU acceleration. This lesson shows how to install PyTorch on each platform and how to confirm it can see your hardware.

Create an Environment

python -m venv .venv
# Windows: .venv\Scripts\activate
# macOS/Linux: source .venv/bin/activate
python --version
Python 3.12.x

Install with pip

Go to pytorch.org/get-started and choose your OS, pip, Python and compute platform; the site generates the exact command. Typical cases:

  • macOS (Apple Silicon or Intel): pip install torch torchvision — MPS GPU support is included.
  • Linux with NVIDIA GPU: pip install torch torchvision usually ships with a recent CUDA build already.
  • Windows with NVIDIA GPU: use the command from pytorch.org, which adds an --index-url pointing at a CUDA wheel (for example .../whl/cu128).
  • CPU only: add --index-url https://download.pytorch.org/whl/cpu for a smaller download.
pip install torch torchvision
Successfully installed torch-2.x.x torchvision-0.x.x ...

You do not need to install the full CUDA Toolkit separately: the pip wheels bundle the CUDA runtime libraries. You only need a recent NVIDIA driver.

Verify the Installation

import torch

print(torch.__version__)
print("CUDA:", torch.cuda.is_available())
print("MPS:", torch.backends.mps.is_available())
print(torch.rand(2, 3))
2.x.x
CUDA: False
MPS: True
tensor([[0.4963, 0.7682, 0.0885],
        [0.1320, 0.3074, 0.6341]])
(sample output — values are random and availability depends on your machine)

When CUDA is Not Available

  • Run nvidia-smi in a terminal; if it fails, install or update the NVIDIA driver.
  • Check torch.version.cuda: None means you installed a CPU-only build.
  • Uninstall torch and reinstall using the CUDA command from pytorch.org.
  • Make sure your notebook or IDE uses the same virtual environment you installed into.

Using Google Colab

If you have no GPU, Google Colab and Kaggle notebooks come with PyTorch preinstalled and offer free GPU time. Choose Runtime, Change runtime type, and select a GPU, then all examples in this tutorial work unchanged.

Frequently asked questions

How do I install PyTorch with GPU support?

Install a recent NVIDIA driver, then run the pip command that pytorch.org generates for your OS and CUDA version. Verify with torch.cuda.is_available().

Why does torch.cuda.is_available() return False?

Usually because a CPU-only build was installed, the NVIDIA driver is missing or old, or your editor uses a different Python environment.

Do I need to install CUDA separately for PyTorch?

No. The pip wheels include the CUDA runtime. You only need a compatible NVIDIA driver.

How do I check my PyTorch version?

Run import torch; print(torch.__version__) in Python, or pip show torch in a terminal.