How to Install GCC on Ubuntu Linux

The GNU Compiler Collection (GCC) is the open-source compiler toolchain that builds Linux kernel drivers, system utilities, scientific applications, and software demanding standards compliance and performance tuning. Whether compiling C, C++, Objective-C, Fortran, Ada, Go, or other languages, GCC delivers the optimization techniques and cross-platform support that makes it essential for software development across Linux systems.

This guide covers installing GCC on Ubuntu through the default repository or Ubuntu Toolchain PPA for newer versions, configuring multiple GCC versions with update-alternatives, and switching between compiler versions for different development needs. You’ll learn both standard installation for general development and advanced multi-version setups for projects requiring specific compiler releases.

Choose Your GCC Installation Method

Ubuntu provides three distinct paths for installing GCC, each serving different development scenarios. Understanding these options helps you balance stability, version recency, and maintenance effort based on your project requirements.

Update Ubuntu Before Installation

Before installing GCC, update your system packages to ensure all existing packages are current and avoid version conflicts during installation. This is similar to running Windows Update before installing development tools.

sudo apt update
sudo apt upgrade

The apt update command refreshes the package index (the list of available software), while apt upgrade installs any pending updates. These commands ensure your package manager has the latest repository information before installing new software.

Installation MethodGCC Versions AvailableBest ForTrade-offs
Ubuntu Default RepositoryStable releases tested with your Ubuntu versionGeneral development, production builds, systems requiring long-term stabilityOlder compiler versions may lack the newest language standards or optimizations
Ubuntu Toolchain PPAMultiple GCC branches maintained by Ubuntu (availability varies by release)Projects needing newer compiler versions without upgrading Ubuntu, testing across multiple GCC releasesPre-packaged convenience but still slightly behind upstream GCC
Compile from SourceLatest upstream GCC releases directly from GNUAbsolute newest features, optimization testing, contributing to GCC developmentTime-consuming build process, manual dependency management, no automated updates

The default repository provides compiler versions thoroughly tested against Ubuntu’s system libraries, ensuring maximum compatibility and minimizing unexpected build failures. Think of this as the stable, well-tested option similar to using Visual Studio’s default compiler on Windows. The Toolchain PPA (Personal Package Archive, Ubuntu’s community repository system) delivers newer compiler versions without requiring a full Ubuntu upgrade, striking the right balance between convenience and version recency for most developers. Source compilation delivers bleeding-edge upstream releases but requires significant time and manual maintenance; most developers find the PPA offers the best balance between features and convenience.

Method 1: Install GCC with the Ubuntu Repository

The Ubuntu default repository provides the most straightforward GCC installation path for general development. This method installs the compiler version officially supported by your Ubuntu release, thoroughly tested against system libraries for maximum compatibility.

Open a terminal with Ctrl+Alt+T (similar to launching Command Prompt or PowerShell on Windows). Most developers should install build-essential, a metapackage that bundles GCC, G++, Make, pkg-config, dpkg-dev, and other essential compilation tools. A metapackage is a convenient bundle that installs multiple related tools at once, similar to how Visual Studio installs the entire C++ toolchain rather than individual components. While you can install standalone gcc, build-essential prevents you from discovering missing dependencies when attempting to compile software later.

sudo apt install build-essential

The installation downloads approximately 50-80 MB of packages and typically completes within a few minutes depending on your internet connection speed. Once finished, verify the installation and confirm the compiler version:

gcc --version
which gcc

The gcc --version command displays the installed compiler version and copyright information, confirming GCC is accessible. The which gcc command shows the full filesystem path where the GCC binary is located (typically /usr/bin/gcc), which helps verify the system is using the correct compiler when multiple versions are installed. Think of PATH as Windows’ environment variables system that tells the shell where to find programs.

Example output on Ubuntu 24.04:

gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The version string shows GCC 13.3.0 with Ubuntu’s package identifier 6ubuntu2~24.04, indicating this is Ubuntu’s packaged version specifically built for Ubuntu 24.04. Your version will differ based on your Ubuntu release.

Example output from which gcc:

/usr/bin/gcc

This confirms the system located GCC in the standard binary directory where most system commands reside.

Method 2: Install GCC via Toolchain PPA

The Ubuntu Toolchain PPA (Personal Package Archive) delivers newer GCC versions beyond what Ubuntu’s default repositories provide. PPAs are community-maintained repositories that extend Ubuntu’s official package collection, similar to third-party package sources on other platforms. This method is ideal when you need newer C++ language standards (C++20, C++23) or modern optimization features without waiting for Ubuntu’s next LTS release.

GCC Version Availability by Ubuntu Release

Check which GCC branches your Ubuntu release already ships and use the built-in repositories first. If your release needs the Toolchain PPA for these packages, complete the PPA add and sudo apt update steps below, then run the install commands. Each command pulls both the C compiler (gcc) and the C++ compiler (g++).

Ubuntu 24.04 LTS (GCC 11-14)

Ubuntu 24.04 ships GCC 14 in the Universe repository by default. You can install gcc-14 and g++-14 directly without adding the Toolchain PPA if Universe is enabled. Check with apt search gcc-14 first. The Toolchain PPA provides additional versions (GCC 11-13) and updates, but isn’t required for GCC 14 on Ubuntu 24.04.

If apt search shows no GCC 14 packages, enable the Universe repository and refresh your package index:

sudo add-apt-repository universe
sudo apt update

Ubuntu 24.04 (Noble) already ships GCC 11 through GCC 14 in the main and Universe repositories. Use the built-in repos first and add the Toolchain PPA only if you specifically want its test builds or prefer keeping GCC 11-13 aligned with PPA updates:

Grab GCC 14 and verify:

sudo apt install gcc-14 g++-14
gcc-14 --version

Next, install GCC 13 and verify:

sudo apt install gcc-13 g++-13
gcc-13 --version

Then install GCC 12 and verify:

sudo apt install gcc-12 g++-12
gcc-12 --version

Finally, add GCC 11 and verify:

sudo apt install gcc-11 g++-11
gcc-11 --version

Set a Toolchain PPA compiler as the default when needed. Example for GCC 14:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 110 --slave /usr/bin/g++ g++ /usr/bin/g++-14 --slave /usr/bin/cpp cpp /usr/bin/cpp-14 --slave /usr/bin/gcov gcov /usr/bin/gcov-14
sudo update-alternatives --config gcc

Use the same pattern for GCC 11, 12, or 13 (adjust the version suffix and priority) so gcc, g++, cpp, and gcov move together.

Ubuntu 22.04 LTS (GCC 10-12)

GCC 14 is not available for Ubuntu 22.04 through the default repositories or the Toolchain PPA. Ubuntu 22.04 users needing GCC 14 must compile from source (Method 3) or upgrade to Ubuntu 24.04. The Toolchain PPA provides GCC 10 through GCC 12 only for this release.

Ubuntu 22.04 (Jammy) offers GCC 10 through GCC 12 from the Toolchain PPA. Install only the versions you need instead of pulling every branch to keep downloads small. If you need these packages, add the PPA using the next section before running the commands. Examples:

sudo apt install gcc-12 g++-12
gcc-12 --version
sudo apt install gcc-11 g++-11
gcc-11 --version
sudo apt install gcc-10 g++-10
gcc-10 --version

After installing a PPA compiler, make it the default when needed:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 90 --slave /usr/bin/g++ g++ /usr/bin/g++-12 --slave /usr/bin/cpp cpp /usr/bin/cpp-12 --slave /usr/bin/gcov gcov /usr/bin/gcov-12
sudo update-alternatives --config gcc

Use the same pattern with other installed PPA versions (swap the version suffix and priority) so gcc, g++, cpp, and gcov stay in sync.

Add the Ubuntu Toolchain PPA

Add the Toolchain PPA only when your release needs it (for example, Ubuntu 22.04) or when you want PPA-maintained builds on newer releases. If add-apt-repository is missing on minimal or CI images, install it first with sudo apt install software-properties-common, then import the PPA:

sudo add-apt-repository ppa:ubuntu-toolchain-r/ppa -y

The -y flag automatically confirms the PPA addition without prompting, similar to silent installation flags on Windows. This command imports the PPA’s GPG signing key and adds the repository configuration to /etc/apt/sources.list.d/.

Expected output:

Repository: 'deb https://ppa.launchpadcontent.net/ubuntu-toolchain-r/ppa/ubuntu noble main'
Description:
Toolchain test builds; see https://wiki.ubuntu.com/ToolChain
More info: https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/ppa
Adding repository.
Adding deb entry to /etc/apt/sources.list.d/ubuntu-toolchain-r-ubuntu-ppa-noble.sources
Adding disabled deb-src entry to /etc/apt/sources.list.d/ubuntu-toolchain-r-ubuntu-ppa-noble.sources

Next, update the package index to refresh the repository metadata with the newly added PPA packages:

sudo apt update

This command refreshes the list of available packages from all configured repositories, including the PPA you just added. You should see the Toolchain PPA listed among the repositories being scanned.

Method 3: Compile GCC from Source

Compiling GCC from source provides access to the absolute latest upstream releases directly from the GNU project before they reach Ubuntu’s repositories or the Toolchain PPA. This method delivers complete control over compilation options, language support, and optimization flags, making it suitable for compiler development, testing bleeding-edge features, or environments requiring specific build configurations unavailable in packaged versions.

Source builds typically consume tens of minutes on modern multi-core systems and require 4-6 GB of temporary disk space during compilation. The build process downloads source archives, compiles hundreds of translation units, and links multiple libraries, similar to building a large Visual Studio C++ project from scratch. Reserve this method for scenarios where the Toolchain PPA’s newest versions still lack required features or when you need precise control over the compiler toolchain configuration.

Install Build Dependencies

Before compiling GCC, install the prerequisite development tools and mathematical libraries GCC requires for its build system:

sudo apt install build-essential flex bison libgmp3-dev libmpc-dev libmpfr-dev libisl-dev texinfo

These packages provide essential compilation tools: build-essential supplies the base compiler toolchain, flex and bison handle lexical analysis and parsing, the -dev packages provide mathematical precision libraries (GMP, MPC, MPFR) and optimization frameworks (ISL), while texinfo generates documentation. Without these dependencies, the GCC build process will fail with cryptic errors about missing headers or libraries.

Download and Extract GCC Source Code

Download your preferred GCC source archive from the official GNU GCC release page. This example uses GCC 14.1.0; replace the version number with your desired release:

wget https://ftp.gnu.org/gnu/gcc/gcc-14.1.0/gcc-14.1.0.tar.xz
tar -xf gcc-14.1.0.tar.xz
cd gcc-14.1.0

The download pulls approximately 90-120 MB compressed (expands to ~900 MB when extracted). The wget command downloads files over HTTP/HTTPS, while tar -xf extracts the compressed archive automatically detecting the .xz compression format.

Next, download the prerequisite libraries that GCC needs for its build system:

./contrib/download_prerequisites

This convenience script automatically downloads the exact GMP, MPFR, MPC, and ISL library versions that match your GCC release, placing them in the source tree where the build system expects them. This prevents version mismatch errors that commonly break source builds when using system-installed math libraries.

Expected output:

2024-06-15 10:32:15 URL:https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.3.0.tar.bz2 [2493916/2493916] -> "gmp-6.3.0.tar.bz2" [1]
gmp-6.3.0.tar.bz2: OK
2024-06-15 10:32:18 URL:https://gcc.gnu.org/pub/gcc/infrastructure/mpfr-4.2.1.tar.bz2 [1626517/1626517] -> "mpfr-4.2.1.tar.bz2" [1]
mpfr-4.2.1.tar.bz2: OK
2024-06-15 10:32:20 URL:https://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.3.1.tar.gz [773573/773573] -> "mpc-1.3.1.tar.gz" [1]
mpc-1.3.1.tar.gz: OK
2024-06-15 10:32:23 URL:https://gcc.gnu.org/pub/gcc/infrastructure/isl-0.26.tar.bz2 [2266821/2266821] -> "isl-0.26.tar.bz2" [1]
isl-0.26.tar.bz2: OK
All prerequisites downloaded successfully.

Configure and Build GCC

GCC’s build system requires compiling in a separate directory to keep the source tree clean, a best practice for large software projects. Create a build directory, configure the compilation options, and compile GCC with parallel jobs:

mkdir build && cd build

Now configure the GCC build with your desired installation prefix and language support:

../configure --prefix=/usr/local/gcc-14.1 --disable-multilib --enable-languages=c,c++

