Linux

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

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

The GNU Compiler Collection (GCC) is a collection of compilers and libraries for the C, C ++, Objective-C, Fortran, Ada, Go, and D. programming languages. Many open source projects, including the Linux kernel and GNU tools, are compiled using GCC.

This article explains how to install GCC on Ubuntu 20.04.

Install GCC on Ubuntu 20.04

The default Ubuntu repository contains a meta-package called “build-essential” which includes a collection of GNU compilers, GNU debuggers, and other development libraries and tools needed to compile software.

To install the Development Tools package, run the following command as root or as a user with sudo rights:
sudo apt update
sudo apt install build-essential

The command installs many packages, including gcc, g ++ and make.

You might also want to install a man page about using GNU / Linux for development:

sudo apt-get install manpages-dev

Verify that the GCC compiler was successfully installed by running the following command that prints the GCC version:

gcc --version

The Ubuntu 20.04 repository provides GCC version 9.3.0:

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.

There she is. GCC tools and libraries have been installed on your Ubuntu system.

Compiling Hello World Examples

Compile basic C or C ++ programs using GCC quite easily. Open your text editor and create the following file:

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

Save the file and compile it into an executable file:

gcc hello.c -o hello

This creates a binary file called hello in the same directory where you run the command.

Run the hello program with:

./hello

The program must print:

Output :

Hello World!

Install Multiple Versions of GCC

This section provides instructions on how to install and use several versions of GCC on Ubuntu 20.04. The latest version of the GCC compiler includes new functions and optimization improvements.

At the time of writing this article, the default Ubuntu repository includes several versions of GCC, from 7.x.x to 10.x.x. At the time of writing, the latest version of GCC is 10.1.0.

In the following example, we will install the three latest versions of GCC and G ++.

Install the desired version of GCC and G ++ by typing:

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

The command below configures alternatives for each version and associates priorities with it. The default version is the one with the highest priority, in our case gcc-10.
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

Later if you want to change the default version use the alternative-update 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:

You will be presented with a list of all versions of GCC installed on your Ubuntu system. Enter the version number that you want to use as the default and press Enter.

This command will make a symbolic link to certain versions of GCC and G ++.

Conclusion

We have shown you how to install GCC on Ubuntu 20.04. You can now visit the official GCC Documentation page and learn how to use GCC and G ++ to compile your C and C ++ programs.

If you experience problems or get feedback, leave a comment below.

Related posts

How to install Zulip chat server on Ubuntu 18.04

Linux

How to Install R on Ubuntu 20.04

Linux

How to Install Atom Text Editor on Ubuntu 20.04

Linux

Using the ‘ldd’ command on Linux

Linux

How to Install Visual Studio Code on Ubuntu 20.04

Linux

How to Install Go on Ubuntu 20.04

Linux

How to Install MariaDB on Ubuntu 20.04

Linux

How to Change the Time Zone in Ubuntu 20.04

Linux

How to use GNU Debugger on Linux

Linux