Ubuntu系统更新PyTorch库的常用方法
准备工作
打开终端(快捷键:Ctrl + Alt + T),升级pip至最新版本以避免兼容性问题:
pip install --upgrade pip
卸载旧版本(可选但推荐)
为避免旧版本残留导致的依赖冲突,建议先卸载现有PyTorch及相关库:
pip uninstall torch torchvision torchaudio
安装最新版本PyTorch
pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu118 # 替换为你的CUDA版本(如cu117、cu113)
pip install torch torchvision torchaudio
验证更新结果
运行Python交互环境,检查PyTorch版本:
import torch
print(torch.__version__) # 输出最新版本号(如2.1.0)
准备工作
打开终端,激活目标conda环境(若使用虚拟环境):
conda activate your_environment_name # 替换为你的环境名称
更新conda工具
确保conda为最新版本,提升更新稳定性:
conda update conda
卸载旧版本(可选但推荐)
清理旧版PyTorch及关联组件,避免冲突:
conda remove pytorch torchvision torchaudio cudatoolkit
安装最新版本PyTorch
conda install pytorch torchvision torchaudio cudatoolkit=11.8 -c pytorch
cpuonly参数:conda install pytorch torchvision torchaudio cpuonly -c pytorch
验证更新结果
在终端中运行以下命令,查看PyTorch版本信息:
conda list torch # 确认版本号已更新
venv或conda环境),避免影响系统全局Python环境。sudo add-apt-repository ppa:deadsnakes/ppa添加PPA后安装,但需自行处理依赖问题。