温馨提示×

Ubuntu PyTorch如何配置环境

小樊
50
2025-08-19 22:54:15
栏目: 智能运维

以下是在Ubuntu上配置PyTorch环境的步骤:

安装Python和pip(若未安装)

sudo apt update sudo apt install python3 python3-pip 

创建虚拟环境(可选但推荐)

python3 -m venv pytorch_env source pytorch_env/bin/activate 

安装PyTorch

  • 使用pip安装
    • CPU版本pip3 install torch torchvision torchaudio
    • GPU版本(以CUDA 11.8为例)pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118
  • 使用conda安装
    • CPU版本conda install pytorch torchvision torchaudio cpuonly -c pytorch
    • GPU版本(以CUDA 11.8为例)conda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch -c nvidia

验证安装

python3 -c "import torch; print(torch.__version__); print(torch.cuda.is_available())" 

若安装成功,会显示PyTorch版本号,GPU版本会显示True

0