The --prefix option determines where GCC installs (here, /usr/local/gcc-14.1 keeps it separate from system packages). The --disable-multilib flag skips 32-bit library support on 64-bit systems, reducing build time and disk space usage unless you specifically need to compile 32-bit binaries. The --enable-languages=c,c++ option builds only C and C++ compilers; add fortran, go, ada, or other languages if your projects require them.

The configure script analyzes your system, checks dependencies, and generates Makefiles. This step typically takes 2-3 minutes and produces extensive output ending with:

checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether gcc accepts -g... yes
...
configure: creating ./config.status
config.status: creating Makefile
config.status: creating config.h
config.status: executing default commands

Once configuration completes successfully, compile GCC using all available CPU cores for parallel compilation:

make -j"$(nproc)"

The -j flag enables parallel compilation jobs, while $(nproc) automatically detects your CPU core count (similar to using all threads in a Visual Studio build). Expect this step to take 20-60 minutes depending on your hardware and the number of languages enabled. On a modern 8-core system, the build typically completes in 25-30 minutes.

The compilation produces thousands of lines of output showing each file being compiled. The final successful output looks like:

make[3]: Leaving directory '/home/user/gcc-14.1.0/build/gcc'
make[2]: Leaving directory '/home/user/gcc-14.1.0/build/gcc'
make[1]: Leaving directory '/home/user/gcc-14.1.0/build'
make: Leaving directory '/home/user/gcc-14.1.0/build'

Install the compiled GCC binaries, libraries, and documentation to your configured prefix location:

sudo make install

This copies the compiled compiler binaries, runtime libraries, header files, and documentation to /usr/local/gcc-14.1/. The installation step typically takes 2-3 minutes.

Register the Source-Built Compiler

After installation completes, register the new compiler with update-alternatives so your shell can locate it automatically when you type gcc:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/gcc-14.1/bin/gcc 80 --slave /usr/bin/g++ g++ /usr/local/gcc-14.1/bin/g++ --slave /usr/bin/cpp cpp /usr/local/gcc-14.1/bin/cpp --slave /usr/bin/gcov gcov /usr/local/gcc-14.1/bin/gcov

This command creates symbolic links in /usr/bin/ that point to your source-built compiler, making it available system-wide without modifying PATH variables. The --slave options ensure that g++, cpp, and gcov switch automatically when you change the active GCC version, keeping the entire toolchain synchronized.

Verify the installation by checking the compiler version and location:

which gcc
gcc --version

Expected output confirming the source-built compiler is active:

/usr/local/gcc-14.1/bin/gcc
gcc (GCC) 14.1.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Notice the path shows /usr/local/gcc-14.1/bin/gcc rather than /usr/bin/gcc, confirming the system is using your source-built compiler instead of the packaged version. The version string shows plain gcc (GCC) 14.1.0 without Ubuntu’s package identifiers, distinguishing source builds from repository packages.

Configure Runtime Library Paths

Finally, add the compiler’s runtime libraries to the system linker configuration so programs you compile can find the required shared libraries at runtime:

echo "/usr/local/gcc-14.1/lib64" | sudo tee /etc/ld.so.conf.d/gcc-14.1.conf
sudo ldconfig

The first command creates a configuration file telling the dynamic linker (the system component that loads shared libraries at runtime) where to find GCC’s libraries. The sudo ldconfig command rebuilds the linker cache, making the libraries immediately available. Without this step, programs you compile with the new GCC may fail at runtime with libstdc++.so.6 or similar missing library errors, even though compilation succeeded.

Verify the library path was added correctly:

ldconfig -p | grep libstdc++

This displays all registered C++ standard library versions. You should see your source-built library listed:

libstdc++.so.6 (libc6,x86-64) => /usr/local/gcc-14.1/lib64/libstdc++.so.6
libstdc++.so.6 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libstdc++.so.6

The output shows both your source-built library (/usr/local/gcc-14.1/lib64) and the system library (/usr/lib), confirming the linker can locate both versions.

Source-built GCC does not receive automatic security updates through the package manager. This installation method is suitable for development machines, isolated build environments, or non-production systems. Production servers should use packaged GCC from the default repository or Toolchain PPA, which receive timely security patches. Repeat the source compilation process whenever GNU publishes a newer release that your projects require.

Configure and Switch Between Multiple GCC Versions

Developers working on multiple projects or testing code across compiler versions often need several GCC releases installed simultaneously. The update-alternatives system manages these installations, allowing quick switching between compiler versions without reinstalling packages.

