How to Install GCC on Ubuntu 22.04

GCC is a free and open-source compiler system developed by the GNU (free software, operating system) project. It supports and compiles the codes of several programming languages, such as the C family, Fortran, etc.

GCC is widely used for GNU-related projects, Linux kernels, etc. This write-up acts as an installation guide for GCC on Ubuntu 22.04. The content of this post is as follows:

Let’s explore the first outcome:

How to Install GCC on Ubuntu 22.04?

GCC can be installed on Ubuntu 22.04 through build-essential using the APT package manager. To install the GCC compiler collection on Ubuntu 22.04, follow the provided step-by-step process.

Step 1: Update System repository

It is a good practice to update the system repository to attain the updated/latest packages. For this aim, run the following command:

$ sudo apt update

It can be seen that all the packages are up to date.

Step 2: Install build-essential (GCC)

The build-essential is the meta-packages consisting of various tools that help in compiling the software. It also includes GCC and other related essential tools. To install build-essential, run the following command:

$ sudo apt install build-essential

You can see that GCC is unpacking, preparing, and setting up in the build-essential packages suite.

Step 3: Check Version

After installing the build-essential, you can confirm the GCC by verifying its version:

$ gcc --version

It is verified that GCC -11.2.0 has been installed on Ubuntu 22.04.

How to Install GCC-12 (Updated Version) on Ubuntu 22.04?

At the time of writing this post, the build-essentials contain the GCC-11.2. However, you can install the latest version-12 on Ubuntu 22.04 using the following command in the terminal:

$ sudo apt install gcc-12

The latest version of GCC (GCC-12) is installed on Ubuntu 22.04.

After the successful installation, you can check the version of installed GCC on Ubuntu 22.04 using the following command:

$ gcc-12 --version

It is verified that the installed version of GCC is 12.

How to Use GCC on Ubuntu 22.04?

This section presents the basic use of GCC on Ubuntu 22.04. To do so, we are creating a simple “.c” file and adding a few C language lines of code to it. Stick to the following steps to understand how GCC can be used on Ubuntu 22.04. 

Step 1: Create a C file

The below script creates a “hello.c” file in PWD:

$ nano hello.c

Now, add the below-mentioned code:

#include <stdio.h>
 
int main() {
    printf("Hello, world!\n");
    return 0;
}

Press CTRL+S to save all the changes and CTRL+X to come out of the nano editor.

Step 2: Compile the C File

Compile the “hello.c” file using the GCC as follows:

$ gcc hello.c -o hello

Step 3: Run the file

The above command has generated the executable file (hello) of the C source code. Execute that file via the following command:

$ ./hello

The output shows that the program has been executed successfully.

How to Remove GCC From Ubuntu 22.04?

If users have already installed GCC with the help of the APT package manager, you can remove it using the following command:

$ sudo apt remove gcc

The GCC was successfully removed from Ubuntu 22.04.

However, to remove the build-essential, run the following command:

$ sudo apt remove build-essential

The build-essential is removed from Ubuntu 22.04.

Congratulations! You have learned the installation of GCC on Ubuntu 22.04 successfully, along with build-essential.

Conclusion

The GCC can be installed on Ubuntu 22.04 from the default repository of Ubuntu 22.04 or the build-essential packages suite. The GCC is a free and open-source GNU compiler system for several programming languages. You have learned to install the available version of GCC from the default repository. Moreover, you have also understood the method to install GCC-12 on Ubuntu 22.04.