Skip to content

Install several GCC versions on Ubuntu

Launch the following script to have several versions of GCC on Ubuntu alongside the default one: install some GCC versions using the Ubuntu Toolchain Uploads repository and manage the different GCC versions using update-alternatives. This works also for Windows Subsystem for Linux.

Click to view
bash
#!/bin/bash
# add gcc 10 and gcc 11 to Ubuntu 20.04
# launch this script using "sudo"

# install the default building tools
apt update
apt install -y build-essential software-properties-common

# add the repository "ubuntu-toolchain-r/test"
apt update
add-apt-repository -y ppa:ubuntu-toolchain-r/test
apt update

# install other GCC versions
apt install -y gcc-10 g++-10 gcc-11 g++-11

# configure the alternatives for gcc and g++, setting a higher priority to a newer version (ymmv)
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 50 --slave /usr/bin/g++ g++ /usr/bin/g++-9
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60 --slave /usr/bin/g++ g++ /usr/bin/g++-10
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 70 --slave /usr/bin/g++ g++ /usr/bin/g++-11

# check or change the alternatives configuration
update-alternatives --config gcc

Install several Clang versions on Ubuntu

Launch the following script to have several versions of Clang on Ubuntu alongside the default one: install some Clang versions using the LLVM Ubuntu/Debian packages and manage the different Clang versions using update-alternatives. This works also for Windows Subsystem for Linux.

Click to view
bash
#!/bin/bash
# add Clang 14 and Clang 16 to a clean Ubuntu
# Ubuntu 18.04 (used by Colab) needs "sudo add-apt-repository ppa:ubuntu-toolchain-r/test"
wget https://apt.llvm.org/llvm.sh
sudo apt update
sudo bash llvm.sh 14
sudo bash llvm.sh 16

# configure the alternatives for clang and llvm-profdata, setting a higher priority to a newer version (ymmv)
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-16 60 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-16 --slave /usr/bin/llvm-profdata llvm-profdata /usr/bin/llvm-profdata-16
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-14 40 --slave /usr/bin/clang++ clang++ /usr/bin/clang++-14 --slave /usr/bin/llvm-profdata llvm-profdata /usr/bin/llvm-profdata-14

# check or change the alternatives configuration
update-alternatives --config clang

# to uninstall a Clang version
# LLVM_VERSION=14
# sudo apt purge -y clang-$LLVM_VERSION lldb-$LLVM_VERSION lld-$LLVM_VERSION clangd-$LLVM_VERSION && sudo apt autoremove -y