Note: With slight modifications, this guide will likely work for most distros but you may need to consult your distro documentation or support forums to confirm.
Maintained by @morrownr
Setup the build environment if not already done:
sudo apt install build-essential
Install multiple C compiler versions (only install the ones you want to add to your system):
sudo apt -y install gcc-10 gcc-11 gcc-12
Use the update-alternatives
tool to create a list of multiple GCC
compiler alternatives:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 12
Check the available C compilers list on your Ubuntu system:
sudo update-alternatives --config gcc
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-11 11 auto mode
1 /usr/bin/gcc-10 10 manual mode
* 2 /usr/bin/gcc-11 11 manual mode
3 /usr/bin/gcc-12 12 manual mode
Press Enter
to keep the current choice [*]
or type selection number.
Check your currently selected compiler version:
gcc --version
Happy compiling!