|home| |posts| |projects| |cv| |bookmarks| |github|

Switch Between Gcc Versions on Ubuntu

Setup alternatives(the gcc versions you want):

update-alternatives \
    --install /usr/bin/gcc gcc /usr/bin/gcc-9 10 \
    --slave /usr/bin/g++ g++ /usr/bin/g++-9 \
    --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-9 \
    --slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-9 \
    --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-9

update-alternatives \
    --install /usr/bin/gcc gcc /usr/bin/gcc-11 20 \
    --slave /usr/bin/g++ g++ /usr/bin/g++-11 \
    --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-11 \
    --slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-11 \
    --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-11

--slave is needed to cascade the update for all tools in the gcc toolchain.

Since gcc-11 is set with higer priority(20 > 10), this will be the default version.

To switch between versions, run:

update-alternatives --config gcc