在Ubuntu系统下安装PyTorch及其依赖,可以按照以下步骤进行:
首先,确保你的系统包是最新的:
sudo apt update
sudo apt upgrade
PyTorch需要一些基本的系统库和工具:
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 python3 python3-pip
如果你有NVIDIA GPU并且想使用GPU加速,需要安装CUDA和cuDNN。
sudo dpkg -i cuda-repo-<distro>_<version>_amd64.deb
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/<distro>/x86_64/7fa2af80.pub
sudo apt update
sudo apt install cuda
tar -xzvf cudnn-<version>-linux-x64-v<version>.tgz
sudo cp cuda/include/cudnn*.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
你可以使用pip来安装PyTorch。根据你的CUDA版本选择合适的安装命令。
pip3 install torch torchvision torchaudio
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu117
安装完成后,可以通过以下命令验证PyTorch是否安装成功:
import torch
print(torch.__version__)
print(torch.cuda.is_available()) # 如果安装了GPU版本,应该返回True
通过以上步骤,你应该能够在Ubuntu系统下成功安装PyTorch及其依赖。如果有任何问题,请参考PyTorch官方文档或社区支持。