在Ubuntu上安装PyTorch及其依赖库,可以按照以下步骤进行:
首先,确保你的系统包列表是最新的:
sudo apt update
PyTorch需要一些基本的系统依赖,包括编译工具和Python开发库。你可以使用以下命令安装这些依赖:
sudo apt install -y build-essential cmake git wget unzip yasm pkg-config libopenblas-dev liblapack-dev libjpeg-dev libpng-dev
如果你还没有安装Python和pip,可以使用以下命令安装:
sudo apt install -y python3 python3-pip
PyTorch提供了多种安装方式,包括通过pip安装预编译的二进制文件或通过源码编译安装。以下是通过pip安装PyTorch的推荐方法:
访问PyTorch官方网站,选择适合你系统的安装命令。以下是一些常见的安装命令示例:
CUDA 11.7(适用于大多数GPU):
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
CPU版本:
pip3 install torch torchvision torchaudio
特定版本的PyTorch: 如果你需要特定版本的PyTorch,可以在URL中指定版本号,例如:
pip3 install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio==0.10.0+cu113 -f https://download.pytorch.org/whl/torch_stable.html
安装完成后,你可以通过以下命令验证PyTorch是否安装成功:
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # 检查CUDA是否可用
如果输出显示了PyTorch的版本号,并且torch.cuda.is_available()
返回True
(如果你安装了CUDA版本),则说明安装成功。
根据你的项目需求,你可能还需要安装其他Python库。你可以使用pip来安装这些库,例如:
pip3 install numpy pandas matplotlib scikit-learn
通过以上步骤,你应该能够在Ubuntu上成功安装PyTorch及其依赖库。