Autumn Sale - up to 36% OFF

How to Install GCC on Ubuntu 22.04 [and Compile a C Program]

How to Install GCC on Ubuntu 22.04 [and Compile a C Program]
Published on Nov 21, 2023 Updated on Sep 25, 2024

Software development plays an important role in the economic landscape. At the core of every software lies a crucial element – the compiler. Enter the GCC (GNU Compiler Collection), or GCC for short. In this article, we'll show you how to install GCC on Ubuntu 22.04 and compile a C program.

#What is GCC?

GCC is a widely used compiler for various programming languages - a compiler is a software tool that translates the code you write for computer programs, commonly known as the "source code," into instructions a computer can understand. This translation process is known as compilation.

As an important part of free and open-source software development, the GCC (GNU Compiler Collection) was developed by the Free Software Foundation (FSF) through a collaborative effort of developers led by Richard Stallman.

#How to use GCC?

GCC is widely used in creating software for Unix-like systems, and it has been adapted for various platforms, demonstrating its adaptability and flexibility in the realm of open-source programming.

The GCC (GNU Compiler Collection) supports various programming languages for compilation. Although initially developed in C, GCC can now compile the following prominent languages, such as C++, Fortran, Ada, and Go.

#Prerequisites

To follow along this tutorial and install GCC on Ubuntu 22.04, you will need:

#1. How to install GCC on Ubuntu 22.04

In the first part of this tutorial, we will show you how to install GCC on Ubuntu in three steps, and in the second part, how to compile a C program.

We will see the different methods of installing GCC and compile a C program, exploring various options.

#Step 1: Update Ubuntu package list

To update the package list, use the following command:

sudo apt update

#Step 2: Install GCC on Ubuntu

We now install GCC with the following command.

sudo apt install gcc

If GCC is already installed on your system, the command will list the version installed.

You can install GCC with the build-essential package. This will install GCC as well as other popular packages such as make, which is often used with GCC to automate the compilation process of bigger software.

To install build-essentials, use the following command:

sudo apt install build-essential

#Step 3: Test GCC installation

Let's now check if GCC has been installed on Ubuntu:

gcc --version

This should return the GCC version and license as follows:

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.

#2. How to compile a C program

Now that we’ve installed GCC on Ubuntu and everything is correctly set up, we can continue to compile a C program.

#Step 1: Write a basic C program

Open your favorite code editor and write in the following C source code:

#include <stdio.h>
int main() {
   printf("Welcome to Cherry Servers\n");
   return 0;
}

Save the file as example1.c

#Step 2: Compile example1.c using GCC

Now that we have our source code, we can compile it with GCC using the following command:

gcc example1.c

By default, GCC will output the compiled source code in a file called a.out

Let's execute it and check the results:

didier@lab:~$ ./a.out
Welcome to Cherry Servers

You can also specify the name of the output with the switch -o followed by the desired name:

gcc example1.c -o example1

#Step 3: Generate an assembly listing of the C program

You can also use GCC to generate an assembly listing of the C source code. This can be done using the -S switch:

 gcc -S example1.c

This will generate a file called example1.s. You can then display its content using:

cat example1.s

The output should be similar to the following code:

        .file   "example1.c"
        .text
        .section        .rodata
.LC0:
        .string "Welcome to Cherry Servers"
        .text
        .globl  main
        .type   main, @function
main:
.LFB0:
        .cfi_startproc
        endbr64
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        leaq    .LC0(%rip), %rax
        movq    %rax, %rdi
        call    puts@PLT
        movl    $0, %eax
        popq    %rbp
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   main, .-main
        .ident  "GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
        .section        .note.GNU-stack,"",@progbits
        .section        .note.gnu.property,"a"
        .align 8
        .long   1f - 0f
        .long   4f - 1f
        .long   5
0:
        .string "GNU"
1:
        .align 8
        .long   0xc0000002
        .long   3f - 2f
2:
        .long   0x3
3:
        .align 8
4:

#Step 4: Generate debug information

Another useful option of GCC is to generate debug information and store it in the compiled program. While this option is very useful to debug programs, it should not be used in the final released version as it makes the program bigger.

To generate debug information that can be used by gdb - the GNU C debugger, use the following switch -ggdb as follows:

gcc example1.c -o example1 -ggdb

We can now access the debug information using gdb:

gdb example1

Let's now enter a breakpoint at the start of the program:

break main

We can now start the debugging with the run command:

run

This will debug the program and display the debugging information generated by GCC:

Starting program: /home/didier/example1
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Breakpoint 1, main () at example1.c:3
3          printf("Welcome to Cherry Servers\n");
(gdb)

You can then exit gdb by typing the exit command in the gdb shell.

#Conclusion

This tutorial covered what a compiler is and how GCC plays a big part in software development. We learned how to install GCC on Ubuntu 22.04, compile a C program, generate an assembly list, and debug information.

After 36 years, GCC is still maintained and is one of the most used software in the world, from home users to big corporations. You can learn more about GCC by accessing its manual page: man gcc or reading the various online manuals.

Cloud VPS Hosting

Starting at just $3.24 / month, get virtual servers with top-tier performance.

Share this article

Related Articles

Published on Sep 29, 2025 Updated on Sep 29, 2025

How to Rename Files in Linux Using mv & rename Commands

Learn how to rename files in Linux using mv for basic renaming and rename for advanced batch operations with Perl expressions and file extensions.

Read More
Published on Sep 9, 2025 Updated on Sep 15, 2025

Best Linux Distros for Developers in 2025

Explore the 10 best Linux distros for developers in 2025. Compare features, stability, and use cases to choose the right OS for coding, cloud, and DevOps.

Read More
Published on Sep 4, 2025 Updated on Oct 9, 2025

Top 20 Linux Commands For Cloud and DevOps Engineers

Master essential Linux commands every Cloud and DevOps engineer needs. Boost skills in navigation, file management, monitoring & networking with clear examples.

Read More
We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: bc227188e.1442