Feb 13, 2023 5 min read

How to Install GCC Compiler (build-essential) on Ubuntu 18.04

Installing GCC Compiler on Ubuntu 18.04 is easier than ever! Get all the details with this complete guide, including instructions and tips from experts.

Install GCC Compiler on Ubuntu 18.04
Table of Contents

Choose a different version or distribution

Introduction

Before we start discussing how to install the GNU Compiler Collection (GCC) on Ubuntu 18.04, let's quickly understand what it is.

GCC is a collection of compilers and libraries for C, C++, and Objective-C. It is used to compile many open-source projects, such as GNU tools and the Linux kernel.

In this tutorial, we will show you how to install GCC on Ubuntu 18.04. We will also answer some frequently asked questions about GCC.

Top 5 benefits of GCC Compiler

  1. Free and Open-Source: GCC is a free and open-source compiler that is available to everyone. This makes it a cost-effective option for individuals and organizations, who don't have to pay for a proprietary compiler.
  2. Cross-Platform Compatibility: GCC is compatible with various platforms, including Linux, Unix, MacOS, and Windows. This allows developers to write code and test it on multiple platforms with the same compiler.
  3. High Performance: GCC has a high performance and is capable of compiling large projects quickly and efficiently. This makes it ideal for large software development projects.
  4. Widely Adopted: GCC is widely adopted and is used by many organizations and individuals worldwide. This makes it a well-supported and reliable compiler with a large community of users and developers.
  5. Supports Various Programming Languages: GCC supports various programming languages, including C, C++, Fortran, Ada, and others. This makes it a versatile compiler that can be used for a wide range of programming projects and applications.

Prerequisites

1) You must log in as root or user with the sudo privileges.

Step 1 – Install GCC on Ubuntu

1) On Ubuntu 18.04, the default repositories include a meta-package called build-essential, which contains the GCC compiler and many other libraries and utilities necessary for compiling software. To install GCC Compiler, follow these steps:

2) Firstly, update the package list:

sudo apt update

3) After that, install build-essential package by typing:

sudo apt install build-essential

The command will install several packages, including gcc, g++, and make. You may also need to install manual pages. This is all in the context of developing with GNU/Linux.

sudo apt-get install manpages-dev

4) To validate the success of the GCC compiler installation, use the gcc --version command. This will display the GCC version.

gcc --version

5) GCC 7.4.0 is the default version available in Ubuntu 18.04 repositories.

Output

gcc (Ubuntu 7.4.0-1ubuntu1~18.04) 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.

GCC installation on your system is now complete. You can begin using it right away.

Step 2 – Compiling a Hello World Example

1) To compile a basic C or C++ program, you can use the GCC. Open your text editor and create the following file:

nano hello.c
#include <stdio.h>
int main()
{
  printf ("Hello World!\n");
  return 0;
}

2) Save the file. Then, compile it into an executable with this command:

gcc hello.c -o hello

3) The command will create a binary file named hello in the same directory. To execute the hello program, do the following:

./hello

The command will print the below output:

Output

Hello World!

Step 3 – Install Multiple GCC Versions

1) On Ubuntu 18.04, you can now install and use multiple versions of GCC. The most recent versions of the compiler come with new features, such as support for new languages, improved performance, and additional capabilities.

The default Ubuntu repositories contain multiple versions of GCC, ranging from 5.x.x to 8.x.x. The most recent version is 9.1.0, which can be obtained from the Ubuntu Toolchain PPA. In this tutorial, we will install the three most recent versions of GCC and G++.

2) To add the ubuntu-toolchain-r/test PPA to your system, use the following command:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test

3) Next, install the necessary GCC and G++ versions by following these steps:

sudo apt install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9

4) Then, the command below will configure an alternative for each version of GCC and assign a priority to it. The version with the highest priority is the default. For example, here is the configuration for gcc-9:

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
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
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7

5) If you need to change the default version later, you can use the update-alternatives command:

sudo update-alternatives --config gcc
Output

There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-9   90        auto mode
  1            /usr/bin/gcc-7   70        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:

6) You will see a list of all installed GCC versions on your Ubuntu system. Then, enter the number of version you want to use as a default. Proceed to press Enter. The command will create symbolic links which will be to the specific versions of GCC and G++.

FAQs to Install GCC Compiler on Ubuntu 18.04

What is the difference between GCC and G++ compilers?

GCC is a general-purpose compiler that can be used to compile code written in various programming languages. G++ is a C++ compiler that is a part of GCC. It is specifically designed to compile code written in the C++ programming language.

How do I check if GCC is installed on my system?

You can check if GCC is installed on your system by opening the terminal and typing "gcc -v". If GCC is installed, it will display the version number and other information about the compiler. If GCC is not installed, you will receive an error message indicating that the command is not found.

What if I encounter an error during GCC installation?

If you encounter an error during GCC installation, it may be due to missing dependencies or other issues with your system. You can try installing the missing dependencies or resolving the issue by consulting online forums or seeking assistance from the Linux community. If all else fails, you may need to reinstall your operating system or seek professional help.

Conclusion

We hope this detailed tutorial helped you to install GCC Compiler on Ubuntu 18.04.

If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.