在 Ubuntu 上,gcc 可以安装多个版本,并且可以共存,没有硬性数量限制(主要受磁盘空间和软件源支持限制)。
apt-cache search gcc | grep '^gcc-'
常见版本如:
sudo apt update
sudo apt install gcc-9 gcc-11 gcc-12
Ubuntu 使用 update-alternatives 管理默认版本:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 110
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120
切换版本:
sudo update-alternatives --config gcc
不切换默认版本也能直接用:
gcc-9 main.c
gcc-12 main.c
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-14
✅ 总结:Ubuntu 支持同时安装任意多个 gcc 版本,通过 update-alternatives 或带版本号命令使用。