How to Install GCC Compiler (build-essential) on Ubuntu 22.04 LTS

GCC, better known as The GNU Compiler Collection, is a set of compilers development tools. Front ends such as C, C++, Objective-C, Fortran, Ada, Go, and D. GCC is open-source and is widely used as it was the original compiler for GNU and is currently used to compile the Linux Kernel along with many other projects.

In the following tutorial, you will learn how to install GCC Compiler on Ubuntu 22.04 LTS desktop or server using the default APT repository maintained by Ubuntu or the PPA toolchain for the latest bleeding-edge or alternative backport versions.

Update Ubuntu

First, update your system to ensure all existing packages are up to date to avoid conflicts.

sudo apt update && sudo apt upgrade -y

Install GCC Compiler – APT Method (build-essential)

The first recommended option to install GCC is to install the build-essential package containing GCC and many other application packages such as make.

To begin the installation, use the following command.

sudo apt install build-essential -y

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

gcc --version

Example output:

How to Install GCC Compiler (build-essential) on Ubuntu 22.04 LTS

Install GCC Compiler Bleeding Edge – APT PPA Method

The second option is to install the Toolchain PPA, which contains the latest versions of GCC.

To import the PPA, use the following command.

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

For users that have installed the build-essential package, you can run an update and upgrade.

sudo apt update && sudo apt upgrade

For users who need to install for the first time, run the same command as option 1.

sudo apt install build-essential -y

Install & Configure Multiple Versions

For developers and specific users, you may require multiple versions installed of GCC.

First, install the versions you require, or install four versions of GCC along with G++.

Example:

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

Once installed, you can use the update-alternatives command to configure the priority of each version. The default version used on your system will be the highest priority.

Below is an example of setting all 4 with a priority split, with GCC-11 being the highest set at priority 100.

EXAMPLE ONLY:

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

Confirm GCC-11 is now the default version of your system.

gcc --version

Example output:

How to Install GCC Compiler (build-essential) on Ubuntu 22.04 LTS

The default GCC version set on your system can be re-configured. Use the following command first to list the priority.

sudo update-alternatives --config gcc

Example output:

How to Install GCC Compiler (build-essential) on Ubuntu 22.04 LTS

As above, you need to enter the number of the version corresponding to the list you would like to switch to.

For example, switching from GCC-11 to GCC-10, type 1 and press the ENTER KEY.

Confirm the version switch has occurred successfully using the –version command.

gcc --version

Example output:

How to Install GCC Compiler (build-essential) on Ubuntu 22.04 LTS

Compile a Hello World Example

Test compiling with GCC, create the famous “Hello World” in a C program using any text editor, the tutorial will use nano.

nano hello.c

Next, add the following.

#include <stdio.h>
int main()
{
   printf("Hello, World from Linuxcapable.com!");
   return 0;
}

Save the file, CTRL+O, then exit CTRL+X.

Compile the Hello World test using the following command.

gcc hello.c -o hello

Next, run the hello compiled program.

./hello

Example output:

Hello, World from Linuxcapable.com!

A handy package to install is the manual pages (man page). This will help you with using GNU by providing documentation on your system.

Use the following command to install the manpages-dev package. By default, this should be installed on most Ubuntu systems.

sudo apt-get install manpages-dev

Once installed, use the following command to bring up the options.

man -h

Comments and Conclusion

In the tutorial, you have learned how to install GCC Compiler, along with a PPA option for newer GCC alternatives and how to switch between versions on Ubuntu 22.04 LTS Jammy Jellyfish.

For further reading and information, visit the GCC official documentation.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!