The GNU Compiler Collection (GCC) is the open-source compiler toolchain that builds Linux kernel drivers, system utilities, scientific simulations, and software demanding standards compliance and performance tuning. Whether you compile kernel modules for custom hardware, build high-performance computing applications, develop embedded systems firmware, or work on open-source projects requiring specific compiler versions, GCC delivers the optimization techniques and cross-platform support essential for software development across Linux systems.
This guide covers installing GCC on Ubuntu through the default repository or Ubuntu Toolchain PPA, configuring multiple GCC versions with update-alternatives, and switching between compiler versions for different development needs. You will learn both standard installation for general development and advanced multi-version setups for projects requiring specific compiler releases.
Update Ubuntu Before Installation
Update your system packages to ensure all existing packages are current and avoid version conflicts during installation:
sudo apt update && sudo apt upgrade
The apt update command refreshes the package index with the latest repository information, while apt upgrade installs pending updates for currently installed packages.
Choose Your GCC Installation Method
Ubuntu provides three paths for installing GCC, each serving different development scenarios. These options balance stability, version recency, and maintenance effort based on your project requirements.
| Installation Method | Channel | GCC Versions Available | Best For |
|---|---|---|---|
| Ubuntu Default Repository | Ubuntu Packages | Multiple versions tested with your Ubuntu release | General development, production builds, systems requiring long-term stability |
| Ubuntu Toolchain PPA | Launchpad PPA | Test builds and updates (22.04/24.04 only) | Developers needing latest patches before official release |
| Compile from Source | GNU Releases | Latest upstream GCC (currently 15.2.0) | Bleeding-edge features, optimization testing, GCC development |
For most users, the default repository method is recommended because it provides compiler versions thoroughly tested against Ubuntu’s system libraries, ensuring maximum compatibility and minimizing unexpected build failures. The default repositories already include multiple GCC versions, so most developers do not need the Toolchain PPA. Source compilation delivers bleeding-edge upstream releases but requires significant time and manual maintenance.
This guide covers Ubuntu 26.04 LTS, 24.04 LTS, and 22.04 LTS. GCC version availability varies by release: Ubuntu 26.04 provides GCC 11 through 15, Ubuntu 24.04 provides GCC 9 through 14, and Ubuntu 22.04 provides GCC 9 through 12. The Ubuntu Toolchain PPA does not currently support Ubuntu 26.04 (Resolute). Commands work identically across all supported LTS releases unless noted otherwise.
Method 1: Install GCC with the Ubuntu Repository
The Ubuntu default repository provides the most straightforward GCC installation path. This method installs the compiler version officially supported by your Ubuntu release, thoroughly tested against system libraries for maximum compatibility.
Install the 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 Your Installation
Confirm the compiler installed correctly and check which version you received:
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 filesystem path where the GCC binary resides (typically /usr/bin/gcc), helping verify the system uses the correct compiler when multiple versions exist.
Ubuntu 26.04 LTS output:
gcc (Ubuntu 15.2.0-11ubuntu1) 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
Ubuntu 24.04 LTS output:
gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0 Copyright (C) 2023 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
Ubuntu 22.04 LTS output:
gcc (Ubuntu 11.4.0-1ubuntu1~22.04.2) 11.4.0 Copyright (C) 2021 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 includes Ubuntu’s package identifier (e.g., 6ubuntu2~24.04), indicating this is Ubuntu’s packaged version built specifically for your release.
Test Your Installation
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
Method 2: Install Multiple GCC Versions
Ubuntu’s default repositories include multiple GCC versions beyond the system default. You can install older or newer compiler releases without adding external PPAs, useful for testing code compatibility across compiler versions or building projects that require specific GCC releases.
Ubuntu 26.04 LTS (GCC 11-15 Available)
Ubuntu 26.04 ships GCC 15 as the default compiler. The default repositories provide GCC 11 through 15, so the Toolchain PPA is unnecessary for this release.
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
Each command installs both the C compiler (gcc-NN) and the C++ compiler (g++-NN) for that version. Verify installation by running gcc-15 --version or the equivalent for your chosen version.
Ubuntu 24.04 LTS (GCC 9-14 Available)
Ubuntu 24.04 ships GCC 13 as the default. The default repositories provide GCC 9 through 14, with GCC 14 available from the Universe repository.
If
apt search gcc-14shows no results, enable the Universe repository withsudo add-apt-repository universe && sudo apt updatebefore installing GCC 14.
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 9-12 Available)
Ubuntu 22.04 ships GCC 11 as the default. The default repositories provide GCC 9 through 12.
GCC 13, 14, and 15 are not available for Ubuntu 22.04 through the default repositories or the Toolchain PPA. If you need these versions on Ubuntu 22.04, compile from source (Method 3) or upgrade to Ubuntu 24.04 or newer.
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 Toolchain PPA (Optional)
The Ubuntu Toolchain PPA provides test builds and updates for developers who want the latest patches before they reach official repositories. For most users, the default Ubuntu repositories already include multiple GCC versions, making the PPA unnecessary.
The Toolchain PPA currently supports Ubuntu 24.04 (Noble) and Ubuntu 22.04 (Jammy). It does not support Ubuntu 26.04 (Resolute). Attempting to add the PPA on Ubuntu 26.04 fails with “Release file not found” errors.
Add the PPA only if you need specific test builds not yet in the default repos. If add-apt-repository is missing on minimal images, install it first with sudo apt install software-properties-common:
sudo add-apt-repository ppa:ubuntu-toolchain-r/ppa -y
sudo apt update
The -y flag automatically confirms the PPA addition without prompting. 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.
Method 3: Compile GCC from Source
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 typically consume 30-60 minutes on modern multi-core systems and require 4-6 GB 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 Build Dependencies
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. Use this command to automatically detect and download the latest stable release:
GCC_VERSION=$(curl -sL https://ftp.gnu.org/gnu/gcc/ | grep -oP 'gcc-\d+\.\d+\.\d+' | sort -V | tail -1 | sed 's/gcc-//')
echo "Latest GCC version: $GCC_VERSION"
wget "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}"
Alternatively, specify a version manually (this example uses GCC 15.2.0, the latest stable release as of this writing):
wget https://ftp.gnu.org/gnu/gcc/gcc-15.2.0/gcc-15.2.0.tar.xz
tar -xf gcc-15.2.0.tar.xz
cd gcc-15.2.0
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
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-15sets the installation directory, keeping it separate from system packages--disable-multilibskips 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; addfortran,go, oradaif your projects require them
The configure script analyzes your system, checks dependencies, and generates Makefiles. This step 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
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. Expect this step to take 30-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.
Install the compiled binaries, libraries, and documentation:
sudo make install
This copies the compiled compiler to /usr/local/gcc-15/. The installation step typically takes 2-3 minutes.
Register the Source-Built 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 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:
echo "/usr/local/gcc-15/lib64" | sudo tee /etc/ld.so.conf.d/gcc-15.conf
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
You should see your source-built library listed alongside 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. 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.
Register Multiple GCC Versions
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-15):
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
The --slave options ensure g++ and gcov versions automatically track the selected gcc version, maintaining consistency across the entire toolchain.
Switch Between GCC Versions
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 Common GCC Installation Issues
PPA Repository Errors
If you encounter “Unable to locate package” errors after adding the Toolchain PPA, ensure you ran sudo apt update to refresh the package index:
sudo apt update
apt search gcc-14
If no results appear, verify your Ubuntu version is supported by the PPA. The Toolchain PPA supports Ubuntu 24.04 and 22.04 but does not support Ubuntu 26.04. Check the PPA package listings filtered by release to confirm availability.
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 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
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.
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
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.
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. Refresh your package index and explicitly install both compilers:
sudo apt update
sudo apt install gcc-14 g++-14
Verify you received the correct compiler:
gcc-14 --version
The output should show gcc in the version string, not clang. If you still see clang, verify the package with apt show gcc-14 to confirm it points to the correct GCC package.
Runtime Library Compatibility Issues
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.6from 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.
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
Remove the build-essential metapackage and any specific GCC versions you installed. Adjust the package list to match your installed versions:
sudo apt remove build-essential gcc-11 g++-11 gcc-12 g++-12 gcc-13 g++-13 gcc-14 g++-14
sudo apt autoremove
The autoremove command cleans up orphaned dependencies that were installed automatically.
Remove the Toolchain PPA
If you added the Ubuntu Toolchain PPA, remove it along with its configuration:
sudo add-apt-repository --remove ppa:ubuntu-toolchain-r/ppa -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
The following commands permanently delete the source-compiled GCC installation. 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
Conclusion
Your Ubuntu system now has GCC installed with the ability to switch between multiple compiler versions as your projects require. Whether you chose the stable repository version for production builds, additional versions from the default repositories for testing, or source compilation for the latest upstream features, the update-alternatives system provides seamless version management. With GCC configured, you can proceed to install CMake on Ubuntu to set up a complete cross-platform build environment for C and C++ projects, or explore Python virtual environments if your development workflow includes Python extension modules that require compilation.
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!
Thanks for reporting this, Swamy. The error means
cppis already registered as its own master alternative on your system, so it cannot also be a slave ofgccin the same group. At the same time,gcchas no alternatives configured yet, which is why you see “no alternatives for gcc”.You have two options:
1. Keep cpp independent (simpler): Drop
cppfrom the slave list and only tieg++andgcovtogcc:In most cases this is enough, because
gccwill invoke the correct preprocessor internally.2. Make cpp a slave of gcc (matches the guide exactly): First remove the standalone
cppalternative, then re-run the full command from the article sogcc,g++,cpp, andgcovmove together:Either approach will clear the error; the first keeps
cppindependent, while the second matches the multi-version setup described in the guide.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!
facing the same problem
E: Unable to locate package g++-13
E: Unable to locate package gcc-13
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:
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:
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.
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).
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.
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
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:
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:
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.