Table of Contents
GCC stands for GNU Compiler Collection, which is a set of compilers and libraries for various programming languages, such as C, C++, Objective-C, Fortran, Ada, Go, and D. It is one of the most widely used tools for open-source software development and is the default compiler for many Linux distributions, including Ubuntu.
Ubuntu 22.04 comes with GCC 11 as the default version. However, if you need to use a different version of GCC for your projects, you can easily install it on your system. In this article, we will show you two methods to install GCC on Ubuntu 22.04, using the default Ubuntu repository or using a third-party repository called Ubuntu Toolchain PPA.
Install GCC with Ubuntu Repository
The easiest way to install GCC on Ubuntu 22.04 is to use the default Ubuntu repository that contains a meta-package called build-essential. This package includes not only GCC but also other development tools such as make, g++, and dpkg-dev. To install it, follow these steps:
Open a terminal window by pressing Ctrl+Alt+T
or searching for Terminal in the Dash.
Update your system package list by running the following command.
sudo apt update
Install the build-essential package by running the below command.
sudo apt install build-essential
Verify that GCC is successfully installed by running
gcc --version
You should see something like this:
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This means that you have installed GCC 11 on your system.
Install GCC with Ubuntu Toolchain PPA
If you want to install a newer or older version of GCC on Ubuntu 22.04, you can use a third-party repository called Ubuntu Toolchain PPA. This repository provides the latest versions of GCC and other compilers for Ubuntu users. To use it, follow these steps:
Open a terminal window by pressing Ctrl+Alt+T
or searching for Terminal in the Dash.
Add the Ubuntu Toolchain PPA to your system by running:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
Update your system package list by running:
sudo apt update
Install the desired GCC version by running:
sudo apt install gcc-12
Verify that GCC is successfully installed by running:
gcc-12 --version
You should see something like this:
gcc (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This means that you have installed GCC 12 on your system.
How to Use GCC on Ubuntu
To use GCC to compile a basic C or C++ program on Ubuntu, you need to create a source file, compile it and run it. Here is an example of how to do it:
Open a text editor of your choice and create a file called helloworld.c
with the following content:
#include <stdio.h>
int main() {
printf(“Hello, world!\n”);
return 0;
}
Save the file and close the editor.
Open a terminal window, navigate to the directory where you saved the file.
For example, if you saved the file in your Documents folder, run:
cd ~/Documents
Compile the file using GCC by running:
gcc helloworld.c -o helloworld
This will create an executable file called hello in the same directory.
Execute the file the executable file by running:
./helloworld
You should see the following output:
Hello, world!
You can use various options and flags with GCC to control the compilation process, such as optimization level, warning messages, debugging information, and so on. To learn more about them, you can run:
gcc --help
or visit the official GCC documentation.
Comparison of GCC Versions
There are several versions of GCC available on Ubuntu 22.04, ranging from 7.x.x to 12.x.x. Each version has its own features and performance characteristics that may affect your projects differently. Here is a table that compares some of the main aspects of different GCC versions on Ubuntu 22.04:
Version | Release Date | C Standard | C++ Standard | Optimization Level | New Features |
---|---|---|---|---|---|
GCC 7 | May 2017 | C11 | C++17 | -O3 | Parallel STL, C++ Coroutines, OpenMP 4.5, etc. |
GCC 8 | May 2018 | C11 | C++17 | -O3 | Profile-guided optimization improvements, C++20 Concepts, OpenMP 5.0, etc. |
GCC 9 | May 2019 | C11 | C++17 | -O3 | Link-time optimization improvements, C++20 Ranges and Modules, OpenACC 2.6, etc. |
GCC 10 | May 2020 | C11 | C++20 | -O3 | Whole program optimization improvements, C++20 Coroutines and Contracts, OpenMP Offloading Library Routines, etc. |
GCC 11 | April 2021 | C17 | C++20 | -O3 | Interprocedural optimization improvements, C++20 Concepts and Ranges improvements, OpenMP Offloading Library Routines improvements, etc. |
GCC 12.3 | May 2023 | C23 | C++23 | -O3 | Static analysis improvements, C++23 Contracts and Modules improvements, OpenMP Offloading Library Routines improvements, etc. |
As you can see from the table, each version of GCC introduces new features and improvements that may benefit your projects in different ways. However, each version also has its own drawbacks and limitations that may affect your projects negatively.
For example, newer versions of GCC may not be compatible with older versions of libraries or frameworks that you use, or may introduce new bugs or errors that you need to fix. Therefore, it is important to choose the right version of GCC for your projects depending on your needs and preferences.
Install Multiple GCC Versions on Ubuntu
To install multiple versions of GCC on Ubuntu, you need to use the Ubuntu Toolchain PPA that provides different versions of GCC and other compilers. To do so, follow these steps:
Open a terminal window by pressing Ctrl+Alt+T or searching for Terminal in the Dash.
Add the Ubuntu Toolchain PPA to your system by running:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
Update your system package list by running:
sudo apt update
Install the desired GCC versions by running:
For example, to install GCC 11 and 12, run:
sudo apt install gcc-11 gcc-12
Verify that GCC is successfully installed by running
For example, to check GCC 11 and 12, run:
gcc-11 --version
gcc-12 --version
You should see something like this:
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
gcc (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This means that you have installed GCC 11 and 12 on your system.
Switch between different versions of GCC on Ubuntu
To switch between different versions of GCC on Ubuntu, you need to use the update-alternatives command that allows you to configure alternative versions of programs on your system. To do so, follow these steps:
Open a terminal window by pressing Ctrl+Alt+T or searching for Terminal in the Dash.
Register the GCC versions that you want to switch between by running:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-<version> <priority>
For example, to register GCC 11 and 12, run:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 12
The priority value determines which version is used by default when you run gcc without specifying the version. The higher the priority, the higher the preference.
Switch between the GCC versions by running:
sudo update-alternatives --config gcc
You will see something like this:
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/gcc-12 12 auto mode
1 /usr/bin/gcc-11 11 manual mode
2 /usr/bin/gcc-12 12 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
You can type the selection number of the version that you want to use and press enter. For example, to switch to GCC 12, type 2 and press enter.
Verify that GCC is successfully switched by running:
gcc --version
You should see something like this:
gcc (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
This means that you have switched to GCC 12 on your system.
You can repeat these steps to switch between any versions of GCC that you have installed on your system.
Conclusion
In this article, we have shown you how to install GCC on Ubuntu 22.04 using two methods: using the default Ubuntu repository or using the Ubuntu Toolchain PPA. We have also shown you how to use GCC to compile a basic C or C++ program and how to compare different versions of GCC available on Ubuntu 22.04. We hope that this article has helped you to learn more about GCC and Ubuntu and how to use them for your software development projects.
If you have any questions, comments, or feedback, please feel free to share them with us in the comment section below. We would love to hear from you and help you with any issues that you may encounter. Thank you for reading and happy coding!