How to Install GCC on Ubuntu 26.04, 24.04 and 22.04

Last updated Thursday, April 30, 2026 7:49 am Joshua James 12 min read 9 comments

Ubuntu systems need GCC before they can build kernel modules, native extensions, command-line tools, or C and C++ projects from source. The fastest way to install GCC compiler on Ubuntu 26.04, 24.04, and 22.04 is the build-essential package, which adds GCC, G++, Make, headers, and packaging tools in one APT install.

Use the default Ubuntu repositories for normal development because they track each release’s tested compiler stack. Ubuntu 26.04 defaults to GCC 15.x, Ubuntu 24.04 defaults to GCC 13.x, and Ubuntu 22.04 defaults to GCC 11.x. Versioned packages, the Ubuntu Toolchain Test PPA, and source builds are available when a project needs a different compiler branch.

Install GCC on Ubuntu

Update your package index before installing GCC so APT uses the latest repository metadata:

sudo apt update && sudo apt upgrade

These commands use sudo for root privileges. If your user is not in the sudoers file yet, run the commands as root or follow the guide on how to add and manage sudo users on Ubuntu.

Choose the default repository path unless you have a specific compiler-version requirement. The table below summarizes the current GCC branches available for each supported Ubuntu release.

Ubuntu ReleaseDefault GCCDefault Repository BranchesToolchain Test PPA BranchesBest Path
Ubuntu 26.04 LTSGCC 15.xGCC 11, 12, 13, 14, 15, and 16No exact GCC packages currently listedDefault repositories
Ubuntu 24.04 LTSGCC 13.xGCC 9, 10, 11, 12, 13, and 14GCC 15 and 16, plus newer GCC 14 buildsDefault repositories, or PPA for GCC 15/16
Ubuntu 22.04 LTSGCC 11.xGCC 9, 10, 11, and 12GCC 13, 14, 15, and 16Default repositories, or PPA for GCC 13+

The default repository method installs compilers tested against Ubuntu’s system libraries and receives normal APT security and point-release updates. The Ubuntu Toolchain Test PPA is useful for version-specific development, while source compilation is best reserved for compiler development or upstream feature testing.

Install the GCC Build-Essential Package

Install build-essential, a metapackage that bundles GCC, G++, Make, pkg-config, dpkg-dev, and other essential compilation tools:

sudo apt install build-essential

A metapackage bundles multiple related tools in one installation. While you can install standalone gcc, build-essential prevents you from discovering missing dependencies when compiling software later. The installation downloads approximately 50-80 MB of packages.

Verify the GCC Installation

Confirm the compiler installed correctly and check which version you received:

gcc --version
g++ --version
which gcc
which g++

The version commands confirm both the C and C++ compilers are available. The which command shows the active compiler paths, which is useful when several versioned compilers are installed side by side.

Ubuntu 26.04 LTS output:

gcc (Ubuntu 15.2.0-16ubuntu1) 15.2.0
g++ (Ubuntu 15.2.0-16ubuntu1) 15.2.0
/usr/bin/gcc
/usr/bin/g++

Ubuntu 24.04 LTS output:

gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
g++ (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
/usr/bin/gcc
/usr/bin/g++

Ubuntu 22.04 LTS output:

gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
g++ (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
/usr/bin/gcc
/usr/bin/g++

The version string includes Ubuntu’s package identifier, such as 6ubuntu2~24.04.1, indicating this is Ubuntu’s packaged compiler for your release. Exact package revisions can change after normal security or point-release updates.

Test GCC with a Sample Program

Verify GCC works correctly by compiling and running a simple test program. Create a file called hello.c:

cat <<'EOF' > hello.c
#include <stdio.h>
int main() {
    printf("Hello from GCC!\n");
    return 0;
}
EOF

Compile and run the program:

gcc hello.c -o hello
./hello

Expected output:

Hello from GCC!

If you see this message, GCC is installed and working correctly. Clean up the test files:

rm hello.c hello

Install Specific GCC Versions on Ubuntu

Install a versioned GCC package when a build system requires a specific compiler branch, such as gcc-14 or gcc-15. Run only the command for the branch you need; each command installs the matching C compiler and C++ compiler pair.

Ubuntu 26.04 LTS GCC Packages

Ubuntu 26.04 ships GCC 15 as the default compiler. The default repositories provide GCC 11 through GCC 16, so use the repository packages instead of adding the Toolchain Test PPA.

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

Verify the branch you installed by running the matching versioned binary, for example gcc-16 --version or g++-16 --version.

Ubuntu 24.04 LTS GCC Packages

Ubuntu 24.04 ships GCC 13 as the default. The default repositories provide GCC 9 through GCC 14. Use the Toolchain Test PPA section below only when you need GCC 15 or GCC 16 on Ubuntu 24.04.

GCC 14 and several older versioned packages are in Ubuntu’s Universe component. If a minimal system cannot locate them, enable Universe first; see the guide on how to enable Universe on Ubuntu.

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

Ubuntu 22.04 LTS GCC Packages

Ubuntu 22.04 ships GCC 11 as the default. The default repositories provide GCC 9 through GCC 12. Use the Toolchain Test PPA section below if you need GCC 13, GCC 14, GCC 15, or GCC 16 on Ubuntu 22.04.

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

Install GCC from the Ubuntu Toolchain Test PPA

The Ubuntu Toolchain Test PPA provides newer compiler builds for developers who need branches not available in their Ubuntu release. Use it only when a project specifically requires that branch, because PPA packages can update compiler runtime libraries outside the normal Ubuntu repository cadence.

Current PPA metadata provides GCC 15 and GCC 16 for Ubuntu 24.04, and GCC 13 through GCC 16 for Ubuntu 22.04. Ubuntu 26.04 already provides GCC 15 and GCC 16 from the default repositories, and the PPA currently does not list exact gcc/g++ packages for that release.

Install the repository helper if your system does not already include it, then add the test PPA:

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

Install the required branch after APT refreshes the PPA metadata. For GCC 15 on Ubuntu 24.04 or 22.04, run:

sudo apt install gcc-15 g++-15

For GCC 14 on Ubuntu 22.04, use the same PPA and install this branch:

sudo apt install gcc-14 g++-14

For GCC 13 on Ubuntu 22.04, install the GCC 13 pair instead:

sudo apt install gcc-13 g++-13

Confirm APT is selecting the PPA package before installing a PPA-only branch:

apt-cache policy gcc-15 g++-15

The candidate version should point to ppa.launchpadcontent.net/ubuntu-toolchain-r/test on Ubuntu 24.04 or Ubuntu 22.04. If the candidate is empty, recheck your Ubuntu release and repository configuration before continuing.

Compile GCC from Source on Ubuntu

Compiling GCC from source provides access to the latest upstream releases directly from the GNU project before they reach Ubuntu’s repositories. 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 take substantially longer than APT installs and require several gigabytes of temporary disk space. Reserve this method for scenarios where repository versions lack required features or when you need precise control over the compiler toolchain configuration.

Install GCC Build Dependencies on Ubuntu

Install the prerequisite development tools and mathematical libraries GCC requires:

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

These packages provide the base compiler toolchain (build-essential), lexical analysis and parsing tools (flex, bison), mathematical precision libraries (GMP, MPC, MPFR), optimization frameworks (ISL), and documentation generation (texinfo). Without these dependencies, the GCC build process fails with 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. The commands below use the curl command to download GCC 15.2.0, which was the current stable GCC release when this article was checked:

GCC_VERSION=15.2.0
curl -fLO --progress-bar "https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.xz"
tar -xf "gcc-${GCC_VERSION}.tar.xz"
cd "gcc-${GCC_VERSION}"

Check the official GCC releases page before source-building. If a newer stable release is required, change GCC_VERSION and adjust the later /usr/local/gcc-15 paths to the matching major version.

The download pulls approximately 90-120 MB compressed, expanding to roughly 900 MB when extracted. The tar -xf command extracts the archive, automatically detecting the .xz compression format.

Download the prerequisite libraries that GCC needs for its build system:

./contrib/download_prerequisites

This 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:

gmp-6.3.0.tar.bz2: OK
mpfr-4.2.1.tar.bz2: OK
mpc-1.3.1.tar.gz: OK
isl-0.26.tar.bz2: OK
All prerequisites downloaded successfully.

Configure and Build GCC from Source

GCC’s build system requires compiling in a separate directory to keep the source tree clean. Create a build directory:

mkdir build && cd build

Configure the build with your desired installation prefix and language support:

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

Configuration options:

  • --prefix=/usr/local/gcc-15 sets the installation directory, keeping it separate from system packages
  • --disable-multilib skips 32-bit library support on 64-bit systems, reducing build time and disk space unless you specifically need to compile 32-bit binaries
  • --enable-languages=c,c++ builds only C and C++ compilers; add fortran, go, or ada if your projects require them

The configure script analyzes your system, checks dependencies, and generates Makefiles. It 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

Compile GCC using all available CPU cores:

make -j"$(nproc)"

The -j flag enables parallel compilation jobs, while $(nproc) automatically detects your CPU core count. Build time depends heavily on CPU speed, storage performance, and the languages enabled.

Install the compiled binaries, libraries, and documentation:

sudo make install

This copies the compiled compiler to /usr/local/gcc-15/ when using the GCC 15.2.0 example above.

Register the Source-Built GCC Compiler

Register the new compiler with update-alternatives so your shell locates it automatically:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/gcc-15/bin/gcc 100 \
  --slave /usr/bin/g++ g++ /usr/local/gcc-15/bin/g++ \
  --slave /usr/bin/gcov gcov /usr/local/gcc-15/bin/gcov

This command creates symbolic links in /usr/bin/ pointing to your source-built compiler, making it available system-wide without modifying PATH variables. The --slave options ensure g++ and gcov switch automatically when you change the active GCC version. The priority value (100) determines which compiler becomes the default when multiple versions are registered.

Verify the installation:

gcc --version
which gcc

Expected output confirming the source-built compiler is active:

gcc (GCC) 15.2.0
Copyright (C) 2025 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.

/usr/bin/gcc

The version string shows plain gcc (GCC) 15.2.0 without Ubuntu’s package identifiers, distinguishing source builds from repository packages. The which gcc output shows /usr/bin/gcc because update-alternatives creates a symlink there pointing to your source-built compiler.

Configure GCC Runtime Library Paths

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

printf '%s\n' "/usr/local/gcc-15/lib64" | sudo tee /etc/ld.so.conf.d/gcc-15.conf > /dev/null
sudo ldconfig

The first command creates a configuration file telling the dynamic linker where to find GCC’s libraries. The ldconfig command rebuilds the linker cache, making the libraries immediately available. Without this step, programs compiled with the new GCC may fail at runtime with libstdc++.so.6 or similar missing library errors.

Verify the library path was added:

ldconfig -p | grep libstdc++ | head -2

The output lists both your source-built library and the system library:

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

Source-built GCC does not receive automatic security updates through the package manager. This installation method suits development machines, isolated build environments, or non-production systems. Production servers should use packaged GCC from the default repository, which receives timely security patches. To update a source-built compiler, download the newer release, rebuild with the same configure flags and --prefix, and run sudo make install to overwrite the existing installation.

Configure and Switch Between GCC Versions on Ubuntu

Developers working on multiple projects or testing code across compiler versions often need several GCC releases installed simultaneously. Ubuntu packages install versioned binaries such as gcc-14 and g++-14; register them with update-alternatives when you want the unversioned gcc and g++ commands to switch between branches.

Register Multiple GCC Versions on Ubuntu

After installing multiple GCC versions, register each one with update-alternatives. The priority number determines which compiler becomes the system default when you run gcc without a version suffix. Higher numbers take precedence.

Ubuntu 26.04 LTS (GCC 11-16):

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-16 160 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-16 \
  --slave /usr/bin/gcov gcov /usr/bin/gcov-16

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-15 150 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-15 \
  --slave /usr/bin/gcov gcov /usr/bin/gcov-15

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

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 130 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-13 \
  --slave /usr/bin/gcov gcov /usr/bin/gcov-13

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-12 \
  --slave /usr/bin/gcov gcov /usr/bin/gcov-12

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

Ubuntu 24.04 LTS (GCC 9-14):

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

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 130 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-13 \
  --slave /usr/bin/gcov gcov /usr/bin/gcov-13

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-12 \
  --slave /usr/bin/gcov gcov /usr/bin/gcov-12

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

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

Ubuntu 22.04 LTS (GCC 9-12):

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-12 \
  --slave /usr/bin/gcov gcov /usr/bin/gcov-12

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

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

If you installed GCC 15 from the Toolchain Test PPA on Ubuntu 24.04 or 22.04, register it with the same pattern:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-15 150 \
  --slave /usr/bin/g++ g++ /usr/bin/g++-15 \
  --slave /usr/bin/gcov gcov /usr/bin/gcov-15

The --slave options ensure g++ and gcov versions automatically track the selected gcc version, maintaining consistency across the toolchain.

Switch Between GCC Versions on Ubuntu

Verify which version became the system default:

gcc --version

Switch between installed GCC versions interactively:

sudo update-alternatives --config gcc

Example output on Ubuntu 24.04 with multiple versions registered:

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

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-14   140        auto mode
  1            /usr/bin/gcc-9    90         manual mode
  2            /usr/bin/gcc-10   100        manual mode
  3            /usr/bin/gcc-11   110        manual mode
  4            /usr/bin/gcc-12   120        manual mode
  5            /usr/bin/gcc-13   130        manual mode
  6            /usr/bin/gcc-14   140        manual mode

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

Enter the number corresponding to your desired default compiler. The selection takes effect immediately across the system.

Troubleshooting GCC Installation Issues on Ubuntu

GCC Command Not Found on Ubuntu

Build scripts or Makefiles that reference gcc without a version suffix fail with gcc: command not found or /bin/sh: 1: gcc: not found when no unversioned symlink exists. If the error names a specific branch, such as /bin/sh: 1: gcc-12: not found, install that matching versioned package instead.

Install build-essential to get the default compiler with all standard symlinks:

sudo apt install build-essential

If you need a specific version as the default gcc command, register it with update-alternatives:

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

Unable to Locate GCC Versioned Packages on Ubuntu

If APT returns “Unable to locate package gcc-14” or a similar error, first confirm that the requested branch exists for your Ubuntu release. Ubuntu 26.04 provides GCC 11 through GCC 16 in the default repositories, Ubuntu 24.04 provides GCC 9 through GCC 14 by default, and Ubuntu 22.04 needs the Toolchain Test PPA for GCC 13 and newer.

sudo apt update
apt-cache policy gcc-15 g++-15

Replace gcc-15 g++-15 with the branch you need. If the candidate is empty after adding the correct repository, recheck your Ubuntu release and the package matrix near the start of this article before trying to install the package.

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.

GCC Source Build Compilation Failures on Ubuntu

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

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

Check for error messages during dependency installation and ensure you have sufficient free disk space:

df -h .

You need at least 4-6 GB of free space during compilation. Insufficient disk space causes cryptic compilation errors like “No space left on device” or unexpected build failures without clear error messages.

Multiple GCC Versions Not Switching on Ubuntu

If sudo update-alternatives --config gcc shows installed versions but switching fails, verify each version’s binary exists:

ls -l /usr/bin/gcc-*

This displays all installed GCC compiler binaries with their version suffixes. Only register versions that actually exist on your system.

Fix GCC cpp Master Alternative Conflict on Ubuntu

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

This occurs because cpp is already registered as its own master alternative. Ubuntu registers cpp separately by default.

Solution: Drop cpp from the slave list and only tie g++ and gcov to gcc. This approach works because gcc invokes the correct preprocessor internally without needing cpp as an explicit slave:

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

This approach works reliably on all Ubuntu releases.

Kernel Module or DKMS GCC Version Mismatch on Ubuntu

DKMS or out-of-tree kernel module builds can fail when the module build uses a different GCC branch than the one used for the running kernel. Check the active compiler and the kernel build string before changing alternatives:

gcc --version
cat /proc/version

If the kernel build string names a specific compiler branch, install that branch and use the versioned binary directly or register it with update-alternatives for the module build. The example below uses GCC 13; on Ubuntu 22.04, add the Toolchain Test PPA first if the required branch is not in the default repositories.

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

Adjust the package number to match the compiler shown by your error message. For DKMS problems, also confirm the matching Ubuntu kernel headers are installed for the running kernel.

GCC Runtime Library Compatibility Issues on Ubuntu

Newer GCC versions introduce ABI changes to libstdc++.so.6. Programs compiled with GCC 14 or 15 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 binaries compiled with newer GCC to systems running older GCC versions. Solutions include:

  • Ship libstdc++.so.6 from your GCC 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 compatible GCC runtime libraries installed

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

Update GCC on Ubuntu

APT-installed GCC receives patch updates automatically through the standard system upgrade process:

sudo apt update && sudo apt upgrade

This applies security patches and point releases (e.g., GCC 13.3.0-6 to 13.3.0-7) without changing your compiler’s major version. To upgrade to a newer GCC major version from the repositories, install the target version and update your default:

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

For source-compiled GCC, download the newer release from the GNU releases page, rebuild using the same --prefix and configure flags from the source-build section, and run sudo make install to overwrite the previous installation in place. Re-run sudo ldconfig afterward to refresh the runtime library cache.

Remove GCC from Ubuntu

The cleanup process depends on how you installed the compiler. Follow the section that matches your installation method.

Remove APT-Installed GCC from Ubuntu

List installed GCC packages first, then remove only the metapackage and versioned compilers you no longer need:

dpkg-query -W -f='${db:Status-Abbrev} ${binary:Package}\n' 'gcc*' 'g++*' 2>/dev/null | grep '^ii'
sudo apt remove build-essential gcc g++

If the installed-package list includes versioned compilers you also want to remove, run a separate command such as sudo apt remove gcc-15 g++-15, replacing the branch number with the packages you actually installed. Preview orphaned dependency cleanup before removing anything else:

sudo apt autoremove --purge --dry-run

Continue only if the preview lists packages you intend to remove:

sudo apt autoremove --purge

Remove the GCC Toolchain Test PPA from Ubuntu

If you added the Ubuntu Toolchain Test PPA, remove it along with its configuration. For a broader guide on PPA management, see how to remove a PPA on Ubuntu.

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

Verify the PPA was removed:

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

This command should return no output after successful removal.

Remove Source-Compiled GCC from Ubuntu

The following commands permanently delete the source-compiled GCC 15 binaries, libraries, and documentation from /usr/local/gcc-15/. This does not affect GCC versions installed through APT or the Ubuntu repositories. Ensure you have a working alternative compiler before proceeding.

Remove the compiler from update-alternatives:

sudo update-alternatives --remove gcc /usr/local/gcc-15/bin/gcc

Remove the installation directory and linker configuration:

sudo rm -rf /usr/local/gcc-15
sudo rm /etc/ld.so.conf.d/gcc-15.conf
sudo ldconfig

Clean up the build directory if you no longer need it:

rm -rf ~/gcc-15.2.0

Replace gcc-15.2.0 with the source directory name you downloaded earlier if you used a different release.

Conclusion

With GCC compiler installed on Ubuntu, switching between multiple compiler versions requires only a single update-alternatives --config gcc command whether you use repository packages or source builds. Complete your development toolchain: install CMake on Ubuntu for build automation, set up Git on Ubuntu for version control, or explore Python virtual environments on Ubuntu for compiling extension modules.

Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffee Buy me a coffee

9 thoughts on “How to Install GCC on Ubuntu 26.04, 24.04 and 22.04”

  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
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<blockquote>quote</blockquote> quote block

Got a Question or Feedback?

We read and reply to every comment - let us know how we can help or improve this guide.

Let us know you are human: