PyTorch is one of the most popular open-source libraries for machine learning and deep learning. Developers use it for tasks like image recognition, natural language processing, and AI model training. The installation steps differ based on your operating system and whether you have a GPU, so this guide covers each setup correctly so you do not waste time with the wrong command.

Step 1: Check Your System Requirements
Before installing PyTorch, confirm your system meets these requirements:
- Python 3.9 or newer
- pip package manager (comes with Python)
- Windows 10/11, macOS 12+, or a modern Linux distribution
If you plan to use GPU acceleration on Windows or Linux, your system needs an NVIDIA GPU with updated drivers. On macOS, Apple Silicon chips (M1, M2, M3, M4) use a different GPU backend called MPS, covered in the macOS section below.
Step 2: Set Up a Virtual Environment (Recommended)
Installing PyTorch into a virtual environment keeps it isolated from your system Python and other projects. Skipping this step often causes dependency conflicts later.
Windows (Command Prompt or PowerShell):
python -m venv pytorch-env
pytorch-env\Scripts\activatemacOS and Linux:
python3 -m venv pytorch-env
source pytorch-env/bin/activateOnce activated, your terminal prompt shows the environment name. Run all remaining commands inside this environment.
If you use Anaconda for Python environments, skip to the Conda installation section at the bottom of this guide.
Step 3: Upgrade pip
An outdated pip version causes installation errors and downloads the wrong PyTorch build. Update it before proceeding.
python -m pip install --upgrade pipStep 4: Install PyTorch on Windows
CPU version (works on all Windows systems):
pip install torch torchvision torchaudioCUDA 12.4 (recommended for NVIDIA GPUs):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124CUDA 11.8 (for older NVIDIA GPUs):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118Before installing a CUDA build, make sure your NVIDIA drivers are up to date. Outdated or corrupted drivers can cause system instability. If you have experienced a blue screen after installing a graphics driver, resolve that first before adding CUDA software on top.
Do not install a CUDA build if your system does not have an NVIDIA GPU. Use the CPU version instead.
Step 5: Install PyTorch on macOS
The correct command depends on whether you have an Apple Silicon Mac or an Intel Mac.
Apple Silicon (M1, M2, M3, M4) with MPS GPU acceleration:
pip install torch torchvision torchaudioPyTorch detects Apple Silicon automatically and includes MPS support in the standard package. No separate GPU build is required. MPS lets PyTorch run computations on the Mac GPU through Apple’s Metal framework.
Intel Mac (CPU only):
pip install torch torchvision torchaudioIntel Macs do not support CUDA or MPS, so the standard CPU build is the correct choice.
Step 6: Install PyTorch on Linux
CPU version:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpuCUDA 12.4 (recommended for NVIDIA GPUs):
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124CUDA 11.8 (for older NVIDIA GPUs):
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118On Linux, use pip3 if your system has both Python 2 and Python 3 installed to avoid running the wrong pip.
Step 7: Install PyTorch Using Conda (Alternative)
If you manage Python environments through Anaconda, use conda to install PyTorch instead of pip. This avoids conflicts between conda and pip packages.
CPU version:
conda install pytorch torchvision torchaudio cpuonly -c pytorchCUDA 12.4:
conda install pytorch torchvision torchaudio pytorch-cuda=12.4 -c pytorch -c nvidiaCreate a dedicated conda environment first to keep PyTorch isolated:
conda create -n pytorch-env python=3.11
conda activate pytorch-envStep 8: Verify the Installation
After installation, confirm PyTorch works correctly. Open a Python prompt or a script in Visual Studio Code and run:
Check PyTorch version:
import torch
print(torch.__version__)Check NVIDIA GPU availability (Windows and Linux):
import torch
print(torch.cuda.is_available())
print(torch.cuda.get_device_name(0)) # Prints your GPU model if detectedIf torch.cuda.is_available() prints True, PyTorch detects your GPU correctly.
Check Apple Silicon MPS availability (macOS):
import torch
print(torch.backends.mps.is_available())Run a basic tensor test to confirm the install works end to end:
import torch
x = torch.rand(3, 3)
print(x)If the output shows a 3×3 matrix of random numbers, PyTorch is installed and working.
Common PyTorch Installation Errors and Fixes
Common PyTorch Installation Errors and Fixes
Error 1: Running pip install pytorch Instead of torch
The package name on PyPI is torch, not pytorch. Running pip install pytorch installs a completely different, unrelated package. Always use:
bash
pip install torch torchvision torchaudioError 2: Installing a CUDA Build Without an NVIDIA GPU
PyTorch installs without error but torch.cuda.is_available() returns False. If your system has no NVIDIA GPU, use the CPU version or, on macOS Apple Silicon, use the standard build with MPS.
Error 3: Skipping the pip Upgrade Step
Older pip versions sometimes download the wrong wheel file for your platform. Run this before any PyTorch installation:
bash
python -m pip install --upgrade pipError 4: Mixing Multiple Python Versions
If you have Python 3.10 and 3.12 both installed, pip may install PyTorch into the wrong Python. Activate a virtual environment first to pin which Python receives the package.
Error 5: torch.cuda.is_available() Returns False After Installing a CUDA Build
This usually means your NVIDIA driver is outdated or does not match the CUDA version. Check the driver version in Device Manager on Windows, then download the latest driver from NVIDIA’s website. CUDA 12.4 requires driver version 525.60.13 or newer on Linux and 527.41 or newer on Windows.
Error 6: ModuleNotFoundError: No module named 'torch'
You installed PyTorch outside the active environment. Activate your virtual environment or conda environment, then reinstall.
Installing PyTorch is straightforward once you use the correct command for your system and hardware. The CPU version works well for learning and smaller models. The CUDA version and MPS backend unlock significantly faster training for larger deep learning workloads. Once installed, you can start building and training models right away.
When new PyTorch versions release, upgrade with:
pip install torch torchvision torchaudio --upgradeFAQs:
Does PyTorch support Apple Silicon Macs?
Yes. PyTorch supports Apple Silicon (M1, M2, M3, M4) through the MPS backend. Install the standard pip build and check availability with torch.backends.mps.is_available().
How do I know if PyTorch is using my GPU?
Run torch.cuda.is_available() in Python. If it returns True, PyTorch detects your NVIDIA GPU. On macOS, run torch.backends.mps.is_available() instead.
What is the difference between the CPU and CUDA version of PyTorch?
The CPU version runs on any system but is slower for large models. The CUDA version uses your NVIDIA GPU for faster training and inference, but requires a compatible NVIDIA driver.
Can I install PyTorch without a virtual environment?
Yes, but it is not recommended. Installing without a virtual environment can cause conflicts with other Python packages and makes it harder to manage multiple projects.
Why does torch.cuda.is_available() return False after installing the CUDA build?
This usually means your NVIDIA driver is outdated or does not match the installed CUDA version. Update your driver from the NVIDIA website and ensure it meets the minimum version for CUDA 12.4 or 11.8.
