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 gc
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
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 gcc installation would be appreciated. Running which gcc from outside the conda environment does not return anything: ... Update. I'm trying the following as per the helpful suggestion below, but still to no avail. (tensorflow_p36) ubuntu... 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
linux - How to change the default GCC compiler in Ubuntu? - Stack Overflow
And to go back to auto mode is just update-alternatives --auto labelForTheGroupOfAlternatives. Why use ln manually? 2024-06-01T05:38:37.933Z+00:00 ... Save this answer. ... Show activity on this post. I used just the lines below and it worked. I just wanted to compile VirtualBox and VMWare WorkStation using kernel 4.8.10 on Ubuntu 14.04. Initially, most things were not working for example graphics and networking. I was lucky that VMWare workstation requested for gcc ... More on stackoverflow.com
🌐 stackoverflow.com
Fedora alternative gcc version
A toolbox with that version, or an Ubuntu 20.04 container since that's what your professor is using. More on reddit.com
🌐 r/Fedora
23
15
January 17, 2022
🌐
GitHub
gist.github.com › yunqu › 0cc6347905f73b7448898f50484e77b3
Install a different version of GCC on Ubuntu · GitHub
There are 3 choices for the alternative gcc (providing /usr/bin/gcc). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/bin/gcc-9 90 auto mode 1 /usr/bin/gcc-7 70 manual mode 2 /usr/bin/gcc-8 80 manual mode 3 /usr/bin/gcc-9 90 manual mode · Press to keep the current choice[*], or type selection number: You will be presented with a list of all installed GCC versions on your Ubuntu system.
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
🌐
LinuxConfig
linuxconfig.org › home › how to switch between multiple gcc and g++ compiler versions on ubuntu 22.04 lts jammy jellyfish
How to switch between multiple GCC and G++ compiler versions on Ubuntu 22.04 LTS Jammy Jellyfish
February 28, 2022 - $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8 $ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 8 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9 $ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10 $ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 10 · Check the available C and C++ compilers list on your Ubuntu 22.04 system and select desired version by entering relevant selection number:
🌐
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 - After that, the gcc and g++ version will be switched to the version you want. The alternatives work like this. /usr/bin/gcc will point to /etc/alternatives/gcc, which in turn points to the real command /usr/bin/gcc-7 or /usr/bin/gcc-4.8 depending on which alternative you choose.
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.)

Find elsewhere
🌐
GitHub
gist.github.com › SunnyRaj › 788ca494db609a404c9dc4c88d47e775
Configure multiple GCC versions on ubuntu · GitHub
Indeed, can't get gcc-5 nor g++-5 on Ubuntu 19.10 Trying to build ROS2 and from what I've seen it needs gcc/g++ 5
🌐
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 gc
🌐
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 - The Architect’s Edge: If update-alternatives feels too heavy for a single project, consider using CC and CXX environment variables. I use these to target specific compilers for a single terminal session without changing the system-wide default. Q: Does switching GCC versions affect my installed software?
🌐
AlternativeTo
alternativeto.net › software › gnu-compiler-collection
GNU Compiler Collection Alternatives: Top 10 Compilers & Similar Apps | AlternativeTo
The best GNU Compiler Collection alternatives are MinGW, LLVM and clang. Our crowd-sourced lists contains more than 10 apps similar to GNU Compiler Collection for Linux, Windows, Mac, BSD and more.
🌐
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
🌐
Reddit
reddit.com › r/fedora › fedora alternative gcc version
r/Fedora on Reddit: Fedora alternative gcc version
January 17, 2022 -

I am required to use a certain gcc version (9.3). Currently fedora ships with gcc v11.

I am wondering if there is a way to use this gcc version on my current fedora machine. Is it possible to use toolboxes for this purpose? Or is there some other solution besides using an distro with an older gcc version?

🌐
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 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
🌐
GitHub
gist.github.com › bhaskarvk › 78a80d9b5d308c84ba43b4a4e599a439
Proper GCC (vers. 5/6/7) & LLVM/CLANG (vers. 4/5) Toolchain Setup on Ubuntu/Linux Mint · GitHub
To change the default compiler/linker combo used by the system do sudo update-alternatives --config cc. NOTE: Both the gcc and the clang compilers use the libstdc++, the C++ standards library which is the default on Linux.
🌐
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 - There are 2 choices for the alternative gcc (providing /usr/bin/gcc). Selection Path Priority Status ------------------------------------------------------------ 0 /usr/bin/gcc-8 80 auto mode 1 /usr/bin/gcc-4.8 60 manual mode * 2 /usr/bin/gcc-8 80 manual mode Press <enter> to keep the current choice[*], or type selection number: 0 (tensorflow_p36) ubuntu@user:~$ gcc --version gcc (GCC) 4.8.5
🌐
thelinuxvault
thelinuxvault.net › blog › how-to-switch-between-multiple-gcc-and-g-compiler-versions-on-ubuntu-22-04-lts-jammy-jellyfish
How to Switch Between Multiple GCC and G++ Compiler Versions on Ubuntu 22.04 LTS Jammy Jellyfish
sudo update-alternatives --remove gcc /usr/bin/gcc-10 sudo update-alternatives --remove g++ /usr/bin/g++-10 · You now know how to install, manage, and switch between multiple GCC/G++ versions on Ubuntu 22.04. This flexibility is critical for developers working across projects with varying compiler requirements.
🌐
Linux Questions
linuxquestions.org › questions › linux-software-2 › best-alternative-to-the-famous-gcc-4175605734
Best alternative to the famous GCC ?
May 11, 2017 - Hello, I am looking for a possible more simple alternative to GCC ? Actually, I would be happy actually to have a small minimal source code of a C