The best way to correctly install gcc-4.9 and set it as your default gcc version use:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9 g++-4.9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9

The --slave, with g++, will cause g++ to be switched along with gcc, to the same version. But, at this point gcc-4.9 will be your only version configured in update-alternatives, so add 4.8 to update-alternatives, so there actually is an alternative, by using:

sudo apt-get install gcc-4.8 g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8

Then you can check which one that is set, and change back and forth using:

sudo update-alternatives --config gcc

If you have an issue with update-alternatives gcc priority 60 not being higher than previous versions installed you can use the previous update-alternatives --config gcc command to check installed versions and use:

sudo update-alternatives --remove gcc

Or:

sudo update-alternatives --remove-all gcc

NOTE: You could skip installing the PPA Repository and just use /usr/bin/gcc-4.9-base but I prefer using the fresh updated toolchains.


For GCC 5.X or 6, the packages (and correspondingly, the commands) are just called gcc-5, gcc-6, etc. This is due to the change in GCC's version scheme, where 5.1 is the first GCC 5 release, and future 5.X releases are for bug fixes.

Answer from SudoSURoot on askubuntu.com
Top answer
1 of 5
240

The best way to correctly install gcc-4.9 and set it as your default gcc version use:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9 g++-4.9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9

The --slave, with g++, will cause g++ to be switched along with gcc, to the same version. But, at this point gcc-4.9 will be your only version configured in update-alternatives, so add 4.8 to update-alternatives, so there actually is an alternative, by using:

sudo apt-get install gcc-4.8 g++-4.8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8

Then you can check which one that is set, and change back and forth using:

sudo update-alternatives --config gcc

If you have an issue with update-alternatives gcc priority 60 not being higher than previous versions installed you can use the previous update-alternatives --config gcc command to check installed versions and use:

sudo update-alternatives --remove gcc

Or:

sudo update-alternatives --remove-all gcc

NOTE: You could skip installing the PPA Repository and just use /usr/bin/gcc-4.9-base but I prefer using the fresh updated toolchains.


For GCC 5.X or 6, the packages (and correspondingly, the commands) are just called gcc-5, gcc-6, etc. This is due to the change in GCC's version scheme, where 5.1 is the first GCC 5 release, and future 5.X releases are for bug fixes.

2 of 5
63

Ultimate mega master compatibility table

OK let's do this:

                  GCC                              clang
        +-------------------------------------+-------------------------------------------------+
        | 15 14 13 12 11 10  9  8  7  6  5  4 | 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5 |
+-------+-------------------------------------+-------------------------------------------------+
| 25.10 |  D  M  M  M  M                      |  D  M  M  M                                     |
| 25.04 |     D  M  M  M                      |  D  M  M  M                                     |
| 24.10 |     D  M  M  M                      |     D  M  M  M  M  M                            |
| 24.04 |     M  D  M  M  M  M                |        D  M  M  M  M                            |
| 23.10 |        D  M  M  M  M                |           M  D  M  M  M                         |
| 23.10 |        D  M  M  M  M                |              D  M  M  M                         |
| 23.04 |        M  D  M  M  M                |              M  D  M  M                         |
| 22.10 |           D  M  M  M                |                 D  M  M                         |
| 22.04 |        P  M  D  M  M                |                    D  M  M  M                   |
| 21.10 |              D  M  M  M             |                       D  M  M  M  M             |
| 21.04 |              M  D  M  M  M          |                          D  M  M  M             |
| 20.10 |                 D  M  M  M          |                             D  M  M  M  M  M    |
| 20.04 |                 P  D  M  M          |                                D  M  M  M  M    |
| 19.10 |                    D  M  M          |                                                 |
| 19.04 |                    M  D  M  M       |                                                 |
| 18.10 |                       D  M  M  M    |                                                 |
| 18.04 |                 P  P  M  D  M  M    |                                   M  M  M  D  M |
| 16.04 |                    P  P  P  P  D  M |                                                 |
+-------+-------------------------------------+-------------------------------------------------+
  • D: Default GCC

    sudo apt-get update
    sudo apt-get install gcc g++
    gcc --version
    

    Whatever the gcc package aliases to: https://packages.ubuntu.com/search?keywords=gcc and also present in manifests: How do I list the default installed packages?

  • M: Present in Main repo

    sudo apt-get update
    sudo apt-get install gcc-X g++-X
    gcc-X --version
    

    All Ubuntu versions that have a hit for a given GCC version, e.g. for GCC 7: https://packages.ubuntu.com/search?keywords=gcc-7 or clang 7 https://packages.ubuntu.com/search?keywords=clang-7

    The minor versions of these packages can get updated from time to time within a single Ubuntu version (on the . revisions?), e.g. 8.3.0 to 8.4.0 so we are not keeping track of that.

  • P: ppa:ubuntu-toolchain-r/test, which is owned by Ubuntu people and therefore can be trusted to not be a virus, although it is possibly unstable:

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

    Full list: https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test

    The minor versions of these packages can get updated from time to time, e.g. 8.3.0 to 8.4.0.

