在Linux系统上安装PyTorch,你可以选择使用pip或conda包管理器。以下是两种常见的安装方法:
打开终端: 打开你的Linux终端。
更新pip: 确保你的pip是最新版本,可以使用以下命令更新pip:
pip install --upgrade pip
安装PyTorch: 根据你的CUDA版本和系统架构,选择合适的PyTorch安装命令。你可以在PyTorch官方网站的安装指南中找到这些命令。以下是一些常见的安装命令示例:
CPU版本:
pip install torch torchvision torchaudio
CUDA 11.3版本:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
CUDA 11.6版本:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116
CUDA 11.7版本:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
CUDA 12.0版本:
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu120
打开终端: 打开你的Linux终端。
更新conda: 确保你的conda是最新版本,可以使用以下命令更新conda:
conda update conda
创建新的conda环境(可选): 如果你不想在当前环境中安装PyTorch,可以创建一个新的conda环境:
conda create -n pytorch_env python=3.9
conda activate pytorch_env
安装PyTorch: 根据你的CUDA版本和系统架构,选择合适的PyTorch安装命令。你可以在PyTorch官方网站的安装指南中找到这些命令。以下是一些常见的安装命令示例:
CPU版本:
conda install pytorch torchvision torchaudio cpuonly -c pytorch
CUDA 11.3版本:
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
CUDA 11.6版本:
conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch
CUDA 11.7版本:
conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
CUDA 12.0版本:
conda install pytorch torchvision torchaudio cudatoolkit=12.0 -c pytorch
安装完成后,你可以通过以下命令验证PyTorch是否安装成功:
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # 如果使用GPU版本,应该返回True
如果一切正常,你应该能够看到PyTorch的版本号,并且如果使用的是GPU版本,torch.cuda.is_available()
应该返回True
。
希望这些步骤能帮助你在Linux系统上成功安装PyTorch!