Provides all the steps required to install the GCC Compiler on Ubuntu 18.04 LTS and getting started with C/C++ using Hello World.

How To Install GCC On Ubuntu 18.04 LTS
How To Install GCC On Ubuntu 18.04 LTS
September 13, 2019

C++ is one of popular programming language and the applications developed using it are very fast as compared to the modern programming languages. The GNU Compiler Collection i.e. GCC is the de-facto compiler used to compile and build the C/C++ programs and applications on Linux system. GCC is a collection of compilers and libraries used the develop applications using C, C++, Objective-C, Fortran, Ada, Go, and D programming languages. This tutorial provides all the steps required to install GCC on the popular Linux distribution Ubuntu. It provides steps required for Ubuntu 18.04 LTS, though the steps should be similar for other distributions of Linux.

Step 1 - Install GCC

In this step, we will install the default GCC available on the official repositories of Ubuntu. Install the default GCC using the commands as shown below.

#Refresh the Packages Index
sudo apt update

# Install build-essential to install gcc, g++, and make
sudo apt install build-essential

# Install manual pages
sudo apt-get install manpages-dev

# Verify the installation
gcc --version

# The GCC version from Ubuntu
gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 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.

This is how we can install and verify the GCC on Ubuntu.

Step 2 - Install Multiple GCC

In the previous step, we have installed the default GCC i.e. GCC 7.4.0 which is not the latest one. In this step, we will install the latest GCC using the PPP. The most recent version of GCC available while writing this tutorial is 9.2.0. Use the below-mentioned commands to add the PPA and install the most recent version of GCC.

# Prerequisites
sudo apt install software-properties-common

# Add the PPA
sudo add-apt-repository ppa:ubuntu-toolchain-r/test

It will take some time to add the repository. Once it gets ready, it will ask to press Enter Key to continue adding it. It might fail to retrieve the gpg key. In such a case, we might need to add the PPA manually using the commands as mentioned below.

Make sure to have system backup before executing below-mentioned commands since I got my system crashed for the first time when I attempted to execute these commands. You may also continue with next steps to start programming with C/C++ using the default GCC installed in Step 1.

# Edit sources
sudo nano /etc/apt/sources.list.d/toolchain.list

# Update the content
deb http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu precise main
deb-src http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu precise main

# Fix key error
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1E9377A2BA9EF27F

# Refresh the packages list
sudo apt update

# Dist upgrade if not done recently
sudo apt dist-upgrade

Now install the most recent version of GCC using the commands as mentioned below.

# Install GCC 9
sudo apt-get install gcc-9 g++-9

You can also install the older version of GCC using the commands as mentioned below.

# Install GCC 8
sudo apt-get install gcc-8 g++-8

This is how we can install multiple versions of GCC on the same system. We can use the below-mentioned commands to switch among the installed GCC.

# GCC 9 alternative
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

# GCC 8 alternative
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

# Switch active GCC
sudo update-alternatives --config gcc

The output of the above-mentioned commands is as shown in Fig 1.

GCC - Multiple Versions

Fig 1

Step 3 - Getting started with C++ - Hello World

Create a file named hello.cpp and update the program as shown below.

#include<iostream>

using namespace std;

int main() {

cout << "Hello World\n";

return 0;
}

Now use the below-mentioned commands to compile, build, and execute the program.

# Compile and build the program
g++ hello.cpp -o hello

# Execute the program
./hello

# Output
Hello World

These commands will compile, make, and execute the program to print Hello World on the console.

Step 4 - Getting started with C - Hello World

Create a file named hello.c and update the program as shown below.

#include<stdio.h>

int main() {

printf( "Hello World\n" );

return 0;
}

Now use the below-mentioned commands to compile, build, and execute the program.

# Compile and build the program
gcc hello.c -o hello

# Execute the program
./hello

# Output
Hello World

These commands will compile, make, and execute the program to print Hello World on the console.

This is how we can install the default or latest GCC on Ubuntu and compile and execute the programs written in C or C++.

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS