The GCC (GNU Compiler Collection) is a free and open-source compiler that provides support for multiple programming languages, such as C/C++, Fortran, Ada, D, and Go.

If you're unfamiliar with the term compiler, it's basically a program that translates the code you write into machine code, allowing your computer to understand and run it.

It's a mandatory tool for writing and compiling program files, but certain applications that ship as source files require GCC on your system for compilation.

In this article, I'll show you how to install GCC on Ubuntu 24.04 (it'll also work for Ubuntu 23.04 and 22.04) and create and compile C/C++ program files.

How to Install GCC on Ubuntu 24.04

On Ubuntu, you can install the build-essential package that contains GCC, GNU Make, Git, GDB (GNU Debugger), and numerous other essentials required for building, compiling, and installing software programs from source code.

$ sudo apt update
$ sudo apt install build-essential

Output:

install gcc on ubuntu

Once the installation is complete, check the GCC version to verify its installation.

$ gcc --version

Output:

check GCC version on ubuntu

How to Write and Compile C/C++ Program Using GCC on Ubuntu 24.04

To write and compile a C/C++ program using GCC on Ubuntu 24.04, follow the steps below sequentially.

1. Use your choice of text editor (I prefer Nano) and create a new file with the name "myprogram" and use the extension ".c" (for C) or ".cpp" (for C++).

$ nano myprogram.c

2. Copy and paste the following line of codes:

#include <stdio.h>

int main() {
    printf("Hello, UbuntuShell!\n");
    return 0;
}

Save and close the file.

3. Run the following command to compile the C/C++ program and store the output as the "myprogram" filename.

# For C programs
$ gcc myprogram.c -o myprogram

# For C++ programs
$ g++ myprogram.cpp -o myprogram

4. Lastly, execute the compiled C/C++ program file.

$ ./myprogram

Output:

compiling C and C++ program using GCC on ubuntu

How to Remove GCC from Ubuntu 24.04

To uninstall GCC from your Ubuntu system, simply execute the following two commands:

$ sudo apt remove --purge build-essential
$ sudo apt autoremove

That's all. In this article, you've learned how to install GCC on Ubuntu, followed by writing and compiling basic C/C++ programs.