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

Choose a different version or distribution

Introduction

Before we begin talking about how to install GCC Compiler on Ubuntu 20.04. Let’s briefly understand – What is a GCC Compiler?

The GNU Compiler Collection is mainly a collection of compilers as well as of libraries for C, C++, and Objective-C. There are many open-source projects, like GNU tools and the Linux kernel, that are compiled with GCC.

In this tutorial, you will install the GCC Compiler on Ubuntu 20.04.

GCC Compiler Features

  1. Cross-platform support: GCC supports a wide range of operating systems, including Linux, macOS, Windows, and others. This allows developers to write code for one platform and compile it for another.
  2. Optimization: GCC provides a variety of optimization options that help developers to generate highly optimized code for their target platform. These options can significantly improve the performance of the compiled code.
  3. Language support: GCC supports a variety of programming languages, including C, C++, Objective-C, Fortran, Ada, and others. This makes it a versatile tool for developers who work with different programming languages.
  4. Standard compliance: GCC complies with various industry standards, such as the C11 and C++17 standards. This ensures that the code generated by GCC is compatible with other compilers that also comply with these standards.
  5. Open-source: GCC is an open-source tool, which means that developers can access the source code, modify it, and redistribute it as per their requirements.
  6. Debugging support: GCC provides a range of debugging options, such as generating debug symbols, supporting breakpoints, and generating core dumps. These options help developers to easily debug their code.
  7. Extensibility: GCC can be extended using plugins and custom front-ends, allowing developers to customize the compiler to meet their specific requirements.

What does the build-essential Package do?

The build-essential meta-package is a package available on Ubuntu that includes five separate packages essential during the software compilation process. It also contains the GCC compiler, which is required to build and run C programs. The five packages included in the build-essential package are as follows:

  1. gcc: The GNU Compiler Collection, which is used to compile programs written in C language.
  2. g++: The GNU C++ compiler, which is used to compile programs written in C++ language.
  3. libc6-dev: This package includes the GNU library files needed to compile simple C and C++ programs, along with header files.
  4. make: This is a useful tool for controlling the compilation of a program. It reads a file called a "makefile" to instruct the compiler on how to complete its job.
  5. dpkg-dev: This package can be used for unzipping, compiling, and uploading Debian source packages. If you plan to package your program for a Debian-based system, this tool will be useful.

Prerequisites to Install GCC Compiler

1) You must log in as root or user with the sudo privileges.

Step 1 – Install GCC Compiler on Ubuntu

1) Here, the default Ubuntu repositories have a meta-package as build-essential. It contains the GCC compiler and many libraries and utilities necessary for compiling the software. Perform the below steps to install GCC Compiler on Ubuntu 20.04.

2) Firstly, start by updating the packages list:

sudo apt update

3) After that, install build-essential package by typing:

sudo apt install build-essential

The command will install a bunch of new packages. It will also include gcc, g++ and make. You may also need to install the manual pages. It is about using GNU/Linux for the development:

sudo apt-get install manpages-dev

4) To validate if the installation of the GCC compiler is successful, you will use gcc --version command. It will print the GCC version:

gcc --version

5) 9.3.0 is a default version of GCC available in Ubuntu 20.04 repositories.

Output

gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
Copyright (C) 2019 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.

GCC compiler installation on your system is now successful. You can now start using it.

Step 2 – Compiling a Hello World Example

1) To Compile a basic C or C++ program, you will do it using GCC. Open your text editor and create the below file:

nano hello.c
#include <stdio.h>
int main()
{
  printf ("Hello World!\n");
  return 0;
}

2) Proceed to save the file. Then, compile it into an executable using the below command:

gcc hello.c -o hello

3) It will create a binary file with a name hello in the same directory. It is where you run the command. Now, execute the hello program, by:

./hello

The command will print:

Output

Hello World!

Step 3 – Install Multiple GCC Versions

1) Here, you will now install and use multiple versions of GCC on Ubuntu 20.04. The latest versions of the GCC compiler have new functions and optimization improvements.

Further, the default Ubuntu repositories have several GCC versions. It is from 7.x.x to 10.x.x. Now, you will install the latest three versions of GCC and G++.

2) Therefore, first, add ubuntu-toolchain-r/test PPA to your system using the following command:

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

3) Next, install the necessary GCC and G++ versions by:

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

4) Then, the command below will configure an alternative for each version and associate a priority with it. The default version is the one with the highest priority. So, here is the gcc-9:

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

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8

5) Later, if you will need to change the default version, you can do it using the update-alternatives command:

sudo update-alternatives --config gcc
Output

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

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-10   100       auto mode
  1            /usr/bin/gcc-10   100       manual mode
  2            /usr/bin/gcc-8    80        manual mode
  3            /usr/bin/gcc-9    90        manual mode

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

6) You will see a list of all installed GCC compiler versions on your Ubuntu system. Then, enter the number of versions you want to use as a default. Proceed to press Enter. The command will create symbolic links which will be to the specific versions of GCC and G++.

FAQs to Install GCC Compiler on Ubuntu

Where is GCC compiler in Ubuntu?

You need to use the which command to locate c compiler binary called gcc. Usually, it is installed in /usr/bin directory.

To find the location of the Linux C Compiler, launch a terminal and then type the following commands:

which g++
which cc
which gcc

How to verify if GCC is installed on my PC?

You can use the following command to verify if GCC is installed on your PC:

gcc -v

If GCC is installed on your computer, a variety of data should be shown, beginning with the phrase Using built-in specs which indicates that GCC is installed on your PC.

Conclusion

We hope this detailed tutorial helped you to install the GCC Compiler on Ubuntu 20.04.

If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.