How To

Install GCC on Ubuntu 18.04: C and C++ Compiler Setup Guide

GCC (GNU Compiler Collection) is a set of compilers and development libraries for C, C++, Fortran, Go, and several other languages. It is one of the most important tools in the open-source ecosystem. The Linux kernel, GNU coreutils, and thousands of other projects are compiled with GCC. Everything from embedded firmware to desktop applications can be built with it.

This guide shows you how to install GCC on Ubuntu 18.04, compile a basic C program to verify the setup, and manage multiple GCC versions on the same system.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

Install GCC on Ubuntu Using build-essential

The easiest way to install GCC on Ubuntu is through the build-essential meta-package. It pulls in GCC, G++, make, and a set of standard development libraries in a single command.

Update the package list:

bashsudo apt update

Install build-essential:

bashsudo apt install build-essential

You can also install the GNU/Linux development manual pages, which are useful for looking up standard library functions:

bashsudo apt-get install manpages-dev

Verify the installation:

bashgcc --version

Output:

gcc (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0Copyright (C) 2017 Free Software Foundation, Inc.

GCC 7.4.0 is the default version in the Ubuntu 18.04 repositories. It is now installed and ready to use.

Compile a Hello World Program

To confirm GCC is working, write and compile a simple C program. Create a source file:

bashnano hello.c

Add the following code:

c#include &lt;stdio.h&gt;int main(){  printf ("Hello World!\n");  return 0;}

Compile it into an executable:

bashgcc hello.c -o hello

This creates a binary file named hello in the current directory. If you leave out the -o hello flag, GCC names the output a.out by default. Run the program with:

bash./hello

Output:

Hello World!

Install and Manage Multiple GCC Versions

Ubuntu 18.04 includes GCC versions from 5.x to 8.x in its default repositories. GCC 9.x is available through the ubuntu-toolchain-r PPA. Installing multiple versions is useful when you need to test code against different compiler behaviors or build older projects that require a specific compiler version.

Add the PPA and install GCC versions 7, 8, and 9:

bashsudo apt install software-properties-commonsudo add-apt-repository ppa:ubuntu-toolchain-r/testsudo apt install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9

Register each version with update-alternatives and assign a priority. The version with the highest priority becomes the default:

bashsudo 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

To switch the active default version at any time:

bashsudo update-alternatives --config gcc

You will see a numbered list of all installed GCC versions. Type the number of the version you want and press Enter. The command updates the gccg++, and gcov symlinks to point to the version you selected.

GCC is now installed and configured on your Ubuntu 18.04 system. You can compile C and C++ programs right away and switch between compiler versions whenever your project requires it. Leave a comment below if you run into any issues during setup.

Cyber Defence

Recent Posts

xargs Command in Linux: Build and Execute Commands from Input

The xargs command in Linux reads items from standard input and passes them as arguments to another…

2 days ago

locate Command in Linux: Find Files Fast with a Database Search

The locate command in Linux searches for files and directories by name. It queries a pre-built database…

2 days ago

Listing Linux Services with systemctl: A Complete Guide

Most modern Linux distributions use systemd as the default service manager. Knowing how to list…

2 days ago

How to Truncate Files in Linux: Empty Files Without Deleting

Truncating a file in Linux means removing its contents while leaving the file itself in…

2 days ago

ss Command in Linux: Display Socket Statistics and Connections

The ss command in Linux lists open sockets and active network connections. It replaced the deprecated netstat command and…

3 days ago

ftp Command in Linux: Connect to Servers and Transfer Files

The ftp command in Linux connects to a remote FTP server and transfers files. FTP transmits everything…

3 days ago