How to Install GCC on Ubuntu 21.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 your 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... Done
Reading state information... Done
The following additional packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu cpp cpp-10 dpkg-dev fakeroot fontconfig-config
  fonts-dejavu-core g++ g++-10 gcc gcc-10 gcc-10-base libalgorithm-diff-perl libalgorithm-diff-xs-perl
  libalgorithm-merge-perl libasan6 libatomic1 libbinutils libc-dev-bin libc-devtools libc6-dev libcc1-0
  libcrypt-dev libctf-nobfd0 libctf0 libdeflate0 libdpkg-perl libfakeroot libfile-fcntllock-perl libfontconfig1
  libgcc-10-dev libgd3 libgomp1 libisl23 libitm1 libjbig0 libjpeg-turbo8 libjpeg8 liblsan0 libmpc3 libnsl-dev
  libquadmath0 libstdc++-10-dev libtiff5 libtirpc-dev libtsan0 libubsan1 libwebp6 libxpm4 linux-libc-dev
  lto-disabled-list make manpages-dev rpcsvc-proto
Suggested packages:
  binutils-doc cpp-doc gcc-10-locales debian-keyring g++-multilib g++-10-multilib gcc-10-doc gcc-multilib
  autoconf automake libtool flex bison gdb gcc-doc gcc-10-multilib glibc-doc bzr libgd-tools libstdc++-10-doc
  make-doc
The following NEW packages will be installed:
  binutils binutils-common binutils-x86-64-linux-gnu build-essential cpp cpp-10 dpkg-dev fakeroot
  fontconfig-config fonts-dejavu-core g++ g++-10 gcc gcc-10 gcc-10-base libalgorithm-diff-perl
  libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan6 libatomic1 libbinutils libc-dev-bin libc-devtools
  libc6-dev libcc1-0 libcrypt-dev libctf-nobfd0 libctf0 libdeflate0 libdpkg-perl libfakeroot
  libfile-fcntllock-perl libfontconfig1 libgcc-10-dev libgd3 libgomp1 libisl23 libitm1 libjbig0 libjpeg-turbo8
  libjpeg8 liblsan0 libmpc3 libnsl-dev libquadmath0 libstdc++-10-dev libtiff5 libtirpc-dev libtsan0 libubsan1
  libwebp6 libxpm4 linux-libc-dev lto-disabled-list make manpages-dev rpcsvc-proto
0 upgraded, 57 newly installed, 0 to remove and 0 not upgraded.
Need to get 59.5 MB of archives.
After this operation, 223 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

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 10.3.0-1ubuntu1) 10.3.0
Copyright (C) 2020 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 in C program and open hello.c 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 21.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.