Thanks to you all for your help. I ended up just creating aliases within ~/.bash_profile as follows:

alias gcc='gcc-4.8'
alias cc='gcc-4.8'
alias g++='g++-4.8'
alias c++='c++-4.8'

The answer from Lynken is very helpful, but I adapted it with aliases since that's easier for me to undo if necessary.

Specifically, if PATH is set such that /usr/local/bin (where brew puts the link to gcc 4.8) appears before appears /usr/bin (where gcc is linked by default), then creating links as Lyken suggested within /usr/local/bin should theoretically work for me. In practice, it doesn't for some reason -- failing with a linker error and aliases work around that error without me needing to solve that issue, too.

The other benefit of aliases is that I'm not having to link which I want homebrew to handle and not have to compete with that tool for which version of gcc is linked in /usr/local

Answer from quine on Stack Exchange
Top answer
1 of 10
411

First erase the current update-alternatives setup for gcc and g++:

sudo update-alternatives --remove-all gcc 
sudo update-alternatives --remove-all g++

Install Packages

It seems that both gcc-4.3 and gcc-4.4 are installed after install build-essential. However, we can explicitly install the following packages:

sudo apt-get install gcc-4.3 gcc-4.4 g++-4.3 g++-4.4

Install Alternatives

Symbolic links cc and c++ are installed by default. We will install symbol links for gcc and g++, then link cc and c++ to gcc and g++ respectively. (Note that the 10, 20 and 30 options are the priorities for each alternative, where a bigger number is a higher priority.)

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.3 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 20

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.3 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 20

sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc

sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++

Configure Alternatives

The last step is configuring the default commands for gcc, g++. It's easy to switch between 4.3 and 4.4 interactively:

sudo update-alternatives --config gcc
sudo update-alternatives --config g++

Or switch using script:

#!/bin/sh

if [ -z "$1" ]; then
    echo "usage: $0 version" 1>&2
    exit 1
fi

if [ ! -f "/usr/bin/gcc-1" ]; then
    echo "no such version gcc/g++ installed" 1>&2
    exit 1
fi

update-alternatives --set gcc "/usr/bin/gcc-$1"
update-alternatives --set g++ "/usr/bin/g++-$1"
2 of 10
62

execute in terminal :

gcc -v
g++ -v

Okay, so that part is fairly simple. The tricky part is that when you issue the command GCC it is actually a sybolic link to which ever version of GCC you are using. What this means is we can create a symbolic link from GCC to whichever version of GCC we want.

  • You can see the symbolic link :
ls -la /usr/bin | grep gcc-4.4
ls -la /usr/bin | grep g++-4.4
  • So what we need to do is remove the GCC symlink and the G++ symlink and then recreate them linked to GCC 4.3 and G++ 4.3:
rm /usr/bin/gcc
rm /usr/bin/g++

ln -s /usr/bin/gcc-4.3 /usr/bin/gcc
ln -s /usr/bin/g++-4.3 /usr/bin/g++
  • Now if we check the symbolic links again we will see GCC & G++ are now linked to GCC 4.3 and G++ 4.3:
ls -la /usr/bin/ | grep gcc
ls -la /usr/bin/ | grep g++
  • Finally we can check our GCC -v again and make sure we are using the correct version:
gcc -v
g++ -v
Discussions

In centos,how to switch to default gcc after switched to a higher version of gcc with devtoolset - Unix & Linux Stack Exchange
I want to install several gcc with different versions in centos. The default version of gcc in centos 6 is 4.9.3. So I use devtoolset install a higher version of gcc. Then I switch to the higher v... More on unix.stackexchange.com
🌐 unix.stackexchange.com
December 1, 2018
How to use a newer version of gcc on ubuntu? - Stack Overflow
I am trying to use the latest gcc-8 as show here. However when I run · sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8 ... How is it that I do not have alternative for gcc? Any suggestions on how I can resolve this error and configure gcc-8 to be the default ... More on stackoverflow.com
🌐 stackoverflow.com
arch linux - How to temporarily change default version of gcc in ArchLinux - Unix & Linux Stack Exchange
I have a hard time building some ... official run-file of cuda-8.0 from nvidia, or building projects which have used cuda library and its header files) all I want is to change default version of gcc on my environment (temporarily) and set it back later when i'm done, something ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
August 8, 2017
How to change the default GCC options by editing files? - Unix & Linux Stack Exchange
I want to change the default GCC options by editing files. I think it's impossible, but I want to double check. More on unix.stackexchange.com
🌐 unix.stackexchange.com
February 14, 2021
Top answer
1 of 9
147

As @Tommy suggested, you should use update-alternatives.
It assigns values to every software of a family, so that it defines the order in which the applications will be called.

It is used to maintain different versions of the same software on a system. In your case, you will be able to use several declinations of gcc, and one will be favoured.

To figure out the current priorities of gcc, type in the command pointed out by @tripleee's comment:

update-alternatives --query gcc

Now, note the priority attributed to gcc-4.4 because you'll need to give a higher one to gcc-3.3.
To set your alternatives, you should have something like this (assuming your gcc installation is located at /usr/bin/gcc-3.3, and gcc-4.4's priority is less than 50):

update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-3.3 50

--edit--

Finally, you can also use the interactive interface of update-alternatives to easily switch between versions. Type update-alternatives --config gcc to be asked to choose the gcc version you want to use among those installed.

--edit 2 --

Now, to fix the CXX environment variable systemwide, you need to put the line indicated by @DipSwitch's in your .bashrc file (this will apply the change only for your user, which is safer in my opinion):

echo 'export CXX=/usr/bin/gcc-3.3' >> ~/.bashrc
2 of 9
82

Here's a complete example of jHackTheRipper's answer for the TL;DR crowd. :-) In this case, I wanted to run g++-4.5 on an Ubuntu system that defaults to 4.6. As root:

apt-get install g++-4.5
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 100
update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.5 50
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 100
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.5 50
update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.6 100
update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-4.5 50
update-alternatives --set g++ /usr/bin/g++-4.5
update-alternatives --set gcc /usr/bin/gcc-4.5
update-alternatives --set cpp-bin /usr/bin/cpp-4.5

Here, 4.6 is still the default (aka "auto mode"), but I explicitly switch to 4.5 temporarily (manual mode). To go back to 4.6:

update-alternatives --auto g++
update-alternatives --auto gcc
update-alternatives --auto cpp-bin

(Note the use of cpp-bin instead of just cpp. Ubuntu already has a cpp alternative with a master link of /lib/cpp. Renaming that link would remove the /lib/cpp link, which could break scripts.)

🌐
Linux Mint Forums
forums.linuxmint.com › board index › main edition support › software & applications
How to specify gcc version to use in shell? - Linux Mint Forums
October 13, 2023 - This is what I appear to have at the moment: ls /usr/bin/gcc* /usr/bin/gcc /usr/bin/gcc-ar-7 /usr/bin/gcc-nm-9 /usr/bin/gcc-4.4 /usr/bin/gcc-ar-8 /usr/bin/gcc-ranlib /usr/bin/gcc-7 /usr/bin/gcc-ar-9 /usr/bin/gcc-ranlib-7 /usr/bin/gcc-8 /usr/bin/gcc-nm /usr/bin/gcc-ranlib-8 /usr/bin/gcc-9 /usr/bin/gcc-nm-7 /usr/bin/gcc-ranlib-9 /usr/bin/gcc-ar /usr/bin/gcc-nm-8 I want this to work just for the terminal in use not change the default gcc used by other things. Last edited by LockBot on Sat Apr 13, 2024 10:00 pm, edited 1 time in total. Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed. ... On your shell, for your own use, you can run alias gcc=gcc-4.4 so you can type gcc as shorthand for gcc-4.4.
Top answer
1 of 4
7

The version of gcc that's distributed with CentOS 6 is actually 4.4.7.

You can install as many versions of gcc either by installing devtoolset-# via yum or by compiling then from source.

The first way is the easiest. Make sure that you are installing the devtoolset packages via the scl repo. I figure that you already did as you have installed one already but in case you didn't:

yum install centos-release-scl

You can then use the below command to set the gcc version to whichever one you want. Using 5 for this example and assuming that your shell is bash:

scl enable devtoolset-5 bash

If you want to change to 6:

scl enable devtoolset-6 bash

If you want to change back to the default then any of the following will work assuming bash is your shell:

bash

source ~/.bash_profile

The first will start a new shell session and set any aliases/variables/commands in ~/.bashrc. The second will set it with the variables/commands in ~/.bash_profile. (Without the devtoolset enabled).

You can even put scl enable devtoolset-5 bash, for example, in ~/.bashrc or ~/.bash_profile so that it sets the gcc version to one of the devtoolset versions at login. To go back to the system default if you use this method, comment the line out in ~/.bashrc or ~/.bash_profile and then run bash or source ~/.bash_profile, respectively. That will start a new shell session with everything in one of those shell init files except the scl enable command that you commented out. The only downside is that any variables that you've set via the export command will no longer be there as the shell session will be new.

