GCC, better known as The GNU Compiler Collection, is a set of compilers, development tools, and front ends such as C, C++, Objective-C, Fortran, Ada, Go, and D. GCC is open-source and is widely used as it was the original compiler for GNU and currently is in use to compile the Linux Kernel along with many other projects.
In the following tutorial, you will learn how to install GCC on Ubuntu 20.04 LTS Focal Fossa desktop or server.
Table of Contents
Prerequisites
- Recommended OS: Ubuntu 20.04.
- User account: A user account with sudo or root access.
The tutorial will utilize the terminal interface, which can be found in the show applications menu.
Example:
Update Operating System
Update your Ubuntu operating system to make sure all existing packages are up to date:
sudo apt update && sudo apt upgrade -y
The tutorial will be using the sudo command and assuming you have sudo status.
To verify sudo status on your account:
sudo whoami
Example output showing sudo status:
[joshua@ubuntu ~]$ sudo whoami
root
To set up an existing or new sudo account, visit our tutorial on How to Add a User to Sudoers on Ubuntu.
Use the following command with the root password to log in to use the root account.
su
Option 1 – Install GCC with APT – Ubuntu Repository
The first recommended option to install GCC is to install the build-essential package containing GCC and many other application packages such as make.
To begin the installation, use the following command.
sudo apt install build-essential
Example output:
Type Y, then press the ENTER KEY to proceed.
Once installed, verify the installation and check the version using the following command.
gcc --version
Example output:
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Option 2 – Install Latest GCC with APT – Toolchain PPA
The second option is to install the Toolchain PPA, which contains the latest versions of GCC.
To import the PPA, use the following command.
sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
For users that have installed the build-essential package, you can run an update and upgrade.
sudo apt update && sudo apt upgrade
For users who need to install for the first time, run the same command as option 1.
sudo apt install build-essential
Install & Configure Multiple Versions
For developers and specific users, you may require multiple versions installed of GCC.
First, install the versions you require, or install four versions of GCC along with G++.
Example:
sudo apt install gcc-8 g++-8 gcc-9 g++-9 gcc-10 g++-10 gcc-11 g++-11
Once installed, you can use the update-alternatives command to configure the priority of each version. The default version used on your system will be the highest priority.
Below is an example of setting all 4 with a priority split, with GCC-11 being the highest set at priority 100.
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 100 --slave /usr/bin/g++ g++ /usr/bin/g++-11 --slave /usr/bin/gcov gcov /usr/bin/gcov-11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 80 --slave /usr/bin/g++ g++ /usr/bin/g++-10 --slave /usr/bin/gcov gcov /usr/bin/gcov-10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 60 --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 40 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8
Confirm GCC-11 is now the default version of your system.
gcc --version
Example output:
gcc (Ubuntu 11.1.0-1ubuntu1~20.04) 11.1.0
The default GCC version set on your system can be re-configured. Use the following command first to list the priority.
sudo update-alternatives --config gcc
Example output:
As above, you need to enter the number of the version corresponding to the list you would like to switch to.
For example, switching from GCC-11 to GCC-10, just type 2 and press the ENTER KEY.
Confirm the version switch has occurred successfully using the –version command.
gcc --version
Example output:
gcc (Ubuntu 10.3.0-1ubuntu1~20.04) 10.3.0
Compile a Hello World Example
Test compiling with GCC, create the famous “Hello World” in a C program using any text editor, the tutorial will use nano.
nano hello.c
Next, add the following.
#include <stdio.h>
int main()
{
printf("Hello, World from Linuxcapable.com!");
return 0;
}
Save the file, CTRL+O, then exit CTRL+X.
Compile the Hello World test using the following command.
gcc hello.c -o hello
Next, run the hello compiled program.
./hello
Example output:
Hello, World from Linuxcapable.com!
A handy package to install is the manual pages (man page). This will help you with using GNU by providing documentation on your system.
Use the following command to install the manpages-dev package. By default, this should be installed on most Ubuntu systems.
sudo apt-get install manpages-dev
Once installed, use the following command to bring up the options.
man -h
Comments and Conclusion
In the tutorial, you have learned how to install GCC, along with a PPA option for newer GCC alternatives and how to switch between versions on Ubuntu 20.04 LTS Focal Fossa.
For further reading and information, visit the GCC official documentation.