PyTorch Install Setup
Installing PyTorch correctly from the start saves you hours of troubleshooting later. This guide walks you through installing PyTorch on your machine, whether you use Windows, macOS, or Linux, and whether you have a GPU or not.
What You Need Before Installing
Before installing PyTorch, make sure you have the following tools ready on your system.
- Python 3.8 or later — PyTorch requires Python. Download it from python.org if you don't have it.
- pip or conda — these are package managers that install Python libraries. pip comes with Python. conda comes with Anaconda.
- A terminal or command prompt — you run installation commands here.
Choosing the Right Installation
PyTorch offers different builds depending on your hardware. Pick the one that matches your system.
Do you have an NVIDIA GPU?
|
Yes ─────────────→ Install PyTorch with CUDA
|
No ──────────────→ Install PyTorch CPU-only version
CPU-Only Installation
Most beginners start with the CPU version. It works on any machine and is perfect for learning the basics.
pip install torch torchvision torchaudioGPU Installation (CUDA)
If you have an NVIDIA GPU and want faster training, install the CUDA-enabled version. First, check your CUDA version by running nvidia-smi in your terminal. Then go to the official PyTorch website (pytorch.org) and use the install selector to generate the exact command for your setup.
A typical GPU install command looks like this:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121The cu121 part refers to CUDA version 12.1. Replace it with your actual CUDA version.
Installing with Conda (Alternative Method)
If you use Anaconda or Miniconda, you can install PyTorch through conda. First, create a new environment to keep things clean:
conda create -n pytorch-env python=3.10
conda activate pytorch-env
conda install pytorch torchvision torchaudio -c pytorchUsing a separate environment prevents PyTorch from clashing with other Python libraries you already have installed.
Verifying the Installation
After installation, open a Python shell or script and run these lines to confirm everything works:
import torch
print(torch.__version__)
print(torch.cuda.is_available())You should see output similar to this:
2.2.0 True ← shows True if GPU is available, False if CPU-only
If you see False for CUDA and you expected True, double-check that your CUDA version matches the PyTorch build you installed.
Setting Up a Code Editor
Jupyter Notebook
Jupyter Notebook is the most popular environment for learning PyTorch. It lets you write and run code in small blocks and see output immediately below each block — great for experimenting.
pip install notebook
jupyter notebookVS Code
Visual Studio Code with the Python extension is a solid choice for writing larger PyTorch projects. It offers code completion, debugging, and a built-in terminal.
Google Colab
If you don't want to install anything on your machine, use Google Colab. It runs in your browser and provides free GPU access. Just go to colab.research.google.com and create a new notebook. PyTorch is already installed on Colab — you just import it.
Project Folder Structure
Organize your PyTorch projects in a clean folder structure from the start. This makes your code easier to manage as it grows.
my-pytorch-project/ ├── data/ ← store datasets here ├── models/ ← save trained models here ├── notebooks/ ← Jupyter notebooks for experiments ├── src/ ← your Python source files └── requirements.txt ← list of libraries your project needs
Common Installation Problems
Wrong Python Version
PyTorch requires Python 3.8 or higher. Run python --version to check. If you have an older version, install a newer Python first.
pip Not Found
On some systems, pip is called pip3. Try pip3 install torch instead of pip install torch.
CUDA Mismatch
The most common GPU problem is a mismatch between the PyTorch CUDA version and the actual CUDA toolkit on your machine. Always visit the official PyTorch install page to get the exact command for your CUDA version rather than copying a command from an old tutorial.
ModuleNotFoundError
If Python says it cannot find torch after installation, you likely installed PyTorch in a different Python environment than the one you are using to run code. Activate the correct environment first.
Summary
Installing PyTorch takes three steps: verify you have Python, pick the right build (CPU or GPU), and run the install command. Use Jupyter Notebook or Google Colab to write your first PyTorch programs. Confirm the installation by importing torch and checking the version. A clean project folder structure keeps your work organized as you learn more.
