How to Install GCC Compiler on Ubuntu

August 2, 2023

Introduction

The GCC (GNU Compiler Collection) is a free software compiler system capable of compiling several programming languages, including C, C++, Objective-C, and Fortran.

Developers who want to build C or C++ software on a Linux-based system need a reliable compiler like GCC.

Find out how to install GCC on Ubuntu and start compiling code on your system in no time.

Install GCC on Ubuntu

Prerequisites

How to Install GCC Compiler on Ubuntu?

There are several ways to install GCC on Ubuntu, and the preferred method depends on your customization needs or if a project requires a specific GCC version.

Method 1: Install GCC Compiler from Ubuntu Repositories

The fastest way to install GCC on Ubuntu is to use the apt package manager. However, installing GCC from Ubuntu repositories has drawbacks, such as a lack of customization options and potential dependency conflicts.

To install the GCC compiler from Ubuntu repositories:

1. Update the Ubuntu package repository using the following command:

sudo apt update

2. Install the build-essential package:

sudo apt install build-essential
Install the build-essential package in Ubuntu.

The build-essential package includes the GCC compiler and other utilities required for building software.

3. Use the following command to check the GCC version:

gcc --version
The GCC version when installed from Ubuntu repositories.

The GCC version in this example is 11.4.0.

Method 2: Install Multiple GCC Versions on Ubuntu

Users can utilize GCC PPAs (Personal Package Archive) to streamline the installation process and install a specific GCC version (or multiple GCC versions).

To do so:

1. Update the Ubuntu package repository:

sudo apt update

2. Install the software-properties-common package using the following command:

sudo apt install software-properties-common

3. Add the GCC PPA that contains all the versions of the GCC compiler:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
Add GCC PPA repository in Ubuntu.

4. Update the packages list to include the PPA packages:

sudo apt update

5. Use the command below to install a specific version or multiple versions of GCC. For example, to install GCC 12 and GCC 13, enter:

sudo apt install gcc-12 g++-12 gcc-13 g++-13 -y
Install multiple GCC versions using PPA.

Note: There are no restrictions to the number of GCC versions you can install using this installation method.

6. The update-alternatives tool helps users manage multiple GCC versions:

Add the GCC 12 alternative to the update-alternatives filesystem:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 12 --slave /usr/bin/g++ g++ /usr/bin/g++-12

Add the GCC 13 alternative to the update-alternatives filesystem:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13 --slave /usr/bin/g++ g++ /usr/bin/g++-13

Note: Replace the GCC version in the example commands (GCC 12 and 13) with the GCC versions installed on your system.

7. Use the update-alternatives tool to switch between installed GCC versions:

sudo update-alternatives --config gcc
Use the update-alternatives tool to select default GCC version.

The system displays a list of the installed GCC versions and asks to select the default version. Enter the selection number for the version you want to use.

8. Check the current GCC version using the following command:

gcc --version

Check default GCC version when installed via PPA.

The current default GCC version in this example is 13.1.0.

Method 3: Install GCC Compiler on Ubuntu from Source

Building GCC from source is an advanced installation method that allows users to customize the GCC configuration. Moreover, you can change install locations or optimize GCC for specific hardware.

To retrieve the GCC source code from official repositories and install the GCC compiler:

1. Install the necessary dependencies using the following command:

sudo apt install build-essential

2. Install the libgmp3-dev, libmpfr-dev, and libmpc-dev packages to facilitate compiling GCC from source:

sudo apt install libmpfr-dev libgmp3-dev libmpc-dev -y
Install dependencies for compiling GCC from source.

3. Use the wget command to download the GCC source code from the GCC website. In this example, the file gcc-13.2.0.tar.gz contains the source code for GCC version 13.2.0:

wget http://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz
Use wget to download GCC source file.

Adjust the URL in the command to reflect the GCC version you want to install.

4. Extract the GCC build files using the following command:

tar -xf gcc-13.2.0.tar.gz

5. Access the GCC directory:

cd gcc-13.2.0

6. Configure the GCC build:

./configure -v --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --prefix=/usr/local/gcc-13.2.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib --program-suffix=-13.2.0

Visit the official GNU GCC configuration page to review available options and recommended configuration procedures.

7. Use the make command to initiate the GCC build process:

make -j3

The build process can consume a lot of resources and take a while to complete. The j3 command tells the system to use three cores for the operation. Change the number of cores to reflect your system's capabilities and setup.

8. After the build process is complete, install GCC using the following command:

sudo make install
Install GCC from source on Ubuntu.

9. Enter the following command to verify the GCC version:

/usr/local/gcc-13.2.0/bin/gcc-13.2.0 --version
Verify GCC version when installed from source.

The system confirms GCC version 13.2.0 is installed.

Conclusion

You now know how to install the GCC compiler on Ubuntu using three different methods. Regardless of the method, installing GCC on an Ubuntu machine allows you to compile and run C and C++ code and complete many programming, debugging, and system administration tasks.

Check out these five different test automation frameworks that make automated testing faster and more reliable.

Was this article helpful?
YesNo
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
22 Best Linux Text Editors for Programming & Coding
September 3, 2019

A text editor is an application that lets you type text. All Linux distributions come with built-in editors, but there are also other options. In this article, we review the 22 best text editors.
Read more
How to Build Linux Kernel From Scratch
November 12, 2020

All Linux distributions come with a predefined kernel. However, they are usually outdated. Follow this step-by-step guide to learn how to build a Linux kernel from scratch.
Read more
How To Install and Use Linux Screen
April 7, 2022

Screen is a powerful tool for working in the command line. It lets you create, monitor, and switch between several different windows.
Read more
15 Best DevOps Tools IT Experts Use
August 2, 2023

This article reviews the best DevOps tools currently on the market and helps make an informed decision on what tools are worth adding to your stack.
Read more