下面给你一份在 Linux 上安装 PyTorch 的常用方法,适用于 Ubuntu / CentOS / Debian 等常见发行版。
大多数 Linux 已自带 Python3,确认一下:
python3 --version
pip3 --version
如果没有:
# Ubuntu / Debian
sudo apt update
sudo apt install python3 python3-pip python3-venv
# CentOS
sudo yum install python3 python3-pip
python3 -m venv pytorch_env
source pytorch_env/bin/activate
访问:https://pytorch.org/get-started/locally/
CPU 版本
pip3 install torch torchvision torchaudio
CUDA 11.8(有 NVIDIA 显卡)
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
CUDA 12.1
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
conda create -n pytorch python=3.10
conda activate pytorch
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
python3
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # True 表示 GPU 可用
使用国内镜像:
pip3 install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple
nvidia-smi 能正常显示加 --user 或使用虚拟环境:
pip3 install --user torch
python3 -m venv env
source env/bin/activate
pip install torch torchvision torchaudio
如果你愿意,可以告诉我:
我可以给你精确的一条安装命令。