在 Linux 下安装 PyTorch,通常分为 CPU 版 和 GPU(CUDA)版,推荐用 conda 或 pip。下面给你最常用、最稳妥的方式。
uname -a
python3 --version
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
创建环境:
conda create -n torch python=3.10
conda activate torch
pip install torch torchvision torchaudio
conda install pytorch torchvision torchaudio cpuonly -c pytorch
nvidia-smi
看右上角 CUDA Version,比如 12.1
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
或 conda:
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
⚠️ CUDA 版本必须 ≤ 驱动支持的版本
import torch
print(torch.__version__)
print(torch.cuda.is_available())
输出:
True → GPU 可用False → 当前是 CPU 版或未正确安装 CUDApip install -i https://pypi.tuna.tsinghua.edu.cn/simple torch torchvision
用 which python 确认当前环境
如果你告诉我:
我可以给你精确的一条命令安装方案。