Ubuntu中更新PyTorch库的常用方法
pip是Python默认的包管理工具,适用于大多数Ubuntu环境(包括虚拟环境)。
Ctrl + Alt + T启动终端。pip show torch,查看已安装的版本信息。pip install --upgrade pip,确保pip为最新版本,避免兼容性问题。pip install --upgrade torch torchvision torchaudio,这会自动下载并安装PyTorch、TorchVision、TorchAudio的最新稳定版。pip install torch==1.12.0 torchvision==0.13.0 torchaudio==0.12.0(替换为所需版本号)。pip show torch,确认Version字段显示为新版本;或在Python中执行import torch; print(torch.__version__),打印版本号。conda是Anaconda的包管理工具,更适合管理复杂的依赖关系(尤其是GPU环境)。
Ctrl + Alt + T。conda activate your_env_name(将your_env_name替换为你的环境名称),确保在目标环境中操作。conda update conda,保持conda版本最新。conda update pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch(将11.8替换为你的CUDA版本,可通过nvcc --version查看)。conda install pytorch torchvision torchaudio cpuonly -c pytorch。conda list torch,检查torch行的版本号是否为新版本;或在Python中执行import torch; print(torch.__version__)。venv或conda环境),避免影响系统全局Python环境。pip uninstall torch torchvision torchaudio;conda:conda remove pytorch torchvision torchaudio)。