How to install GCC on Ubuntu 20.04 LTS

GCC (GNU Compiler Collection) — formerly "GNU C Compiler", is a compiler system produced by the GNU Project. The current version is GCC 9.3, released on March 12, 2020, supporting several major programming languages: C, C++, Objective-C, Objective-C++, Fortran, Ada, D, Go, and BRIG (HSAIL). Find more information about GCC in their main website.

To setup GCC on Linux Ubuntu 20.04 LTS, the most convenient way is by installing the entire development package build-essential. We can achieve that by using apt install build-essential command:

$ sudo apt install build-essential
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-9 dpkg-dev fakeroot g++ g++-9 gcc gcc-9 gcc-9-base
  libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan5 libatomic1 libbinutils libc-dev-bin
  libc6-dev libcc1-0 libcrypt-dev libctf-nobfd0 libctf0 libdpkg-perl libfakeroot libfile-fcntllock-perl libgcc-9-dev libgomp1
  libisl22 libitm1 liblsan0 libmpc3 libquadmath0 libstdc++-9-dev libtsan0 libubsan1 linux-libc-dev make manpages-dev
Suggested packages:
  binutils-doc cpp-doc gcc-9-locales debian-keyring g++-multilib g++-9-multilib gcc-9-doc gcc-multilib autoconf automake
  libtool flex bison gdb gcc-doc gcc-9-multilib glibc-doc bzr libstdc++-9-doc make-doc
The following NEW packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu build-essential cpp cpp-9 dpkg-dev fakeroot g++ g++-9 gcc gcc-9
  gcc-9-base libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan5 libatomic1 libbinutils
  libc-dev-bin libc6-dev libcc1-0 libcrypt-dev libctf-nobfd0 libctf0 libdpkg-perl libfakeroot libfile-fcntllock-perl
  libgcc-9-dev libgomp1 libisl22 libitm1 liblsan0 libmpc3 libquadmath0 libstdc++-9-dev libtsan0 libubsan1 linux-libc-dev make
  manpages-dev
0 upgraded, 41 newly installed, 0 to remove and 0 not upgraded.
Need to get 39.9 MB of archives.
After this operation, 175 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://sg.archive.ubuntu.com/ubuntu focal/main amd64 binutils-common amd64 2.34-6ubuntu1 [207 kB]
Get:2 http://sg.archive.ubuntu.com/ubuntu focal/main amd64 libbinutils amd64 2.34-6ubuntu1 [474 kB]
Get:3 http://sg.archive.ubuntu.com/ubuntu focal/main amd64 libctf-nobfd0 amd64 2.34-6ubuntu1 [47.0 kB]
...
... <purposely truncated> ...
...
Get:39 http://sg.archive.ubuntu.com/ubuntu focal/main amd64 libalgorithm-merge-perl all 0.08-3 [12.0 kB]
Get:40 http://sg.archive.ubuntu.com/ubuntu focal/main amd64 libfile-fcntllock-perl amd64 0.22-3build4 [33.1 kB]
Get:41 http://sg.archive.ubuntu.com/ubuntu focal/main amd64 manpages-dev all 5.05-1 [2,266 kB]
Fetched 39.9 MB in 3min 10s (210 kB/s)
Extracting templates from packages: 100%
Selecting previously unselected package binutils-common:amd64.
(Reading database ... 71350 files and directories currently installed.)
Preparing to unpack .../00-binutils-common_2.34-6ubuntu1_amd64.deb ...
Unpacking binutils-common:amd64 (2.34-6ubuntu1) ...
Selecting previously unselected package libbinutils:amd64.
...
... <purposely truncated> ...
...
Selecting previously unselected package manpages-dev.
Preparing to unpack .../40-manpages-dev_5.05-1_all.deb ...
Unpacking manpages-dev (5.05-1) ...
Setting up manpages-dev (5.05-1) ...
Setting up libfile-fcntllock-perl (0.22-3build4) ...
Setting up libalgorithm-diff-perl (1.19.03-2) ...
...
... <purposely truncated> ...
...
Setting up gcc (4:9.3.0-1ubuntu2) ...
Setting up g++-9 (9.3.0-10ubuntu2) ...
Setting up g++ (4:9.3.0-1ubuntu2) ...
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode
Setting up build-essential (12.8ubuntu1) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...

After installation completed, you can check GCC version:

$ gcc --version
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.

To test the C compiler, let's create a small program like below:

greetings.c
#include <stdio.h>
#include <time.h>

void main()
{
   char name[50];
   time_t rawtime;
   struct tm * timeinfo;

   printf("Your name, please! > ");
   scanf("%s", name);

   time ( &rawtime );
   timeinfo = localtime ( &rawtime );
   printf("Greetings %s, the time now is %s", name, asctime (timeinfo));
}
                    

Save it as greetings.c and compile it using following command:

$ gcc -o greetings greetings.c

Option -o file wile write the output in a file as we name it. Although this is optional, it's best to define your output file. If not, gcc will output to "default" filename like a.out. After we got our binary, we can run it. Here the output for my program:

$ ./greetings
Your name, please! > Dariawan
Greetings Dariawan, the time now is Fri May  1 09:28:53 2020