How to Install PyTorch and Check Your Setup

A
Author
18 Oct, 2026 4 min read 1 views

The easiest way to install PyTorch is inside a virtual environment, using the command generated by the selector on the official pytorch.org "Get Started" page. That page matches your operating system, package manager and compute platform (CPU or a specific CUDA version) and gives you the exact pip command. After installing, a five-line script confirms that everything works.

Step 1: create a virtual environment

A virtual environment keeps PyTorch and its large dependencies away from your system Python. Use Python 3 with a version that the PyTorch install page lists as supported.

python3 -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
python -m pip install --upgrade pip

Step 2: install the right build

PyTorch ships in different builds. The choice depends on your hardware:

  • CPU only: smallest download, fine for learning and small models.
  • NVIDIA GPU (CUDA): needs a recent NVIDIA driver. The wheel bundles the CUDA libraries it needs, so you usually do not install the full CUDA toolkit yourself.
  • Apple Silicon Macs: the default macOS wheel supports the mps device, which uses the Apple GPU.
  • AMD GPUs (ROCm): available on Linux through a separate build listed on the install page.

On many systems the plain command works, and the selector page shows alternatives for specific CUDA versions with an extra index URL:

# Default build for your platform
pip install torch torchvision

# Specific build: copy the exact command from pytorch.org/get-started
# It adds an --index-url option for CPU or a CUDA version.

Install torchvision as well because later posts use its datasets and pretrained models. Always copy the current command from the official page rather than from an old tutorial, since supported CUDA versions change over time.

Step 3: verify the installation

import torch

print("PyTorch version:", torch.__version__)
print("CUDA available:", torch.cuda.is_available())

if torch.cuda.is_available():
    print("GPU:", torch.cuda.get_device_name(0))
    print("CUDA runtime:", torch.version.cuda)
elif torch.backends.mps.is_available():
    print("Apple GPU (MPS) available")
else:
    print("Running on CPU")

x = torch.rand(3, 3)
print(x @ x)

The script prints the version, tells you which accelerator PyTorch can use, and multiplies a random matrix by itself as a smoke test. On a CPU-only machine you will see "Running on CPU", which is perfectly fine for the early posts.

Choosing a device in code

Write your scripts so they run anywhere. This small helper picks the best available device, and you will reuse the pattern throughout the course:

import torch

def get_device():
    if torch.cuda.is_available():
        return torch.device("cuda")
    if torch.backends.mps.is_available():
        return torch.device("mps")
    return torch.device("cpu")

device = get_device()
t = torch.ones(2, 2, device=device)
print(t.device)

Troubleshooting

  • CUDA available: False on a machine with an NVIDIA GPU: you probably installed the CPU build. Uninstall with pip uninstall torch and install the CUDA build from the selector.
  • Driver too old: update the NVIDIA driver. Run nvidia-smi to see the driver version and the highest CUDA version it supports.
  • Very large download or disk use: CUDA builds are several gigabytes. Use the CPU build if you only want to learn.
  • Mixed environments: if import torch fails after install, check that pip and python belong to the same environment with python -m pip --version.

No GPU? Use a hosted notebook

If your computer has no usable GPU, hosted notebook services offer free or low-cost GPU sessions with PyTorch already installed. Everything in this course up to the transformer posts also runs fine on a CPU with small datasets.

Why a separate environment matters

PyTorch pins itself to particular versions of NumPy and, on GPU builds, to specific CUDA libraries. Installing it into your global Python can break other projects, and upgrading another package later can break PyTorch. A virtual environment per project removes that risk. Tools such as venv, conda or uv all work; the important thing is that you can delete the environment and recreate it from a requirements file in a few minutes.

It also helps to record the exact PyTorch version in your project notes. When you later share code or revisit an old experiment, knowing the version explains differences in behaviour, such as changed defaults or removed functions. Print torch.__version__ at the start of important training runs and write it to your log file so the information travels with your results.

Checklist

  1. Create and activate a virtual environment.
  2. Copy the install command from pytorch.org for your platform.
  3. Run the verification script and note your device.
  4. Save the working versions with pip freeze > requirements.txt so you can reproduce the setup.

With a working install you are ready to learn the object everything else is built from: the tensor.

Was this article helpful?

Comments

No comments yet. Have a question or a tip? Be the first to share it.

Leave a comment

Comments are reviewed before they appear.