PyTorch Installation
PyTorch is a popular deep learning framework that supports both CPU and GPU computing.
Supported operating systems
- Windows: Windows 10 or later (64-bit)
- macOS: macOS 10.15 (Catalina) or later
- Linux: mainstream distributions (Ubuntu 18.04+, CentOS 7+, RHEL 7+, etc.)
Python version requirements
- Recommended version:Python 3.8 - 3.11
- Minimum requirements:Python 3.7
- Note: support for Python 3.12+ may be limited; a stable version is recommended
Hardware requirements
- CPU: x86_64 processor with SSE4.2 instruction set support
- Memory: at least 4GB RAM (8GB+ recommended)
- Storage: at least 3GB of free space
- GPU(Optional): NVIDIA GPU with CUDA Compute Capability 3.5+
CUDA Compatibility (GPU version)
| PyTorch version | Supported CUDA versions | Recommended CUDA version |
|---|---|---|
| 2.1.x | 11.8, 12.1 | 12.1 |
| 2.0.x | 11.7, 11.8 | 11.8 |
| 1.13.x | 11.6, 11.7 | 11.7 |
Preparations Before Installation
Check system information
Windows:
# 检查 Windows 版本
winver
# Check Python version
python --version
# Check for NVIDIA GPU
nvidia-smi
macOS
# 检查 macOS 版本
sw_vers
# Check Python version
python3 --version
# Check for compatible GPU (Apple Silicon)
system_profiler SPDisplaysDataType
Linux
# 检查发行版信息
cat /etc/os-release
# Check Python version
python3 --version
# Check NVIDIA GPU
nvidia-smi
# Check CUDA version (if installed)
nvcc --version
Python environment management
Using Anaconda/Miniconda:
# 下载并安装 Miniconda
# Windows: https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe
# macOS: https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
# Linux: https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
# Create dedicated environment
conda create -n pytorch_env python=3.10
conda activate pytorch_env
Using venv (built into Python)
# 创建虚拟环境
python -m venv pytorch_env
# Activate environment
# Windows
pytorch_env\Scripts\activate
# macOS/Linux
source pytorch_env/bin/activate
Installing PyTorch
PyTorch officially provides several installation methods; you can install it via pip or conda.
CPU Version Installation
Install pytorch using pip:
# Latest stable version
pip install torch torchvision torchaudio
# Specify version
pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0
# CPU only version (smaller installation package)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
Install using conda:
If you use Anaconda or Miniconda to manage your Python environment, installing PyTorch with conda may be simpler and more efficient.
# Install from conda-forge
conda install pytorch torchvision torchaudio cpuonly -c pytorch
# or from the conda-forge channel
conda install pytorch torchvision torchaudio -c conda-forge
If you are not familiar with Anaconda, you can refer to: Anaconda Tutorial
Install via the PyTorch official website
Visit the official PyTorch website https://pytorch.org/get-started/locally/, which provides a handy tool that recommends installation commands based on your system configuration (operating system, package manager, Python version, and CUDA version).

Install from Source
If you need to install PyTorch from source, or want to try the latest development version, you can use the following command:
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
python setup.py install
This clones the PyTorch source code from GitHub and installs it using setup.py.
GPU Version Installation (CUDA)
Install CUDA (if needed):
# Ubuntu/Debian
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda-repo-ubuntu2004-12-1-local_12.1.0-530.30.02-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu2004-12-1-local_12.1.0-530.30.02-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu2004-12-1-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda
# CentOS/RHEL
sudo yum install -y https://developer.download.nvidia.com/compute/cuda/repos/rhel8/x86_64/cuda-repo-rhel8-12.1.0-1.x86_64.rpm
sudo yum clean all
sudo yum -y install cuda
Install the PyTorch GPU version:
# CUDA 12.1 版本
pip install torch torchvision torchaudio
# CUDA 11.8 version
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
# Use conda
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
macOS-specific Notes
Apple Silicon (M1/M2) Mac:
# 使用 Metal Performance Shaders (MPS) 后端
pip install torch torchvision torchaudio
# Verify MPS availability
python -c "import torch; print(torch.backends.mps.is_available())"
Intel Mac:
# 标准安装
pip install torch torchvision torchaudio
Verify the installation
To ensure PyTorch is installed correctly, we can verify the installation by running the following PyTorch code:
Example
import
torch
The version of the currently installed PyTorch library
(
torch.version
)
Check whether CUDA is available, i.e., whether your system has an NVIDIA GPU
(
torch.
cuda
.
is_available
(
)
)
If torch.cuda.is_available() outputs True, then PyTorch has successfully recognized your GPU.
A simple example: construct a randomly initialized tensor:
Example
import
torch
x
=
torch.
rand
(
5
,
3
)
(
x
)
If the installation is successful, the output will look similar to the following:
tensor([[0.3380, 0.3845, 0.3217],
[0.8337, 0.9050, 0.2650],
[0.2979, 0.7141, 0.9069],
[0.1449, 0.1132, 0.1375],
[0.4675, 0.3947, 0.1426]])