温馨提示×

linux怎么安装pytorch

小樊
81
2026-10-04 11:25:43
栏目: 智能运维

在 Linux 上安装 PyTorch,通常推荐使用 conda 或 pip,并且要根据你是否使用 NVIDIA GPU(CUDA) 来选择对应版本。

下面按常见情况说明:


一、先确认环境

1. 查看是否有 NVIDIA GPU

nvidia-smi
  • 有输出 → 支持 CUDA(可用 GPU 版)
  • 无输出 → 只能用 CPU 版

2. 查看 Python 版本

python --version

建议 Python ≥ 3.8


二、使用 pip 安装(最简单)

✅ CPU 版

pip install torch torchvision torchaudio

✅ GPU 版(以 CUDA 11.8 为例)

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

如果你用 CUDA 12.1:

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

三、使用 conda 安装(推荐,环境隔离好)

1. 创建环境

conda create -n pytorch python=3.10
conda activate pytorch

2. 安装 PyTorch

CPU 版

conda install pytorch torchvision torchaudio cpuonly -c pytorch

GPU 版(CUDA 11.8)

conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia

四、验证安装

python -c "import torch; print(torch.__version__)"

查看是否能用 GPU:

python -c "import torch; print(torch.cuda.is_available())"
  • True → GPU 可用
  • False → 当前是 CPU 版或未正确安装驱动

五、常见问题

1. 下载慢

使用国内镜像(如清华):

pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple

2. 系统缺少依赖

Ubuntu 示例:

sudo apt update
sudo apt install python3-pip python3-venv

如果你告诉我:

  • 发行版(Ubuntu / CentOS / Arch)
  • 是否有 GPU
  • Python 版本

我可以给你精确的一条命令安装方式。

0 踩