centos

如何在CentOS上安装PyTorch库

小樊
41
2025-10-15 14:29:48
栏目: 智能运维

在CentOS上安装PyTorch库,可以按照以下步骤进行:

方法一:使用pip安装

  1. 更新系统包

    sudo yum update -y
    
  2. 安装必要的依赖项

    sudo yum install -y python3 python3-pip python3-devel
    
  3. 安装PyTorch: PyTorch官方提供了多种安装方式,可以根据你的CUDA版本选择合适的命令。以下是一些常见的安装命令示例:

    • CPU版本

      pip3 install torch torchvision torchaudio
      
    • GPU版本(CUDA 11.7)

      pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
      
    • GPU版本(CUDA 11.6)

      pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116
      
    • GPU版本(CUDA 11.3)

      pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
      
    • GPU版本(CUDA 10.2)

      pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu102
      

    请根据你的CUDA版本选择合适的命令。

  4. 验证安装: 打开Python解释器并尝试导入PyTorch:

    import torch
    print(torch.__version__)
    

方法二:使用conda安装

如果你已经安装了Anaconda或Miniconda,可以使用conda来安装PyTorch:

  1. 更新conda

    conda update conda
    
  2. 创建一个新的conda环境(可选):

    conda create -n pytorch_env python=3.8
    conda activate pytorch_env
    
  3. 安装PyTorch: PyTorch官方提供了conda安装命令,可以根据你的CUDA版本选择合适的命令。以下是一些常见的安装命令示例:

    • CPU版本

      conda install pytorch torchvision torchaudio cpuonly -c pytorch
      
    • GPU版本(CUDA 11.7)

      conda install pytorch torchvision torchaudio cudatoolkit=11.7 -c pytorch
      
    • GPU版本(CUDA 11.6)

      conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch
      
    • GPU版本(CUDA 11.3)

      conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
      
    • GPU版本(CUDA 10.2)

      conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
      

    请根据你的CUDA版本选择合适的命令。

  4. 验证安装: 打开Python解释器并尝试导入PyTorch:

    import torch
    print(torch.__version__)
    

通过以上步骤,你应该能够在CentOS上成功安装PyTorch库。如果有任何问题,请参考PyTorch官方文档或相关社区论坛寻求帮助。

0
看了该问题的人还看了