0


如何在 Ubuntu 18.04 上升级 gcc 到 9.4:一步步详细指南

文章目录


问题描述

在尝试编译 PyTorch 时遇到的报错提示需要 GCC 版本 9.3 或更高,但系统中安装的是版本 7.5.0。因此,需要升级 GCC 到至少 9.3 版本。

damon@damon-virtual-machine:/opt/pytorch/build$ sudo python ../tools/build_libtorch.py
cmake -DBUILD_PYTHON=False -DBUILD_TEST=True -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/opt/pytorch/torch -DCMAKE_PREFIX_PATH=/usr/lib/python3.6/site-packages -DNUMPY_INCLUDE_DIR=/usr/lib/python3/dist-packages/numpy/core/include -DPYTHON_EXECUTABLE=/usr/bin/python -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DUSE_NUMPY=True /opt/pytorch
-- The CXX compiler identification is GNU 7.5.0-- The C compiler identification is GNU 7.5.0-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler:/usr/bin/c++- skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler:/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
CMake Error at CMakeLists.txt:54(message):
  GCC-9.3or newer is required to compile PyTorch, but found 7.5.0-- Configuring incomplete, errors occurred!
See also "/opt/pytorch/build/build/CMakeFiles/CMakeOutput.log".

问题解决

1.添加 Toolchain 测试 PPA

Ubuntu 18.04 默认的源中可能没有 GCC-9.3 或更新版本,因此可以通过添加 Ubuntu Toolchain 测试 PPA 来获取更新的编译器版本。在终端运行以下命令来添加 PPA:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudoapt update

2.安装 GCC 和 G++

安装 GCC-9 和 G+±9(如果你的系统是用 C++ 编写的,也需要 G++):

sudoaptinstall gcc-9 g++-9

3.更新替代版本

如果系统中安装了多个 GCC 版本,需要配置系统使用新安装的版本作为默认版本。使用以下命令更新替代配置:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90--slave /usr/bin/g++ g++ /usr/bin/g++-9

4.验证安装

验证安装的 GCC 版本是否正确:

gcc --version

5.出现报错

在执行 sudo apt install gcc-9 g+±9 时,出现以下报错。

damon@damon-virtual-machine:~$ sudoaptinstall gcc-9 g++-9
[sudo] damon 的密码: 
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列软件包是自动安装的并且现在不需要了:
  gir1.2-goa-1.0 gir1.2-snapd-1 linux-hwe-5.4-headers-5.4.0-147
使用'sudo apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
  cpp-9 gcc-11-base gcc-13-base gcc-9-base libasan5 libatomic1 libcc1-0
  libgcc-9-dev libgcc-s1 libgomp1 libitm1 liblsan0 libquadmath0
  libstdc++-9-dev libstdc++6 libtsan0 libubsan1
建议安装:
  gcc-9-locales g++-9-multilib gcc-9-doc libstdc++6-9-dbg gcc-9-multilib
  libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan5-dbg
  liblsan0-dbg libtsan0-dbg libubsan1-dbg libquadmath0-dbg libstdc++-9-doc
下列【新】软件包将被安装:
  cpp-9 g++-9 gcc-11-base gcc-13-base gcc-9 gcc-9-base libasan5 libgcc-9-dev
  libgcc-s1 libstdc++-9-dev libubsan1
下列软件包将被升级:
  libatomic1 libcc1-0 libgomp1 libitm1 liblsan0 libquadmath0 libstdc++6
  libtsan0
升级了 8 个软件包,新安装了 11 个软件包,要卸载 0 个软件包,有 99 个软件包未被升级。
需要下载 29.7 MB/30.5 MB 的归档。
解压缩后会消耗 122 MB 的额外空间。
N: 忽略‘99proxy.save.1’(于目录‘/etc/apt/apt.conf.d/’),鉴于它的文件扩展名无效
N: 忽略‘99proxy.save.2’(于目录‘/etc/apt/apt.conf.d/’),鉴于它的文件扩展名无效
您希望继续执行吗? [Y/n] Y
获取:1 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 cpp-9 amd64 9.4.0-1ubuntu1~18.04 [7,489 kB]
获取:2 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libcc1-0 amd64 13.1.0-8ubuntu1~18.04 [43.8 kB]
获取:3 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libgomp1 amd64 13.1.0-8ubuntu1~18.04 [127 kB]
错误:3 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libgomp1 amd64 13.1.0-8ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达) 无法连接上 ppa.launchpad.net:80 (185.125.190.80)。 - connect (111: 拒绝连接)[IP: 185.125.190.80 80]
错误:4 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libitm1 amd64 13.1.0-8ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:5 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libatomic1 amd64 13.1.0-8ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:6 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libasan5 amd64 9.4.0-1ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:7 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 liblsan0 amd64 13.1.0-8ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:8 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 gcc-11-base amd64 11.4.0-2ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:9 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libtsan0 amd64 11.4.0-2ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:10 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libubsan1 amd64 13.1.0-8ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:11 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libquadmath0 amd64 13.1.0-8ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:12 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libgcc-9-dev amd64 9.4.0-1ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:13 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 gcc-9 amd64 9.4.0-1ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:14 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 libstdc++-9-dev amd64 9.4.0-1ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
错误:15 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu bionic/main amd64 g++-9 amd64 9.4.0-1ubuntu1~18.04
  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
已下载 7,171 kB,耗时 1分 26秒 (83.3 kB/s)
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-13/libgomp1_13.1.0-8ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达) 无法连接上 ppa.launchpad.net:80 (185.125.190.80)。 - connect (111: 拒绝连接)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-13/libitm1_13.1.0-8ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-13/libatomic1_13.1.0-8ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-9/libasan5_9.4.0-1ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-13/liblsan0_13.1.0-8ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-11/gcc-11-base_11.4.0-2ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-11/libtsan0_11.4.0-2ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-13/libubsan1_13.1.0-8ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-13/libquadmath0_13.1.0-8ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-9/libgcc-9-dev_9.4.0-1ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-9/gcc-9_9.4.0-1ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-9/libstdc++-9-dev_9.4.0-1ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 无法下载 http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu/pool/main/g/gcc-9/g++-9_9.4.0-1ubuntu1~18.04_amd64.deb  无法发起与 ppa.launchpad.net:80 (2620:2d:4000:1::81) 的连接 - connect (101: 网络不可达)[IP: 185.125.190.80 80]
E: 有几个软件包无法下载,要不运行 apt-get update 或者加上 --fix-missing 的选项再试试?
damon@damon-virtual-machine:~$ 

解决方案

检查网络连接:

  1. 确保你的设备已连接到互联网并可以访问外部网站。
  2. 如果你使用代理服务器,检查代理设置是否正确。
  3. 确认没有防火墙或其他网络安全设备阻止你访问外部 PPA 源。

更新软件源和系统:

  1. 在终端运行 sudo apt-get update 命令更新你的软件源列表。这可以确保你的系统有最新的软件信息
  2. 运行 sudo apt-get upgrade 来更新已安装的软件包到最新版本。

尝试使用 --fix-missing 选项:

  1. 使用命令 sudo apt install gcc-9 g+±9 --fix-missing 可以尝试解决因缺失软件包而导致的问题。

更换或禁用问题 PPA 源:

  1. 如果持续无法从当前 PPA 源下载,考虑暂时禁用该源。你可以编辑 /etc/apt/sources.list 或 /etc/apt/sources.list.d/ 目录下的相关文件,注释掉引起问题的 PPA 源。
  2. 可以考虑添加其他可靠的 PPA源或使用默认的 Ubuntu 软件源。

更换默认的Ubuntu软件源

使用主服务器即可即可

标签: ubuntu linux 运维

本文转载自: https://blog.csdn.net/hzx19901102/article/details/138465656
版权归原作者 草莓奶忻 所有, 如有侵权,请联系我们删除。

“如何在 Ubuntu 18.04 上升级 gcc 到 9.4:一步步详细指南”的评论:

还没有评论