Blank spaces on the table mean either "no package available" or "I didn't bother to check". Notably I've not been looking into PPA packages too thoroughly. Edits accepted.

All the questions:

  • install gcc-9 on Ubuntu 18.04?
  • How to install gcc-7 or clang 4.0?
  • install gcc-9 on Ubuntu 18.04?
  • What are the GCC and clang versions available in Ubuntu 18.04?

How to set a non-default GCC as the default?

E.g., you installed /usr/bin/gcc-7 but you want to use that instead of /usr/bin/gcc when you run gcc main.c.

Use sudo update-alternatives as mentioned in other answers: https://askubuntu.com/a/581497/52975 It creates the required symlinks for you.

See also: What exactly does `update-alternatives` do?

How to build your own toolchain from source

If even the PPA is not old/new enough for you, see this:

  • https://stackoverflow.com/questions/847179/multiple-glibc-libraries-on-a-single-host/52454603#52454603
  • https://stackoverflow.com/questions/26305738/can-i-build-gcc-for-arm-with-an-x64-one/26306591#26306591

Older GCC version questions

  • Ubuntu 20.04 - gcc version lower than gcc-7
  • How to use an older version of GCC
Top answer
1 of 1
18

This is a common pattern in linux. When there are multiple versions of the same program installed, though the executables are all present in the /usr/bin/ directory, only one of them is "visible" as that program. For example, if you install gcc-9 and gcc-10, both executables are present as /usr/bin/gcc-9 and /usr/bin/gcc-10 but only one of them is visible as gcc. This happens by symlinking a preferred version to the same directory as /usr/bin/gcc. In ubuntu 20.04, the preferred version is gcc-9 and so, gcc-9 is symlinked as gcc.

You can check this by running the following command.

$ which gcc | xargs file

The output will be

/usr/bin/gcc: symbolic link to gcc-9

There are a few things you can do to use gcc-10 as your c compiler.

  1. Directly call the gcc-10 executable. Instead of using gcc <code.c>, call gcc-10 <code.c>.
  2. You can manually symlink gcc-10 as the preferred gcc. Assuming you did not modify the system paths, the following command can be used.
# ln -s /usr/bin/gcc-10 /usr/local/bin/gcc

This works because, by default, the executables in /usr/local/bin/ take precedence over /usr/bin/.

  1. If you are using bash, you can create an alias for gcc as gcc-10. Add the following line to your .bashrc.
alias gcc="gcc-10"

Remember to relaunch bash or source ~/.bashrc.

  1. Using update-alternatives (Thanks to @ted-lyngmo for pointing it out). Debian based distributions supply a separate program, that can make symlinking easier / more functional. Read more using man update-alternatives. To use gcc-10 as the preferred gcc, use the following command.
# update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 60

The above command says, /usr/bin/gcc is the link needed and the name is gcc, the target executable is /usr/bin/gcc-10 and it has a priority of 60.

This links gcc to /etc/alternatives/gcc, which itself is a symlink to /usr/bin/gcc-10. If a higher priority program is added to update-alternatives, /etc/alternatives/gcc points to the higher priority program.

If you don't have any specific reason, I would also recommend to upgrade to a newer ubuntu version, so that the default gcc is a newer one.

I read on the gcc website that version 9.4 is more up-to-date than some 10.x versions.

With newer gcc versions, new features are added. Support for newer c/c++ standards are also added. Eg. You can read the changes for gcc-10 here. But people still need gcc-9 because some programs only build with gcc-9. So, GNU maintains gcc-9 (and much older versions) for a long time. Bugs are fixed, and newer releases are made. This can happen after the release of a newer gcc version. So, it is very much possible that a version of gcc-9 is newer than a version of gcc-10.

🌐
Medium
medium.com › @xersendo › moving-to-c-26-how-to-build-and-set-up-gcc-15-1-on-ubuntu-f52cc9173fa0
Moving to C++26: How to Build and Set Up GCC 15.1 on Ubuntu
May 3, 2025 - That’s because we still need to tell the system to use our shiny new GCC by default. sudo update-alternatives --install /usr/bin/gcc gcc /opt/gcc-15/bin/gcc 100 sudo update-alternatives --install /usr/bin/g++ g++ /opt/gcc-15/bin/g++ 100
🌐
GitHub
gist.github.com › cobaohieu › ded429cb892b46ae9bfd9919a11e593a
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).
🌐
DEV Community
dev.to › nullity › zai-ubuntu-2004-an-zhuang-gcc-13-1kd1
Install or uninstall gcc 13 on Ubuntu 20.04 - DEV Community
March 22, 2024 - Testing date: Mar 14, 2024 Install gcc-13, g++-13 sudo apt update sudo apt install... Tagged with ubuntu, gcc.
🌐
Baeldung
baeldung.com › home › installation › how to install the latest version of gcc on ubuntu
How to Install the Latest Version of GCC on Ubuntu | Baeldung on Linux
November 17, 2024 - There are lots of GCC PPAs available on the Web. In this case, we use the ubuntu-toolchain-r/test PPA. So, let’s leverage the add-apt-repository command to add this PPA to the system: $ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
🌐
Linux Mint Forums
forums.linuxmint.com › board index › interests › programming & development
How to Update GCC Latest Version? - Linux Mint Forums
gcc-11 --version gcc-11 (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions.
🌐
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 - To install the GCC compiler from Ubuntu repositories: ... The build-essential package includes the GCC compiler and other utilities required for building software. 3. Use the following command to check the GCC version: ... The GCC version in this example is 13.3.0. Use the GCC PPAs (Personal Package Archive) to streamline the installation process and install a specific GCC version (or multiple GCC versions): ... Press Enter to confirm. 4. Update ...
Find elsewhere
🌐
GitHub
gist.github.com › application2000 › 73fd6f4bf1be6600a2cf9f56315a2d91
How to install latest gcc on Ubuntu LTS (12.04, 14.04, 16.04) · GitHub
sudo apt-get update -y && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get install build-essential software-properties-common -y && sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y && sudo apt-get update -y && sudo ...
🌐
Ubuntu
documentation.ubuntu.com › ubuntu-for-developers › reference › availability › gcc
Available GCC versions - Ubuntu for Developers
5 days ago - Ubuntu GCC (deb) packages:,,, Ubuntu version, available GCC versions, gcc-defaults version,,, 25.10 (Questing Quokka), 11, 12, 13, 14, 15, 15,, 25.04 (Plucky Puffin), 11, 12, 13, 14, 15, 14,, 24.10...
🌐
DedicatedCore
dedicatedcore.com › home › how to install gcc compiler on ubuntu 22.04
How to Install GCC Compiler on Ubuntu 22.04 - DedicatedCore Blog
January 24, 2025 - This guide will show you how to install GCC on a Ubuntu. A step-by-step so that you have all the tools you need to start writing and creating software immediately. For a seamless installation process of the powerful programming language Go (Golang) check.
🌐
Tuxamito
tuxamito.com › wiki › index.php › Installing_newer_GCC_versions_in_Ubuntu
Installing newer GCC versions in Ubuntu - Tuxamito
$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test $ sudo apt-get update · Then install the desired GCC and G++ versions. At the moment of writing this page the latest available version is 7.2.0 (that correspond to the packages gcc-7 g++-7).
🌐
GitHub
gist.github.com › jlblancoc › 99521194aba975286c80f93e47966dc5
Installing gcc-7 & g++-7 in Ubuntu 16.04LTS Xenial · GitHub
FROM ubuntu:16.04 ... # Install gcc 7 RUN apt update -qq \ && apt install -yq software-properties-common \ && add-apt-repository -y ppa:ubuntu-toolchain-r/test \ && apt update -qq \ && apt install -yq g++-7 \ && apt clean \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* # Configure alias RUN update-alternatives \ --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 \ --slave /usr/bin/g++ g++ /usr/bin/g++-7 \ --slave /usr/bin/gcov gcov /usr/bin/gcov-7 \ --slave /usr/bin/gcov-tool gcov-tool /usr/bin/gcov-tool-7 \ --slave /usr/bin/gcc-ar gcc-ar /usr/bin/gcc-ar-7 \ --slave /usr/bin/gcc-nm gcc-nm /usr/bin/gcc-nm-7 \ --slave /usr/bin/gcc-ranlib gcc-ranlib /usr/bin/gcc-ranlib-7
🌐
CyberITHub
cyberithub.com › how-to-update-or-upgrade-gcc-to-latest-version-on-ubuntu-debian
How to Update or Upgrade GCC to Latest Version on Ubuntu/Debian | CyberITHub
August 13, 2023 - After successfully installing the package, you need to link the compiler to detect this latest version by using sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 20 command.
🌐
Web Hosting Geeks
webhostinggeeks.com › home › ubuntu › how to upgrade gcc on ubuntu
How to Upgrade GCC on Ubuntu | Linux Tutorials for Beginners
October 23, 2023 - sudo add-apt-repository ppa:ubuntu-toolchain-r/test · Now, install the GCC version you want. For instance, to install GCC 10: ... sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100 --slave /usr/bin/g++ g++ /usr/bin/g++-10
🌐
Nilo Menezes
blog.nilo.pro.br › en › posts › 2024-04-18-compilando-o-gcc-13-2-0-no-ubuntu-22-04-lts
Compiling the GCC 13.2.0 on Ubuntu 22.04 LTS :: Nilo Menezes
sudo apt install build-essential sudo apt install libmpfr-dev libgmp3-dev libmpc-dev -y wget http://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz tar -xf gcc-13.2.0.tar.gz cd gcc-13.2.0 ./configure -v --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --prefix=/usr/local/gcc-13.2.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib --program-suffix=-13.2.0 make -j3 sudo make install /usr/local/gcc-13.2.0/bin/gcc-13.2.0 --version