Skip to content

PyTorch - Installation

PyTorch is a popular open-source deep learning framework. This guide outlines the modern approach to installing PyTorch. It’s highly recommended to use a virtual environment for managing dependencies.

Step 1: Choose Your Installation Method and Command

Section titled “Step 1: Choose Your Installation Method and Command”

The best way to install PyTorch is by using the command generator on the official PyTorch website. This ensures you get the correct version for your operating system, package manager (pip or conda), Python version, and CUDA version (if you have an NVIDIA GPU and want to use it).

  1. Navigate to the official PyTorch website: https://pytorch.org/get-started/locally/.
  2. On the website, select your preferences for:
    • PyTorch Build (Stable is recommended for most users).
    • Your OS (Linux, Mac, Windows).
    • Package Manager (Conda, Pip, LibTorch, Source).
    • Language (Python or C++/Java).
    • Compute Platform (CUDA version if you have an NVIDIA GPU, or CPU if not).
  3. The website will generate the exact command you need to run in your terminal. Copy this command.

Before running the installation command, create and activate a virtual environment. This isolates your project dependencies.

Using venv (Python’s built-in tool):

# Create a virtual environment (e.g., named 'pytorch_env')
python -m venv pytorch_env
# Activate the environment
# On Windows:
pytorch_env\Scripts\activate
# On macOS/Linux:
source pytorch_env/bin/activate

Using conda:

# Create a conda environment (e.g., named 'pytorch_env') with a specific Python version
conda create -n pytorch_env python=3.9
# Activate the environment
conda activate pytorch_env

Once your virtual environment is activated, paste and run the command you copied from the PyTorch website. For example, a common pip command for CPU-only might look like:

# Example pip command (for CPU, check official site for latest)
pip install torch torchvision torchaudio

And a conda command might look like:

# Example conda command (for CPU, check official site for latest)
conda install pytorch torchvision torchaudio cpuonly -c pytorch

Note: If you have an NVIDIA GPU and want to leverage it, ensure you select the appropriate CUDA version on the PyTorch website, and the generated command will include CUDA-enabled PyTorch builds.

After the installation completes, you can verify it by running a simple Python script:

import torch
# Check PyTorch version
print(f"PyTorch Version: {torch.__version__}")
# Check if CUDA (GPU support) is available
is_cuda_available = torch.cuda.is_available()
print(f"CUDA Available: {is_cuda_available}")
if is_cuda_available:
# Get the CUDA version PyTorch was compiled with
print(f"CUDA Version: {torch.version.cuda}")
# Get the number of GPUs available
print(f"Number of GPUs: {torch.cuda.device_count()}")
# Get the name of the current GPU
print(f"Current GPU Name: {torch.cuda.get_device_name(torch.cuda.current_device())}")
else:
print("PyTorch is using CPU.")

Save this as a Python file (e.g., verify_pytorch.py) and run it from your terminal (python verify_pytorch.py). If PyTorch is installed correctly, it will print the version and CUDA status without errors.

Common Issues:

  • Ensure your GPU drivers are up to date if installing with CUDA support.
  • Double-check that your virtual environment is activated before installing and verifying.