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"
Answer from hhlp on askubuntu.com
🌐
GitHub
gist.github.com › cobaohieu › ded429cb892b46ae9bfd9919a11e593a
update-alternatives for gcc on Ubuntu - Gist - GitHub
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 7 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11 $ sudo update-alternatives --config gcc There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
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

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 More on stackoverflow.com
🌐 stackoverflow.com
linux - How to change the default GCC compiler in Ubuntu? - Stack Overflow
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... More on stackoverflow.com
🌐 stackoverflow.com
How to switch from gcc9 to gcc10 - g++9 to g++10
I can't switch from gcc9 to gcc10 / g++9 to g++10 I installed all needed gcc10/g++10 packages. When I run this commands: sudo update-alternatives --config gcc sudo update-alternatives --config g++ error: update-alternatives: error: no alternatives for gcc update-alternatives: error: no alternatives ... More on ubuntu-mate.community
🌐 ubuntu-mate.community
1
0
May 13, 2021
Creating update-alternatives to switch between multiple installed gcc - Forums Feedback - openSUSE Forums
We can install multiple versions ... the correct gcc? I’d even submitted a feature request to the bugzilla asking for someone to create an update-alternatives for gcc. This is an extremely acute problem in Tumbleweed because although bleeding edge compilers are installed by ... More on forums.opensuse.org
🌐 forums.opensuse.org
0
October 2, 2016
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:

sudo 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

sudo 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
🌐
GitHub
gist.github.com › yunqu › 0cc6347905f73b7448898f50484e77b3
Install a different version of GCC on Ubuntu · GitHub
The default version is the one with the highest priority, in our case that is gcc-9. sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 ...
🌐
Google Groups
groups.google.com › g › scintilla-interest › c › WtgbMh1D4DU
Changing default compiler on OpenSuSE
June 30, 2024 - By default OpenSuSE appears to install gcc-7 and cpp-7. After you install version 11 of both via the package manager you still have a bit of work to do. roland@localhost:~> su - Password: localhost:~ # update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 50 update-alternatives: using /usr/bin/gcc-11 to provide /usr/bin/gcc (gcc) in auto mode localhost:~ # update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 20 localhost:~ # update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-11 50 update-alternatives: using /usr/bin/cpp-11 to provide /usr/bin/cpp (cpp) in auto mode localhost:~ # update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-7 20 localhost:~ # localhost:~ # update-alternatives --config gcc There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
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.)

🌐
Medium
xavier-jouvenot.medium.com › how-to-deal-with-several-gcc-version-on-your-machine-d251044aa9e8
How to deal with several gcc version on your machine | by Xavier Jouvenot | Medium
December 4, 2020 - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10.1 1 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9.3 2sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10.1 1 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9.3 2
Find elsewhere
🌐
The Fedora Project
fedoraproject.org › wiki › Changes › Use-Update-Alternatives-For-usr-bin-cc
Changes/Use-Update-Alternatives-For-usr-bin-cc - Fedora Project Wiki
February 11, 2021 - Modify the gcc package so that the /usr/bin/cc and /usr/bin/c++ symlinks are managed by update-alternatives.
🌐
jdhao's digital space
jdhao.github.io › 2020 › 07 › 02 › ubuntu_update_alternatives
Switch Command with update-alternatives on Ubuntu · jdhao's digital space
October 28, 2021 - If we want to switch back to GCC 7.4, we then need to remove the old symlinks and create new ones, which is cumbersome. update-alternatives provides what is called master and slave alternatives, where if you change the master symlink, the slave symlinks will also get changed automatically.
🌐
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 - Q: How do I remove a version from the alternatives list? A: Use the --remove flag. For example: sudo update-alternatives --remove gcc /usr/bin/gcc-11.
🌐
Digipen
azrael.digipen.edu › ~mmead › www › mg › update-compilers › index.html
Mead's Guide to Upgrading GCC and Clang on Linux
November 16, 2021 - You now know how to add (or install) alternatives. If you had another version, say version 6, you would do the same thing for that, changing gcc-5 to gcc-6 in the above commands. Now, I could do the same thing with g++, as well. But, I don't really want to use two different versions of gcc and g++ at the same time (e.g. gcc-4.8 and g++-5). I want them to be synchronized so that if I change gcc, then g++ will also change. This is the command to do that: sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 60 --slave /usr/bin/g++ g++ /usr/bin/g++-5 This ties gcc-4.8 with g++-4.8 and gcc-5 with g++-5 so when we change gcc, then g++ will also change.
🌐
Logikal Blog
logikalsolutions.com › home › changing default compiler on opensuse
Changing Default Compiler on OpenSuSE - Logikal Solutions
June 30, 2024 - update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 50 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 20 update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-11 50 update-alternatives --install /usr/bin/cpp cpp /usr/bin/cpp-7 20 update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 50 update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 20 update-alternatives --config gcc update-alternatives --config cpp update-alternatives --config g++
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › how to install gcc compiler on ubuntu
How to Install GCC Compiler on Ubuntu {3 Simple Methods}
February 20, 2025 - sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 14 --slave /usr/bin/g++ g++ /usr/bin/g++-14
🌐
openSUSE
en.opensuse.org › User:Tsu2 › gcc_update-alternatives
User:Tsu2/gcc update-alternatives - openSUSE Wiki
October 2, 2016 - If you want the additional gcc options, go ahead and create more based on the following two commands which can be run individually or placed in a script. Note that whatever you configure first will be the default(although you can change later if you make this minor mistake) update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 50 update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 20
🌐
openSUSE Forums
forums.opensuse.org › english › forums feedback
Creating update-alternatives to switch between multiple installed gcc - Forums Feedback - openSUSE Forums
October 2, 2016 - We can install multiple versions ... the correct gcc? I’d even submitted a feature request to the bugzilla asking for someone to create an update-alternatives for gcc. This is an extremely acute problem in Tumbleweed because although bleeding edge compilers are installed by ...
🌐
GitHub
gist.github.com › Aleksoid1978 › ef09c680b82d2032fd78c7c51d3b36e9
update-alternatives for gcc on Ubuntu · GitHub
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 7 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11 $ sudo update-alternatives --config gcc There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
🌐
Linuxize
linuxize.com › home › gcc › how to install gcc (build-essential) on ubuntu 20.04
How to Install GCC (build-essential) on Ubuntu 20.04 | Linuxize
June 4, 2020 - The commands below configures alternative for each version and associate a priority with it. The default version is the one with the highest priority, in our case that is gcc-10. ... sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8
🌐
RobotFlow
forum.robotflow.ai › t › switch-between-gcc-g-versions › 51
Switch between GCC/G++ versions - RobotFlow
July 14, 2022 - Install Different versions (in ... g++-10 Create linkage between different versions to system version sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8 sudo update-alternatives --install /usr/bin/g++ g++ ...