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> You need sudo access.
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.
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 <stdio.h>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!
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.
Python 3.7 was a significant release for the language. It introduced data classes, a decorator that automatically…
Odoo is a popular open-source suite of business applications. A single platform covers CRM, e-commerce, accounting,…
Let's Encrypt is a free, automated certificate authority run by the Internet Security Research Group…
Python is one of the most widely used programming languages in the world. Its clean, readable…
Apache Tomcat is an open-source Java application server that implements the Java Servlet, JavaServer Pages, and…
Setting the correct timezone on your Ubuntu machine is more important than it sounds. Your…