First, install all GCC versions you need. The following example covers Ubuntu 24.04 with GCC 11 through GCC 14; adjust the package list if your release offers different branches:

sudo apt install gcc-11 g++-11 gcc-12 g++-12 gcc-13 g++-13 gcc-14 g++-14

Next, configure update-alternatives to manage these versions. The priority number determines which compiler becomes the system default when you run gcc without a version suffix. Higher numbers take precedence:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 110 --slave /usr/bin/g++ g++ /usr/bin/g++-14 --slave /usr/bin/cpp cpp /usr/bin/cpp-14 --slave /usr/bin/gcov gcov /usr/bin/gcov-14

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 100 --slave /usr/bin/g++ g++ /usr/bin/g++-13 --slave /usr/bin/cpp cpp /usr/bin/cpp-13 --slave /usr/bin/gcov gcov /usr/bin/gcov-13

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 90 --slave /usr/bin/g++ g++ /usr/bin/g++-12 --slave /usr/bin/cpp cpp /usr/bin/cpp-12 --slave /usr/bin/gcov gcov /usr/bin/gcov-12

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 80 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/cpp cpp /usr/bin/cpp-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11

These commands configure GCC 14 as the default (priority 110). The --slave options ensure that g++ and gcov versions automatically track the selected gcc version, maintaining consistency across the entire toolchain.

If you are on Ubuntu 22.04, use the same pattern with GCC 10 through GCC 12:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 90 --slave /usr/bin/g++ g++ /usr/bin/g++-12 --slave /usr/bin/cpp cpp /usr/bin/cpp-12 --slave /usr/bin/gcov gcov /usr/bin/gcov-12

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 80 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/cpp cpp /usr/bin/cpp-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 70 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/cpp cpp /usr/bin/cpp-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10

Verify which version became the system default:

gcc --version

Example output:

gcc (Ubuntu 14.1.0-1ubuntu1~24.04.2) 14.1.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Switch between installed GCC versions interactively by listing all configured alternatives and selecting your preferred default:

sudo update-alternatives --config gcc

Example output:

There are 4 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-14    110        auto mode
  1            /usr/bin/gcc-11    80         manual mode
  2            /usr/bin/gcc-12    90         manual mode
  3            /usr/bin/gcc-13    100        manual mode
  4            /usr/bin/gcc-14    110        manual mode

Press <enter> to keep the current choice, or type selection number:

This displays all configured GCC versions with their priority values. Enter the number corresponding to your desired default compiler. The selection takes effect immediately across the system.

Troubleshooting Common GCC Installation Issues

PPA Repository Errors

If you encounter “Unable to locate package” errors after adding the PPA, ensure you ran sudo apt update to refresh the package index:

sudo apt update
apt search gcc-14

The search command lists all packages matching gcc-14. If no results appear, verify your Ubuntu version is supported by checking the PPA package listings filtered by release. Older Ubuntu versions receive limited PPA updates. You can also verify the PPA was added correctly:

grep -r "ubuntu-toolchain-r" /etc/apt/sources.list.d/

This displays the repository configuration file if the PPA is properly registered.

Source Build Compilation Failures

If the source build fails during the configure or make step, verify all prerequisite dependencies installed correctly:

sudo apt install build-essential flex bison libgmp3-dev libmpc-dev libmpfr-dev libisl-dev texinfo

Check for any error messages during dependency installation. Additionally, ensure you have sufficient free disk space in the directory where you’re building GCC:

df -h .

This displays available disk space in the current directory. You need at least 4-6 GB of free space during compilation. Insufficient disk space is a common cause of cryptic compilation errors like “No space left on device” or unexpected build failures without clear error messages.

Multiple GCC Versions Not Switching

If sudo update-alternatives --config gcc shows installed versions but switching doesn’t work, verify each version’s binary exists before configuring it:

ls -l /usr/bin/gcc-*

This displays all installed GCC compiler binaries with their version suffixes. Additionally, ensure you’re using the --slave option correctly so g++, cpp, and gcov track the gcc version automatically.

cpp Already Registered as Master Alternative

When configuring update-alternatives, you may encounter this error:

update-alternatives: error: alternative cpp can't be slave of gcc: it is a master alternative
update-alternatives: error: no alternatives for gcc

