Skip to content

Instantly share code, notes, and snippets.

@yunqu
Last active April 5, 2024 01:27
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save yunqu/0cc6347905f73b7448898f50484e77b3 to your computer and use it in GitHub Desktop.
Save yunqu/0cc6347905f73b7448898f50484e77b3 to your computer and use it in GitHub Desktop.
Install a different version of GCC on Ubuntu

First, add the ubuntu-toolchain-r/test PPA to your system with:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test

Install the desired GCC and G++ versions by typing:

sudo apt install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9

The commands below will configure 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-9.

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
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7

Later if you want to change the default version use the update-alternatives command:

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   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. Enter the number of the version you want to be used as a default and press Enter.

The command will create symbolic links to the specific versions of GCC and G++.

@greg4735
Copy link

greg4735 commented Apr 6, 2023

Excellent. Very good information. Thank-you.

@tahargithub
Copy link

I am having Ubuntu 22.04 which comes with gcc-11.4 but I need gcc-7.3.0 for some applications.
I added the toolchain repository as suggested and updated the apt list but when I make sudo apt install gcc-11 g++-11
[sudo] password for Samir:
Sorry, try again.
[sudo] password for Samir:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
g++-11 is already the newest version (11.4.0-1ubuntu1~22.04).
as gcc-11 is already installed but when I make sudo apt install gcc-7 g++-7
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package g++-7 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source.
Many many thanks,
Samir

@HaskellZhangSong
Copy link

What if I want install gcc-9.5.0? This install gcc-9.3 by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment