How to Install GCC on Ubuntu 24.04, 22.04, or 20.04

The GNU Compiler Collection (GCC) is a cornerstone in software development, equipping programmers with a robust suite of tools to compile various programming languages. Its significance spans various aspects of development, from enabling the creation of high-performance applications to supporting a multitude of programming paradigms.

Below are key highlights that underscore the utility and versatility of GCC:

  • Comprehensive Language Support: In addition to C and C++, GCC extends its capabilities to include Objective-C, Fortran, Ada, Go, and more, facilitating a diverse development ecosystem.
  • Advanced Optimization Techniques: GCC implements cutting-edge optimization strategies for developing efficient, high-speed applications.
  • Cross-Platform Compatibility: Its support for cross-compiling enables developers to build applications for different platforms and architectures seamlessly.
  • Robust Debugging Tools: GCC has extensive debugging features, allowing for detailed examination and troubleshooting of code.
  • Open Source Advantage: As an open-source project, GCC benefits from continuous updates and improvements by a global community of developers.
  • Extensible Architecture: Its modular design allows adding new languages and features, ensuring that GCC remains at the forefront of technology.
  • Compliance with Standards: GCC strives to adhere closely to industry standards for C, C++, and other languages, promoting code portability and compatibility.
  • Community and Documentation Support: A strong community and comprehensive documentation assist developers in navigating challenges and leveraging GCC’s full potential.

As we delve into the technical specifics of installing GCC on Ubuntu, we must appreciate the blend of traditional and cutting-edge features that make GCC an indispensable tool for developers. Now, let’s dive into the installation process.

Prerequisites for Installing GCC

To successfully install GCC on Ubuntu, ensure you meet the following requirements:

Minimum Hardware and Supported Ubuntu Versions

ComponentRequirement
Processor2 gigahertz (GHz) or faster dual-core processor (based on Ubuntu’s minimum requirements)
RAM4 GB or more (based on Ubuntu’s minimum requirements)
Disk Space500 MB of free space
Supported VersionsUbuntu 24.04 (Noble), Ubuntu 22.04 (Jammy), Ubuntu 20.04 (Focal)

Network and Terminal Requirements

RequirementDescription
Internet ConnectionRequired to download GCC and updates.
Terminal ConventionsAll terminal commands should be run as a regular user with sudo privileges.
CLI CommandsUse the Command Line Interface (CLI) for installation and setup.

Install GCC via Default Repository or Ubuntu ToolChain PPA

Update Ubuntu before GCC Installation on Ubuntu

Before you begin, update your system to ensure all existing packages are up to date to avoid any conflicts during the installation.

sudo apt update
sudo apt upgrade

Select GCC Installation Method

Method 1: Install GCC with the Ubuntu Repository

The first recommended option to install GCC is to install either the GCC package directly or the build-essential package containing GCC and many other essential development tools such as make, g++, and dpkg-dev.

To begin the installation, use the following command.

sudo apt install gcc

Or

sudo apt install build-essential

Once installed, verify the installation and check the version using the following command.

gcc --version

Method 2: Install GCC via Toolchain PPA

The following method will install the latest GCC Compiler or alternative versions you may seek from the Ubuntu Toolchain PPA.

To import this PPA, run the following command:

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

After importing the PPA, update your Ubuntu sources list to reflect the changes made by running the following command in your terminal:

sudo apt update

To install a specific version of the GCC compiler on your Ubuntu system using the Ubuntu ToolChain PPA, use the following commands in your terminal:

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

After running the appropriate command for the version you want to install, the GCC compiler will be successfully installed on your Ubuntu system.

Note: The latest GCC release is typically released on the latest LTS releases; older releases such as 20.04 and 18.04 may not be compatible with GCC 14; for example, check the PPA to ensure compatibility.

Configure Alternative Versions of GCC

You may need to install multiple GCC compiler versions as a developer or specific user. Follow these steps to configure alternative versions of GCC on your Ubuntu system.

First, install the versions of GCC you need. You can install multiple versions of GCC along with G++ using the following command:

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

Note: This is an example command and should be modified to suit your requirements.

Once you have installed the necessary versions, use the update-alternatives command to configure each version’s priority. The following example command sets the priority split between GCC 9, GCC 10, GCC 11, GCC 12, GCC 13, and the latest GCC 14.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 90 --slave /usr/bin/g++ g++ /usr/bin/g++-14 --slave /usr/bin/gcov gcov /usr/bin/gcov-13

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 80 --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 100 --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 70 --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 60 --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 50 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9

The above commands set GCC 12 as the highest priority with a value of 100. However, you can configure the priorities based on your preferences, such as if you need to work with the latest releases, GCC 13 or 14.

To confirm that GCC 12 is the default version on your system, run the following command:

gcc --version

Example output:

You can reconfigure the default GCC version on your system by using the update-alternatives command. First, use the following command to list the priorities you previously set:

sudo update-alternatives --config gcc

Example output:

This command will display a list of installed GCC versions and their priorities. You can then select the default version by entering the corresponding number.

That’s it! You have successfully configured alternative versions of GCC on your Ubuntu system.

Conclusion

Once installed, GCC can compile and run C and C++ programs on your Ubuntu system. With the addition of the manual pages package, you can also access comprehensive documentation on how to use GCC and its various features. Whether a novice or an experienced developer, having GCC installed on your Ubuntu system is essential for developing and running C and C++ programs.

Useful Links

Here are some valuable links related to using GCC:

  • GCC Official Website: Visit the official GCC website for information about the GNU Compiler Collection, its features, and the latest updates.
  • GCC Online Documentation: Access the comprehensive online documentation for detailed guides on using and configuring GCC.
  • GCC FAQ: Browse the frequently asked questions to find answers to common queries about GCC.
  • GCC Wiki: Explore the GCC wiki for additional information, tutorials, and community-contributed content.
  • GCC Readings: Discover a collection of readings and resources to deepen your understanding of GCC and its components.
  • Ubuntu Toolchain PPA: Access the Ubuntu Toolchain PPA to download and install the latest versions of GCC for Ubuntu.

1 thought on “How to Install GCC on Ubuntu 24.04, 22.04, or 20.04”

  1. 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

Leave a Comment