Cyberithub

How to Install GCC(C and C++ Compiler) on Ubuntu 20.04 LTS

Advertisements

In this article, we will see the steps to install GCC on Ubuntu 20.04 LTS. GCC or GNU Compiler Collection is a free and open source set of mostly C and C++ Compilers which are used to compile C and C++ based programs. It is widely used by programmers and developers across the world to compile their complex programs using different features and options provided by the gcc. Furthermore, it can also compile programs written in Fortran, Ada, GO and D programming language. It is very easy to install in almost all the famous linux distributions. Here we will see the steps to install GCC on Ubuntu 20.04 LTS based Systems.

 

How to Install GCC(C and C++ Compiler) on Ubuntu 20.04 LTS 2

How to Install GCC(C and C++ Compiler) on Ubuntu 20.04 LTS

Also Read: How to Install Gawk on Ubuntu 20.04 LTS (Focal Fossa)

Step 1: Prerequisites

a) You should have a running Ubuntu 20.04 LTS Server.

b) You should have sudo or root access to run privileged commands.

c) You should have apt or apt-get utility available in your Server.

 

Step 2: Update Your Server

Before going through the steps to install gcc on Ubuntu, it is highly recommended to update and upgrade all the installed packages to the latest version using sudo apt update && sudo apt upgrade command as shown below.

cyberithub@ubuntu:~$ sudo apt update && sudo apt upgrade
[sudo] password for cyberithub:
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Hit:2 http://in.archive.ubuntu.com/ubuntu focal InRelease
Get:3 https://dl.google.com/linux/chrome/deb stable InRelease [1,811 B]
Get:4 http://in.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:5 https://dl.google.com/linux/chrome/deb stable/main amd64 Packages [1,075 B]
Get:6 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1,950 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [2,335 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu focal-updates/main i386 Packages [775 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [403 kB]
Get:11 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [274 kB]
Get:12 http://in.archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [16.2 kB]
Get:13 http://in.archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [1,565 kB]
..........................................................

 

Step 3: Install GCC

In the next step, you can install gcc by using sudo apt install gcc command as shown below. This will download and install the package along with all its dependencies from Ubuntu repo.

cyberithub@ubuntu:~$ sudo apt install gcc
[sudo] password for cyberithub:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libfwupdplugin1 libllvm11 libxmlb1
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
binutils binutils-common binutils-x86-64-linux-gnu gcc-9 libasan5 libbinutils libc-dev-bin libc6-dev libcrypt-dev libctf-nobfd0 libctf0 libgcc-9-dev
libitm1 liblsan0 libquadmath0 libtsan0 libubsan1 linux-libc-dev manpages-dev
Suggested packages:
binutils-doc gcc-multilib make autoconf automake libtool flex bison gcc-doc gcc-9-multilib gcc-9-doc gcc-9-locales glibc-doc
The following NEW packages will be installed:
binutils binutils-common binutils-x86-64-linux-gnu gcc gcc-9 libasan5 libbinutils libc-dev-bin libc6-dev libcrypt-dev libctf-nobfd0 libctf0 libgcc-9-dev
libitm1 liblsan0 libquadmath0 libtsan0 libubsan1 linux-libc-dev manpages-dev
0 upgraded, 20 newly installed, 0 to remove and 0 not upgraded.
Need to get 25.7 MB of archives.
After this operation, 120 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
..........................................................

 

Step 4: Verify Installation

After successful installation, you can verify the installed files path by using dpkg -L gcc command as shown below. You can check more about dpkg command on 21+ Practical dpkg Command Examples for Linux Beginners article.

cyberithub@ubuntu:~$ dpkg -L gcc
/.
/usr
/usr/bin
/usr/bin/c89-gcc
/usr/bin/c99-gcc
/usr/lib
/usr/lib/bfd-plugins
/usr/share
/usr/share/doc
/usr/share/doc/cpp
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/c89-gcc.1.gz
/usr/share/man/man1/c99-gcc.1.gz
/usr/bin/gcc
/usr/bin/gcc-ar
/usr/bin/gcc-nm
/usr/bin/gcc-ranlib
/usr/bin/gcov
/usr/bin/gcov-dump
.......................................

 

Step 5: Check Version

You can also check the current installed version by using gcc --version command as shown below.

cyberithub@ubuntu:~$ gcc --version
gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.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.

 

Step 6: Write a Sample Program

Now that gcc compiler is installed, it is time to write a sample C program to test the compiler. Here we are using below simple program to show a quick demo. This program will simply display a message Hi, This is from CyberITHub !! on the output.

cyberithub@ubuntu:~$ nano example.c
#include <stdio.h>

int main()
{
  printf("Hi, This is from CyberITHub !!\n");
}

To compile the above program, you can use gcc -o example example.c command as shown below.

cyberithub@ubuntu:~$ gcc -o example example.c

Then you can run the program by using ./example as shown below.

cyberithub@ubuntu:~$ ./example
Hi, This is from CyberITHub !!

Leave a Comment