2 of 4
1

I'm no expert on scl but I do have years of linux experience.

When you do scl enable devtoolset-9 bash what is happening is that a new bash is started and a new environment is set up.

You can see the new bash process by:

  • first starting a new shell and checking your shell's pid via echo $$
  • second enabling the new devtoolset via scl enable devtoolset-9 bash
  • then check your pid again via echo $$
  • for bonus points you can do pstree -p to see that your new bash pid has a parent pid of your old bash process

So to finally answer your question: To return to the default g++ compiler all you need to do is exit your current bash process and then you should have the old g++ compiler.

Important note regarding your ~/.bashrc:

  • my solution won't work if you have somehow modified your ~/.bashrc
  • i.e. if you have something in there that always does the scl enable devtoolset-9
  • see the other solutions on this page because the other solutions talk more in-depth about your ~/.bashrc and how to modify or unmodify it
Top answer
1 of 3
98

gcc-7 and gcc-8 will happily co-live together.

I would suggest to let gcc-7 be installed, for satisfying build-essential and perhaps other dependent packages, and configure gcc-8 to be your default gcc installation.

Use update-alternatives for having gcc redirected automatically to gcc-8:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 700 --slave /usr/bin/g++ g++ /usr/bin/g++-7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8

This will give you the convenience of gcc being at the latest version, and still you will be able to invoke gcc-7 or gcc-8 directly.

If you'll wish to change the default gcc version later on, run sudo update-alternatives --config gcc. It will bring a prompt similar to this, which lets you pick the version to be used:

There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-8   800       auto mode
  1            /usr/bin/gcc-7   700       manual mode
  2            /usr/bin/gcc-8   800       manual mode

Press <enter> to keep the current choice[*], or type selection number: 

The higher priority is the one that is picked automatically by update-alternatives.

2 of 3
25

Master table of all GCC versions for each Ubuntu

At: How do I use the latest GCC on Ubuntu?

GCC 8 on Ubuntu 16.04

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-8 g++-8
gcc-8 --version

gives 8.1.0 as of 2018-11. See also:

GCC 9 on Ubuntu 19.04

sudo apt install gcc-9

https://packages.ubuntu.com/search?keywords=gcc-9

🌐
LinuxConfig
linuxconfig.org › home › how to switch between multiple gcc and g++ compiler versions on ubuntu 20.04 lts focal fossa
How to switch between multiple GCC and G++ compiler versions on Ubuntu 20.04 LTS Focal Fossa
September 21, 2025 - $ sudo update-alternatives --config gcc There are 3 choices for the alternative gcc (providing /usr/bin/gcc). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/gcc-9 9 auto mode 1 /usr/bin/gcc-7 7 manual mode * 2 /usr/bin/gcc-8 8 manual mode ...
🌐
Launchpad
launchpad.net › ~savoury1 › +archive › ubuntu › gcc-defaults-8
GCC 8 - defaults (Xenial & Bionic) : Rob Savoury - Launchpad
February 8, 2023 - sudo add-apt-repository ppa:savoury1/gcc-defaults-8 sudo apt update ... This PPA can be added to your system manually by copying the lines below and adding them to your system's software sources. deb https://ppa.launchpadcontent.net/savoury...
Find elsewhere
🌐
Ask Ubuntu
askubuntu.com › questions › 1191132 › configure-gcc-8-to-be-default-gcc-error-no-alternatives-for-gcc
18.04 - Configure `gcc-8` to be default gcc "error: no alternatives for gcc" - Ask Ubuntu
November 23, 2019 - (tensorflow_p36) ubuntu@user:~$ sudo apt install gcc-8 gcc-8 is already the newest version (8.3.0-6ubuntu1~18.04.1). Update. I'm trying the following as per the helpful suggestion below, but still to no avail.
🌐
Red Hat
access.redhat.com › discussions › 2710631
Red Hat Customer Portal - Access to 24x7 support and knowledge
March 28, 2022 - After installing RHEL 7.2 and Dev. Tool 4.1, the system gcc is 4.8.5 and the gcc used by eclipse is 5.3.1. Is it possible to change the default gcc version to the one used by the dev.
🌐
GitHub
gist.github.com › yunqu › 0cc6347905f73b7448898f50484e77b3
Install a different version of GCC on Ubuntu · GitHub
Selection Path Priority Status ... of all installed GCC versions on your Ubuntu system. Enter the number of the version you want to be used as a default and press Enter....
🌐
GitHub
gist.github.com › cobaohieu › ded429cb892b46ae9bfd9919a11e593a
update-alternatives for gcc on Ubuntu - Gist - GitHub
$ ls -larth `which g++`* lrwxrwxrwx 1 root root 23 Thg 1 4 2019 /usr/bin/g++-7 -> aarch64-linux-gnu-g++-7 lrwxrwxrwx 1 root root 23 Thg 3 10 2020 /usr/bin/g++-8 -> aarch64-linux-gnu-g++-8 lrwxrwxrwx 1 root root 23 Thg 4 23 2020 /usr/bin/g++-9 -> aarch64-linux-gnu-g++-9 lrwxrwxrwx 1 root root 24 Thg 4 15 19:21 /usr/bin/g++-10 -> aarch64-linux-gnu-g++-10 lrwxrwxrwx 1 root root 24 Thg 4 29 01:05 /usr/bin/g++-11 -> aarch64-linux-gnu-g++-11 lrwxrwxrwx 1 root root 21 Thg 5 27 09:22 /usr/bin/g++ -> /etc/alternatives/g++ ... $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 7 --slav
Top answer
1 of 2
2

It's possible you arrived because something blew up with nvidia. (I'm on cudatoolkit 11.4 and actually downgrading got me out of trouble - your mileage may vary.)

sudo apt install gcc-9 g++-9
sudo mkdir /usr/local/cuda/bin
sudo ln -s /usr/bin/gcc-9 /usr/local/cuda/bin/gcc

WARNING - I recommend using timeshift to make a backup of your working system. https://github.com/teejee2008/timeshift

This will get you the latest gcc 11

sudo add-apt-repository 'deb http://mirrors.kernel.org/ubuntu hirsute main universe'
sudo apt-get install g++-11
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 50
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 50
sudo update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-11 100
sudo update-alternatives --install /usr/bin/cpp cpp-bin /usr/bin/cpp-11 50
sudo update-alternatives --set g++ /usr/bin/g++-11
sudo update-alternatives --set gcc /usr/bin/gcc-11
sudo update-alternatives --set cpp-bin /usr/bin/cpp-11
gcc --version
gcc (Ubuntu 11-20210417-1ubuntu1) 11.0.1 20210417 

Copyright (C) 2021 Free Software Foundation, Inc.

2 of 2
0

From the looks of it, inside your conda environment, the gcc command has been linked to /home/ubuntu/anaconda3/envs/tensorflow_p36/bin/gcc, and the version is 4. Whereas outside the environment, the only gcc on the path is gcc-8, which corresponds to version 8.

Also, as you've observed, you are unable to create an alternative for gcc because it is linked to g++ alternative.

I also prefer to have gcc be the main alternative, and all other tools (including g++) follow suit. In this case, I will start by deleting the g++ alternative:

Copysudo update-alternatives --remove-all g++

Now that we've got that out of the way, we can create one for gcc which links to gcc-8

Copysudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 100 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-8 --slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-8 --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-8

References

  1. https://askubuntu.com/a/26518/145907
  2. https://askubuntu.com/a/1206264/145907
🌐
GNU
gcc.gnu.org › gcc-8 › changes.html
GCC 8 Release Series — Changes, New Features, and Fixes - GNU Project
January 31, 2025 - The default mode has been changed to -std=gnu17. GCC 8 (-fabi-version=12) has a couple of corrections to the calling convention, which changes the ABI for some uncommon code: Passing an empty class as an argument now takes up no space on x86_64, as required by the psABI.
🌐
FOSS Linux
fosslinux.com › home › programming › how to install and switch gcc versions on ubuntu: the 2026 master guide
How to Install and Switch GCC Versions on Ubuntu: The 2026 Master Guide
April 21, 2026 - # Verifying the active toolchain ... want the system to always default to the highest version you have installed, leave the status as auto mode....
🌐
Mengyang's blog
archerfmy.github.io › 2017 › 04 › 12 › How-to-switch-your-gcc-g-version-in-ubuntu
How to switch your gcc/g++ version in ubuntu | Mengyang's blog
May 22, 2017 - Generally, we need to keep multiple versions of gcc/g++ to fit our compiling environment. This blog can teach you how to switch your system gcc/g++ version in ubuntu system.
🌐
Ask Ubuntu
askubuntu.com › questions › 448629 › how-do-i-set-gcc-4-8-to-be-the-default-gcc
12.04 - How do I set gcc 4.8 to be the default gcc? - Ask Ubuntu
April 16, 2014 - sudo add-apt-repository ppa:ubuntu-toolchain-r/test sudo apt-get update sudo apt-get install gcc-4.8