在 Ubuntu 上安装 PyTorch 通常推荐用 conda 或 pip,并且要根据你是否有 NVIDIA GPU 来选择 CPU 版或 CUDA 版。
lsb_release -a
nvidia-smi
python3 --version
建议使用 Python 3.8–3.11
pip install torch torchvision torchaudio
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
CUDA 12.1 可替换为
cu121
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
conda create -n pytorch python=3.10
conda activate pytorch
conda install pytorch torchvision torchaudio cpuonly -c pytorch
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
import torch
print(torch.__version__)
print(torch.cuda.is_available())
True → GPU 可用False → 当前是 CPU 版使用国内镜像:
pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple
确保:
如果你愿意,可以告诉我:
我可以给你精确的一条安装命令。