How to Install GCC on Ubuntu 20.04

GCC, the GNU Compiler Collection is a compiler system developed to support various programming languages. GCC is standard compiler for most projects related to GNU and Linux, including the Linux kernel.

Installing GCC on Ubuntu

Ubuntu repositories contain build-essential package which contains the GCC compiler, g++ and make

Update an existing system by running follwing command

apt upgrade

apt install build-essential

Output:

root@vps:~# 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

If you want to install the manual page for GCC, run the below command,

apt-get install manpages-dev

After installing, to verify that GCC is successfully installed by checking gcc version,

gcc --version

Output:

root@vps:~# gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 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.

Compiling a Hello World Example

Create a basic C code source, eg: let's create hello world C program and open hello.c text file,

nano hello.c

Add the following code to hello.c file

// hello.c
#include <stdio.h>

int main() {
        printf("Hello, world!\n");
        return 0;
}

compile it into an executable and execute the hello program by running following commands,

gcc hello.c -o hello

./hello

Output:

root@vps:~# ./hello
Hello, world!
root@vps:~#

Installing Multiple GCC Versions

Install and use multiple versions of GCC on Ubuntu 20.04,

Install the desired GCC and G++ versions by typing below command,

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

Now you have multiple versions of the GCC compiler installed, there’s a priority number that gets associated with each. The version with the highest priority will be the default system compiler.

You will have to run the commands below to set the default system compiler associating each with a priority.

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
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
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 update-alternatives command

update-alternatives --config gcc

Output:

root@vps:~# update-alternatives --config gcc
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 installed GCC versions on your Ubuntu system, Select your choice and press Enter.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS