Skip to content

Instantly share code, notes, and snippets.

@cristiarg
Last active May 1, 2025 09:16
Show Gist options
  • Save cristiarg/bd9958913180a43c17a734c2e93674bf to your computer and use it in GitHub Desktop.
Save cristiarg/bd9958913180a43c17a734c2e93674bf to your computer and use it in GitHub Desktop.
Configure multiple GCC versions on Ubuntu
#!/usr/bin/env bash
#sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
#sudo apt update
# find out what points to what
# an indirect way of finding out if alternatives are currently involved
update-alternatives --display gcc
update-alternatives --display g++
update-alternatives --display cc
update-alternatives --display c++
namei `which gcc`
namei `which g++`
namei `which cc`
namei `which c++`
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++
# install latest from repo
sudo apt-get install gcc g++
# install other desired version from repo
sudo apt-get install gcc-8 g++-8
# install gcc and g++ in the order desired
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 80
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 90
# cc and c++ actually point to the already alternative-cofigured gcc and g++ respectively
sudo update-alternatives --set cc /usr/bin/gcc
sudo update-alternatives --set c++ /usr/bin/g++
# if you need to alternate between different versions, a text based selection menu is triggered with:
sudo update-alternatives --config gcc
sudo update-alternatives --config g++
------------------------------------------------------------------------------
# below is the full log of installing versions 11, 12 and 9 (in this order):
---- gcc
:~$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 70
update-alternatives: using /usr/bin/gcc-11 to provide /usr/bin/gcc (gcc) in auto mode
:~$ sudo update-alternatives --verbose --install /usr/bin/gcc gcc /usr/bin/gcc-12 80
:~$ sudo update-alternatives --verbose --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
update-alternatives: using /usr/bin/gcc-9 to provide /usr/bin/gcc (gcc) in auto mode
:~$ update-alternatives --display gcc
gcc - auto mode
link best version is /usr/bin/gcc-9
link currently points to /usr/bin/gcc-9
link gcc is /usr/bin/gcc
/usr/bin/gcc-11 - priority 70
/usr/bin/gcc-12 - priority 80
/usr/bin/gcc-9 - priority 90
---- g++
:~$ sudo update-alternatives --verbose --install /usr/bin/g++ g++ /usr/bin/g++-11 70
update-alternatives: setting up automatic selection of g++
update-alternatives: using /usr/bin/g++-11 to provide /usr/bin/g++ (g++) in auto mode
:~$ sudo update-alternatives --verbose --install /usr/bin/g++ g++ /usr/bin/g++-12 80
update-alternatives: using /usr/bin/g++-12 to provide /usr/bin/g++ (g++) in auto mode
:~$ sudo update-alternatives --verbose --install /usr/bin/g++ g++ /usr/bin/g++-9 90
update-alternatives: using /usr/bin/g++-9 to provide /usr/bin/g++ (g++) in auto mode
:~$ update-alternatives --display g++
g++ - auto mode
link best version is /usr/bin/g++-9
link currently points to /usr/bin/g++-9
link g++ is /usr/bin/g++
/usr/bin/g++-11 - priority 70
/usr/bin/g++-12 - priority 80
/usr/bin/g++-9 - priority 90
---- cc
:~$ sudo update-alternatives --verbose --install /usr/bin/cc cc /usr/bin/gcc 30
update-alternatives: setting up automatic selection of cc
update-alternatives: using /usr/bin/gcc to provide /usr/bin/cc (cc) in auto mode
:~$ update-alternatives --verbose --display cc
cc - auto mode
link best version is /usr/bin/gcc
link currently points to /usr/bin/gcc
link cc is /usr/bin/cc
/usr/bin/gcc - priority 30
---- c++
:~$ sudo update-alternatives --verbose --install /usr/bin/c++ c++ /usr/bin/g++ 30
update-alternatives: setting up automatic selection of c++
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode
:~$ update-alternatives --verbose --display c++
c++ - auto mode
link best version is /usr/bin/g++
link currently points to /usr/bin/g++
link c++ is /usr/bin/c++
/usr/bin/g++ - priority 30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment