Multiple GNU Compilers
Table of Contents
1. Install multiple versions of GNU compilers
First, we need to add repository:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
Then, we can install multiple versions of GNU compilers
sudo apt install gcc-11 gcc-12 gcc-13 gcc-14
sudo apt install g++-11 g++-12 g++-13 g++-14
sudo apt install gfortran-11 gfortran-12 gfortran-13 gfortran-14
[Optional] Install specific GNU compilers
sudo apt list gcc-11
sudo apt install aptitude
sudo aptitude install gcc-11=11.2.0-19ubuntu1
2. Config with update-alternatives
Using update-alternatives, we can switch between different versions easily:
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
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 14
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 11
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 12
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 13
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-14 14
sudo update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-11 11
sudo update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-12 12
sudo update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-13 13
sudo update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-14 14
4. Check GNU versions
This is a convenient way to set and check GNU compilers:
check-gnu-compilers-version(){
echo "-----------------------"
echo "You are using these compilers"
echo "-----------------------"
gcc --version | grep "gcc"
gfortran --version | grep "GNU Fortran"
g++ --version | grep "g++"
}
set-gnu-compilers-version(){
if [ -z "$1" ]; then
echo "Usage: set-gnu-compilers-version <version>"
return 1
fi
VERSION=$1
# Set gcc version
sudo update-alternatives --set gcc /usr/bin/gcc-$VERSION
if [ $? -ne 0 ]; then
echo "Failed to set gcc to version $VERSION"
return 1
fi
# Set g++ version
sudo update-alternatives --set g++ /usr/bin/g++-$VERSION
if [ $? -ne 0 ]; then
echo "Failed to set g++ to version $VERSION"
return 1
fi
# Set gfortran version
sudo update-alternatives --set gfortran /usr/bin/gfortran-$VERSION
if [ $? -ne 0 ]; then
echo "Failed to set gfortran to version $VERSION"
return 1
fi
echo "Successfully set gcc, g++, and gfortran to version $VERSION"
check-gnu-compilers-version
}