How to Install GCC Compiler on Ubuntu 18.04

Updated on

4 min read

Install GCC on Ubuntu

The GNU Compiler Collection (GCC) is a collection of compilers and libraries for C, C++, Objective-C, Fortran, Ada, Go , and D programming languages. Many open-source projects, including the GNU tools and the Linux kernel, are compiled with GCC.

This tutorial covers the steps required to install the GCC compiler on Ubuntu 18.04. We will show you how to install the distro stable version and the latest version of GCC.

The same instructions apply for Ubuntu 16.04 and any Ubuntu-based distribution, including Kubuntu, Linux Mint and Elementary OS.


Te able to add new repositories and install packages on your Ubuntu system, you must be logged in as root or user with sudo privileges .

Installing GCC on Ubuntu

The default Ubuntu repositories contain a meta-package named build-essential that contains the GCC compiler and a lot of libraries and other utilities required for compiling software.

Perform the steps below to install the GCC Compiler Ubuntu 18.04:

  1. Start by updating the packages list:

    sudo apt update
  2. Install the build-essential package by typing:

    sudo apt install build-essential

    The command installs a bunch of new packages including gcc, g++ and make.

    You may also want to install the manual pages about using GNU/Linux for development:

    sudo apt-get install manpages-dev
  3. To validate that the GCC compiler is successfully installed, use the gcc --version command which prints the GCC version:

    gcc --version

    The default version of GCC available in the Ubuntu 18.04 repositories is 7.4.0:

    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

That’s it. GCC is now installed on your system, and you can start using it.

Compiling a Hello World Example

Compiling a basic C or C++ program using GCC is pretty easy. Open your text editor and create the following file:

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

Save the file and compile it into an executable using the following command:

gcc hello.c -o hello

This will create a binary file named hello in the same directory where you run the command.

Execute the hello program with:


The program should print:

Hello World!

Installing Multiple GCC Versions

This section provides instructions about how to install and use multiple versions of GCC on Ubuntu 18.04. The newer versions of the GCC compiler include support for new languages, better performance and extended features.

At the time of writing this article, the default Ubuntu repositories include several GCC versions, from 5.x.x to 8.x.x. The latest version of GCC, which is 9.1.0 is available from the Ubuntu Toolchain PPA.

In the following example, we will install the latest three versions of GCC and G++.

First, add the ubuntu-toolchain-r/test PPA to your system with:

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

Install the desired GCC and G++ versions by typing:

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

The commands below will configure alternative for each version and associate a priority with it. The default version is the one with the highest priority, in our case that is 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-9sudo 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-8sudo 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

Later if you want to change the default version use the update-alternatives command:

sudo update-alternatives --config gcc
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:

You will be presented with a list of all installed GCC versions on your Ubuntu system. Enter the number of the version you want to be used as a default and press Enter.

The command will create symbolic links to the specific versions of GCC and G++.


You have successfully installed GCC on your Ubuntu 18.04. You can now visit the official GCC Documentation page and learn how to use GCC and G++ to compile your C and C++ programs.

If you hit a problem or have feedback, leave a comment below.