gcc@ubuntu安装较新版本gcc

ppa:ubuntu-toolchain-r/test 是 Ubuntu 官方维护的一个 PPA(Personal Package Archive),它专门用于发布较新版本的 GNU 工具链(GCC、G++、GFortran 等),用于补全或替代 Ubuntu 官方主仓库中较老的版本。


🔍 1.PPA 地址详解

https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test

你可以在浏览器中打开上面这个地址查看包含的包(适用于各个 Ubuntu 版本)。


📦 2.提供内容

  • GCC(包括 gcc-7、gcc-8、gcc-9、gcc-10、gcc-11 等)
  • G++、gfortran 等

✅ 3.支持的 Ubuntu 版本

这个 PPA 通常支持以下 Ubuntu LTS 和主流版本:

  • Ubuntu 14.04 (Trusty)
  • Ubuntu 16.04 (Xenial)
  • Ubuntu 18.04 (Bionic)
  • Ubuntu 20.04 (Focal)
  • Ubuntu 22.04 (Jammy)

注意:Ubuntu 16.04 虽然已经 EOL(官方停止支持),但这个 PPA 仍然可能保留了旧版本支持。


✅ 4.添加 PPA 命令

1
2
3
4
sudo apt-get install -y software-properties-common

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

添加之后你就可以安装新版本 GCC

1
sudo apt-get install gcc-7 g++-7

🛠️ 5.使用新版本 GCC 的方法

为了使用你刚安装的新版本,你可以手动设置默认版本:

1
2
3
4
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 70
sudo update-alternatives --config gcc
sudo update-alternatives --config g++