This occurs when cpp is already registered as its own master alternative on your system, preventing it from becoming a slave of gcc. You have two solutions:

Option 1: Keep cpp independent (simpler)

Drop cpp from the slave list and only tie g++ and gcov to gcc:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 110 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-14 \
  --slave /usr/bin/gcov gcov /usr/bin/gcov-14

sudo update-alternatives --config gcc

This approach works reliably because gcc invokes the correct preprocessor internally without needing cpp as an explicit slave.

Option 2: Make cpp a slave of gcc (full toolchain synchronization)

Remove the standalone cpp alternative first, then configure all four tools together:

sudo update-alternatives --remove-all cpp

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 110 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-14 \
  --slave /usr/bin/cpp cpp /usr/bin/cpp-14 \
  --slave /usr/bin/gcov gcov /usr/bin/gcov-14

sudo update-alternatives --config gcc

This ensures gcc, g++, cpp, and gcov all switch together when you change compiler versions. Choose Option 1 unless you specifically need cpp to follow gcc version changes.

Package Installation Installs Wrong Compiler

Some users report sudo apt install gcc-14 incorrectly installing clang instead of GCC. This typically indicates a misconfigured package cache or conflicting repository. Fix this by refreshing your package index and explicitly installing both C and C++ compilers together:

sudo apt update
sudo apt install gcc-14 g++-14

Verify you received the correct compiler by checking the version string:

gcc-14 --version

Expected output for GCC (not clang):

gcc-14 (Ubuntu 14.2.0-4ubuntu2~24.04) 14.2.0
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The output should clearly show gcc in the version string, not clang. If you still see clang, verify the package name with apt show gcc-14 to confirm it points to the correct GCC package.

Runtime Library Compatibility Issues with GCC 14

GCC 14 introduced ABI changes to libstdc++.so.6, particularly adding __cxa_call_terminate@@CXXABI_1.3.15 to the C++ runtime. Programs compiled with GCC 14 may fail on systems with older libstdc++ versions, producing errors like:

./program: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `CXXABI_1.3.15' not found

This occurs when deploying GCC 14-compiled binaries to systems running older GCC versions. Solutions include:

  • Ship libstdc++.so.6 from your GCC 14 installation alongside your binary and configure RPATH to find it
  • Statically link the C++ standard library with -static-libstdc++ during compilation (increases binary size)
  • Ensure target systems have GCC 14 runtime libraries installed before deploying your software

For source-built GCC 14, the required library typically resides at /usr/local/gcc-14.1/lib64/libstdc++.so.6 and must be included in your deployment package when targeting systems without GCC 14.

Conclusion

Your Ubuntu system now has GCC installed with the ability to switch between multiple compiler versions without reinstalling packages. Whether you chose the stable repository version for production builds, the Toolchain PPA for newer C++ standards, or source compilation for bleeding-edge features, the update-alternatives system lets you manage and switch between installed GCC releases as project requirements change. You have the complete compilation foundation needed to build everything from simple C programs to complex multi-language projects.

9 thoughts on “How to Install GCC on Ubuntu Linux”

  1. Could someone in the know tell me what I am doing wrong with update-alternatives here and how I can do it right, please:

    %> sudo update-alternatives –install /usr/bin/gcc gcc /usr/bin/gcc-14 110 –slave /usr/bin/g++ g++ /usr/bin/g++-14 –slave /usr/bin/cpp cpp /usr/bin/cpp-14 –slave /usr/bin/gcov gcov /usr/bin/gcov-14
    sudo update-alternatives –config gcc

    Returned:
    update-alternatives: error: alternative cpp can’t be slave of gcc: it is a master alternative
    update-alternatives: error: no alternatives for gcc

    Thanks for your time!

    Reply
    • Thanks for reporting this, Swamy. The error means cpp is already registered as its own master alternative on your system, so it cannot also be a slave of gcc in the same group. At the same time, gcc has no alternatives configured yet, which is why you see “no alternatives for gcc”.

      You have two options:

      1. Keep cpp independent (simpler): Drop cpp from the slave list and only tie g++ and gcov to gcc:

      sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 110 \
        --slave /usr/bin/g++ g++ /usr/bin/g++-14 \
        --slave /usr/bin/gcov gcov /usr/bin/gcov-14
      
      sudo update-alternatives --config gcc

      In most cases this is enough, because gcc will invoke the correct preprocessor internally.

      2. Make cpp a slave of gcc (matches the guide exactly): First remove the standalone cpp alternative, then re-run the full command from the article so gcc, g++, cpp, and gcov move together:

      sudo update-alternatives --remove-all cpp
      
      sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 110 \
        --slave /usr/bin/g++ g++ /usr/bin/g++-14 \
        --slave /usr/bin/cpp cpp /usr/bin/cpp-14 \
        --slave /usr/bin/gcov gcov /usr/bin/gcov-14
      
      sudo update-alternatives --config gcc

      Either approach will clear the error; the first keeps cpp independent, while the second matches the multi-version setup described in the guide.

      Reply
      • Hello Joshua,

        Thank you for your amazingly patient and well written response. I later recalled I had myself done this years ago and completely forgot about it.

        Reading your response refreshed my memory as well as increased my knowledge of how update-alternatives works. I will save your response for my future use.

        Thank you!

        Reply
    • Thanks for reporting this, yashwanthi. The “Unable to locate package” error means GCC 13 isn’t available for your Ubuntu version through the Toolchain PPA. This typically happens on Ubuntu 22.04 or older releases.

      Check your Ubuntu version to confirm which GCC releases the PPA supports:

      lsb_release -a

      Ubuntu 22.04 (Jammy) receives GCC 9 through 12 from the Toolchain PPA, while GCC 13 and 14 require Ubuntu 24.04 (Noble) or newer. If you’re on 22.04, install GCC 12 instead, which provides excellent C++20 support and works reliably:

      sudo apt install gcc-12 g++-12

      If your project specifically requires GCC 13, upgrade to Ubuntu 24.04 LTS or compile GCC 13 from source following the “Method 3: Compile GCC from Source” section above. For most development work, GCC 12 delivers the compiler features and language standards you need without the complexity of source compilation.

      Reply
  2. The PPA repository here is should be:

    sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test

    As the main ppa:ubuntu-toolchain-r/ppa does not deploy the latest GCCs (13, 14).

    Reply
    • Thanks for sharing this, test. The Ubuntu Toolchain PPA structure has changed since your comment in February 2025. The main PPA (ppa:ubuntu-toolchain-r/ppa) now includes GCC 13 and 14 for Ubuntu 24.04 (Noble), while the test PPA served as a staging area for experimental builds before stable release.

      Currently, the main PPA provides GCC 11 through 14 on Ubuntu 24.04 and GCC 9 through 12 on Ubuntu 22.04, confirmed through the PPA package listings. The test PPA historically carried newer compiler snapshots under active development, but stable releases migrate to the main PPA after testing completes.

      If you need absolute bleeding-edge GCC builds beyond what the main PPA offers, the test PPA may still carry experimental versions, though this comes with stability trade-offs. For most users, the main PPA now delivers the compiler versions you mentioned (GCC 13 and 14) without needing the test repository.

      Reply
  3. sudo apt install g++-13 gcc-13
    Reading package lists… Done
    Building dependency tree… Done
    Reading state information… Done
    E: Unable to locate package g++-13
    E: Unable to locate package gcc-13

    Reply
    • Thanks for reporting this, df. The “Unable to locate package” error typically means your Ubuntu version doesn’t have GCC 13 available in the Toolchain PPA for your specific release.

      First, verify which Ubuntu version you’re running:

      lsb_release -a

      The Ubuntu Toolchain PPA maintains different GCC versions for each Ubuntu release. If you’re on Ubuntu 22.04 (Jammy), the PPA only provides GCC 9 through GCC 12. GCC 13 and 14 require Ubuntu 24.04 (Noble) or newer. Interim releases between LTS versions may have limited PPA support since the maintainers prioritize long-term support releases.

      Check which GCC versions are available for your Ubuntu release:

      apt-cache search ^gcc-[0-9]

      If you’re on Ubuntu 22.04 and need GCC 13 specifically, your options are upgrading to Ubuntu 24.04 LTS or compiling GCC 13 from source (covered in the “Method 3: Compile GCC from Source” section above). For most development work on 22.04, GCC 12 provides excellent C++20 support and works reliably with the Ubuntu toolchain.

      Reply

Leave a Comment