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
People also ask

Does GCC come pre-installed on Ubuntu?
No. Ubuntu desktop and server images do not include GCC by default. You need to install the build-essential package, which provides GCC, G++, make, and essential development headers.
๐ŸŒ
linuxcapable.com
linuxcapable.com โ€บ home โ€บ ubuntu โ€บ how to install gcc on ubuntu (26.04, 24.04, 22.04)
How to Install GCC on Ubuntu (26.04, 24.04, 22.04) - LinuxCapable
How do I fix "gcc: command not found" on Ubuntu?
Ubuntu does not include GCC by default. Install it with sudo apt install build-essential, which provides gcc, g++, and make. If you installed a specific version like gcc-14, either use the versioned binary (gcc-14) directly or register it with update-alternatives to create the unversioned gcc symlink.
๐ŸŒ
linuxcapable.com
linuxcapable.com โ€บ home โ€บ ubuntu โ€บ how to install gcc on ubuntu (26.04, 24.04, 22.04)
How to Install GCC on Ubuntu (26.04, 24.04, 22.04) - LinuxCapable
What is the default GCC version on each Ubuntu LTS release?
Ubuntu 26.04 LTS defaults to GCC 15 with versions 11 through 16 available, Ubuntu 24.04 LTS defaults to GCC 13 with versions 9 through 14 available, and Ubuntu 22.04 LTS defaults to GCC 11 with versions 9 through 12 available. Install the build-essential package to get the default compiler for your release.
๐ŸŒ
linuxcapable.com
linuxcapable.com โ€บ home โ€บ ubuntu โ€บ how to install gcc on ubuntu (26.04, 24.04, 22.04)
How to Install GCC on Ubuntu (26.04, 24.04, 22.04) - LinuxCapable
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.

๐ŸŒ
Ubuntu
documentation.ubuntu.com โ€บ ubuntu-for-developers โ€บ howto โ€บ gcc-setup
How to set up a development environment for GCC on Ubuntu - Ubuntu for Developers
December 2, 2025 - From within Eclipse, install CDT .../releases/latest/ for the Work with: field. ... The NetBeans C/C++ Development Pack adds support for C/C++ to NetBeans. Install with: sudo apt install default-jre sudo snap install netbeans --classic ยท From within NetBeans, install the C/C++ Pack by going to Tools โ€ฃ Plugins โ€ฃ Available Plugins โ€ฃ Install. The standard debugger developed for GCC is the GNU ...
๐ŸŒ
GitHub
gist.github.com โ€บ application2000 โ€บ 73fd6f4bf1be6600a2cf9f56315a2d91
How to install latest gcc on Ubuntu LTS (12.04, 14.04, 16.04) ยท GitHub
Instead, it installs the older "10.0.1 20200416 (experimental) (Ubuntu 10-20200416-0ubuntu1)" -- because the older version is the only one there at that PPA site for release "Focal". The other package "10.1.0-2ubuntu1~18.04" is for "Bionic" not "Focal". On 20.04, run the following commands today (May 25th, 2020) and it shows that none of the packages are 10.1: sudo add-apt-repository ppa:ubuntu-toolchain-r/test sudo apt update sudo apt list -a gcc-10
๐ŸŒ
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 - The first method that we can follow to install GCC on Ubuntu is using the APT package manager. Before checking out the step-by-step process, letโ€™s note that the Ubuntu Universe repository contains GCC-14 (14.2.0, specifically), which is the latest version available.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ install-gcc-ubuntu22-04
How to install GCC on Ubuntu 22.04 โ€“ Linux Hint
For the purpose of installing GCC on Ubuntu 22.04, first update it by using the command $ sudo apt update then install the build-essential package that contains the GCC package through the command $ sudo apt install build-essential. Then use the $ gcc โ€“version command to verify the successful ...
Find elsewhere
๐ŸŒ
LinuxCapable
linuxcapable.com โ€บ home โ€บ ubuntu โ€บ how to install gcc on ubuntu (26.04, 24.04, 22.04)
How to Install GCC on Ubuntu (26.04, 24.04, 22.04) - LinuxCapable
December 6, 2023 - This command imports the PPAโ€™s GPG signing key and adds the repository configuration to /etc/apt/sources.list.d/. After adding the PPA, install GCC versions using the same sudo apt install gcc-NN g++-NN commands shown above.
๐ŸŒ
Ubuntu
documentation.ubuntu.com โ€บ ubuntu-for-developers โ€บ reference โ€บ availability โ€บ gcc
Available GCC versions - Ubuntu for Developers
1 week 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.
๐ŸŒ
Sorush Khajepor
iamsorush.com โ€บ posts โ€บ build-gcc11
Build GCC 11 from source on Ubuntu
This procedure is probably valid for future versions of GCC too. You need a C and C++ compiler found in $PATH to compile the new version. Try gcc and g++ in a terminal, if not there, install the system default ones ... Go to GCC releases on GitHub, download the latest version in the format of tar.g...
๐ŸŒ
GNU
gcc.gnu.org โ€บ install
Installing GCC - GNU Project
The latest version of this document is always available at https://gcc.gnu.org/install/. It refers to the current development sources, instructions for specific released versions are included with the sources.
๐ŸŒ
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).
๐ŸŒ
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.
Top answer
1 of 1
11

If you were considering updating to a more recent long-term supported (LTS) version of Ubuntu, it might be a good time to do it now: Ubuntu 22.04LTS allows you to just install GCC 12's g++ using apt install g++-12, done. I predict that unless you have a lot of software depending on obsolete dependency libraries, updating your Ubuntu might be the easiest way to get g++12! (You can easily try that out in a podman or docker container, which I'll not go into here.)

If you're stuck to Ubuntu 20.04, I guess the cleanest and most straightforward way of getting g++12 will be to try and backport the 22.04 g++12 package. In that case, you don't have to worry about learning how to build GCC, and how to install a competing g++ that doesn't break all your system by interfering with existing compilers and even worse ABIs. Debian (and Ubuntu as kind-of-downstream of that) have taken the workload of learning that, and you just need to use their tools. Awesome!

Roughly speaking, the procedure to do that is this:

  1. Prepare to build a Debian/Ubuntu package:

    sudo apt update
    sudo apt install -y build-essential fakeroot dpkg-dev devscripts cmake debhelper-compat dh-python equivs
    
  2. Get the package sources. You will need to find the link to the .dsc file on the package page:

    mkdir gcc-12
    cd gcc-12
    dget -u https://URL/OF/DSC/FILE.dsc
    
  3. Install the build dependencies from the .dsc:

    mk-build-deps -i gcc-12*.dsc
    
  4. Build the packages:

    cd gcc-12-somethingsomething
    dpkg-buildpackage -us -uc -ui -d
    

So far for theory. In practice, step 3 will fail โ€“ GCC12 depends on tools whose version shipped by Ubuntu 20.04 is simply too old. Especially, the Debian package says you'd need at least g++11, binutils >= 2.37.

This is really the point where I personally say "If some software that I build needs a bleeding edge C++ compiler, it won't be happy with my Ubuntu 20.04 environment, anyway, and I need to update to a more modern Ubuntu". So, again that's my recommendation; update to Ubuntu 22.04, and then apt install g++-12.

If you want to live with the hassle, you will start to modify the gcc-12-12.*/debian/control and rules.conf, rules2 files to depend on just some g++ instead of g++11, disable ADA support, be less demanding of binutils etc.

However, you can be darn sure that the Debian folks thought about which versions of dependencies to use, and what fixes and backports to include. This is a rocky road. Probably less rocky than a "bare" source build, but it's an easy way to learn way more about GCC than you ever intended, and spend a couple of hours just to start the build.

Honestly, I keep repeating myself, but: if you suddenly find yourself in need of a modern compiler, it's a good time to update to a modern environment. That's way less pain and has way more benefits.

Top answer
1 of 5
20

I'm still on Ubuntu 22.04 LTS but needed g++14. The sudo apt-get gcc-14 did not work for me, as it installed clang++14 for some reason (perhaps a misconfiguration on my part). What did work for me was following the instructions I found at https://www.dedicatedcore.com/blog/install-gcc-compiler-ubuntu/

The steps I took:

sudo apt install build-essential
sudo apt install libmpfr-dev libgmp3-dev libmpc-dev -y
wget https://ftp.gnu.org/gnu/gcc/gcc-14.1.0/gcc-14.1.0.tar.gz
tar -xf gcc-14.1.0.tar.gz
cd gcc-14.1.0
./configure -v --build=$(uname -m)-linux-gnu --host=$(uname -m)-linux-gnu --target=$(uname -m)-linux-gnu --prefix=/usr/local/gcc-14.1.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib --program-suffix=-14.1.0
make
sudo make install

And if you would like to make it the default:

sudo update-alternatives --install /usr/bin/g++ g++ /usr/local/gcc-14.1.0/bin/g++-14.1.0 14
sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/gcc-14.1.0/bin/gcc-14.1.0 14

After that, g++ showed I was running version 14.1.0. I was then able to compile my project that included some c++20/23 features that were not in the previous versions of g++ (chrono/format).

2 of 5
15

GCC-14 (and G++-14) is available in the Universe repository for Ubuntu 24.04, as evident in the Ubuntu Package archive.

It is equally evident that this package is not available for Ubuntu 22.04, so installing this on 22.04 will require some third-party interference, or you have to compile it yourself.

See here on how to enable the Universe repositories.

Top answer
1 of 4
50

gcc-12 is not available in ubuntu 20.04, so we need to compile it from source code, here are the steps which I borrowed from this video:

  • Step 1: clone gcc source code and checkout gcc-12 branch
$ git clone https://gcc.gnu.org/git/gcc.git gcc-source
$ cd gcc-source/
$ git branch -a
$ git checkout remotes/origin/releases/gcc-12
  • Step 2: make another build dir

Note this is important as running ./configure from within the source directory is not supported as documented here.

$ mkdir ../gcc-12-build
$ cd ../gcc-12-build/
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++
  • Step 3: installing GCC prequisites and run configure again

The missing libraries will be shown in above ./confgiure output, search and install them one by one.

$ apt-cache search MPFR
$ sudo apt-get install libmpfrc++-dev
$ apt-cache search MPC | grep dev
$ sudo apt-get install libmpc-dev
$ apt-cache search GMP | grep dev
$ sudo apt-get install libgmp-dev
$ sudo apt-get install gcc-multilib
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++

An alternative is to run the download_prerequisites script.

$ cd ../
$ cd gcc-source/
$ ./contrib/download_prerequisites
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++
  • Step 4: compile gcc-12
$ make -j16

Still flex is missing:

$ sudo apt-get install flex
$ ./../gcc-source/configure --prefix=$HOME/install/gcc-12 --enable-languages=c,c++
$ make -j16
$ make install

Another way is to use Ubuntu 22.04 where gcc-12 is available. In Ubuntu 22.04, gcc-12 can be installed with apt:

$ sudo apt install gcc-12
2 of 4
16

You can use Homebrew to install pre-built binaries. Follow instructions to install Homebrew at https://brew.sh/, then

brew install gcc for default GCC (currently 11) or brew install gcc@12 for gcc-12.

Note that it may compile missing dependencies.

๐ŸŒ
LinuxConfig
linuxconfig.org โ€บ home โ€บ how to install gcc the c compiler on ubuntu 22.04 lts jammy jellyfish linux
How to install GCC the C compiler on Ubuntu 22.04 LTS Jammy Jellyfish Linux
January 8, 2024 - $ sudo apt update $ sudo apt install build-essential ยท DO YOU NEED MULTIPLE C AND/OR C++ COMPILER VERSIONS? Visit our other tutorial on How to switch between multiple GCC and G++ compiler versions on Ubuntu 22.04 to see how to install multiple ...
๐ŸŒ
Lindevs
lindevs.com โ€บ install-gcc-on-ubuntu
Install gcc 13 on Ubuntu 22.04 | Lindevs
October 29, 2023 - This tutorial demonstrates how to install gcc 13 on Ubuntu 22.04.
๐ŸŒ
Cherry Servers
cherryservers.com โ€บ home โ€บ blog โ€บ linux โ€บ how to install gcc on ubuntu 22.04 [and compile a c program]
How to Install GCC on Ubuntu 22.04 [and Compile a C Program] | Cherry Servers
November 7, 2025 - To install build-essentials, use the following command: ... gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions.