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 gcc, g++, 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

What Is a Data Protection Platform and Why Your Organization Needs One

A data protection platform is a unified system that helps organizations discover, classify, monitor, and…

1 week ago

How Swiss Privacy Rules Affect AI Companies Under GDPR

AI companies operating in or around Switzerland face a compliance challenge that most legal teams…

1 week ago

Swiss FADP vs EU GDPR: Key Differences for AI and Data Privacy

Two major data privacy laws now govern how organizations handle personal data across Europe; the…

1 week ago

Kali Linux Commands Cheat Sheet: Complete Quick Reference

This cheat sheet covers the essential Kali Linux commands every pentester and ethical hacker uses…

1 month ago

What I Wish I Knew Before Learning Malware Analysis and Reverse Engineering

When I first started learning malware analysis and reverse engineering, I thought the hardest part…

1 month ago

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

2 months ago