How to Install GCC Compiler on Ubuntu 22.04 LTS

GCC, better known as The GNU Compiler Collection, is a set of compilers and 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 on Ubuntu 22.04 LTS Jammy Jellyfish desktop or headless server using the command line terminal with alternative installation options and handy hints to switch between GCC Compiler versions.

Update 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 -y

Install Build-Essential – APT Method with Ubuntu Repository

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 on Ubuntu 22.04 LTS

Install Latest Build-Essential – APT with Toolchain PPA

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 of GCC Compiler

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

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

Example:

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

Once installed, you can use the update-alternatives command to configure the priority of each version. The following command example below shows the alternatives command install setting for GCC 9, GCC 10, GCC 11, and the latest GCC 12 with a priority split.

The tutorial will make the latest edition with GCC-12 being the highest set at priority 100, but you can configure this however you prefer; this is an idea on how to set priorities.

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 80 --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 40 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9

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

gcc --version

Example output:

How to Install GCC Compiler 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 you originally stated above.

sudo update-alternatives --config gcc

Example output:

How to Install GCC Compiler 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-12 to GCC-11, TYPE 2 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 on Ubuntu 22.04 LTS

As above, you switched versions successfully.

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:

How to Install GCC Compiler on Ubuntu 22.04 LTS

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, a PPA option for newer GCC alternatives, and 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!