Ubuntu上PyTorch常见安装与运行问题及解决方法
build-essential、libssl-dev等依赖库会导致编译错误。sudo apt update && sudo apt upgrade);python3 --version),若版本不符,通过conda或pyenv创建指定版本环境;sudo apt install build-essential libssl-dev libffi-dev python3-dev)。torch.cuda.is_available()返回False,或运行时提示“CUDA version not supported”。
nvidia-smi查看显卡支持的CUDA版本;sudo apt remove --purge nvidia-cuda-toolkit),重新安装匹配版本;~/.bashrc中添加export PATH=/usr/local/cuda/bin:$PATH和export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH,执行source ~/.bashrc)。sudo apt purge nvidia-*),避免冲突;--no-drm参数(如sudo sh NVIDIA-Linux-x86_64-XXX.run --no-drm),防止权限问题。pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple;conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/;.whl文件),通过pip install 文件名.whl本地安装。libmkl_intel_lp64.so错误,在~/.bashrc中添加export LD_LIBRARY_PATH=/root/anaconda3/lib:$LD_LIBRARY_PATH(路径根据实际安装位置调整),执行source ~/.bashrc;/usr/local/cuda/lib64已添加至LD_LIBRARY_PATH;torch.cuda.empty_cache()释放闲置显存;Ctrl+Shift+P打开命令面板,选择“Python: Select Interpreter”,指向虚拟环境中的Python(如~/miniconda3/envs/pytorch_env/bin/python);ipykernel(conda install ipykernel),将虚拟环境添加至Jupyter(python -m ipykernel install --user --name=pytorch_env),重启Notebook后选择对应内核。DataLoader时提示“already started”“RuntimeError: received 0 items of ancdata”。
num_workers设置为0(禁用多进程),测试是否能正常运行;sudo mount -o remount,size=8G /dev/shm,将8G改为系统允许的最大值);~,改用绝对路径如/home/user/data)。