How to Install GCC on Ubuntu 22.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install GCC on Ubuntu 22.04, let's briefly understand – What is GCC?

GCC, or the GNU Compiler Collection, is a widely-used open-source compiler system. It allows programmers to write code in various programming languages, such as C, C++, and Fortran and translates it into machine code that can run on different platforms.

GCC offers a comprehensive set of tools and optimizations, making it popular among developers. With its flexibility and community support, GCC empowers software development by enabling efficient and reliable code compilation and execution across multiple operating systems and architectures.

In this tutorial, you will install GCC on Ubuntu 22.04. We will also address a few FAQs on how to install GCC on Ubuntu 22.04.

Advantages of GCC

  1. Open-Source: GCC is freely available and can be modified to suit specific needs, making it a preferred choice for developers.
  2. Language Support: GCC supports multiple programming languages, including C, C++, and Fortran, providing flexibility to developers.
  3. Portability: GCC generates machine code for various platforms, allowing software to run seamlessly on different systems.
  4. Optimization: GCC offers advanced optimization techniques, improving code performance and efficiency.
  5. Community Support: With a large user community, GCC benefits from continuous improvements, bug fixes, and support from experienced developers.

How to install GCC on Ubuntu 22.04

Please refer to the instructions provided below if you want to install GCC on Ubuntu 22.04.

Step 1 - Update the system

Update your Ubuntu 22.04 by running the following command before installing GCC. This is done to ensure that all relevant packages are updated and no conflicts arise.

sudo apt update

Step 2 – Install GCC

Installing the build-essential package, which includes packages for GCC and numerous other packages, is then advised.

sudo apt install build-essential -y

Step 3 – Evuate the Installation

Now, use the following command to check the C compiler version to see if GCC was successfully installed.

gcc --version
Output

gcc (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.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.

Step 4 - Create a C program

Once installed, test the compiler's functionality by writing a simple C program. For instance, in the example below, we developed a basic hello world C program and saved the file as hello.c.

nano hello.c
#include <stdio.h>
int main()
{
  printf ("Hello World!\n");
  return 0;
}

This file should be saved and made executable.

gcc -o hello hello.c

The hello binary file will be created by this command and placed in the same directory. Now you have to execute the program using the below-mentioned command.

./hello
Output

Hello World!

The C program was successfully run.

Congratulations! You have successfully installed GCC on Ubuntu 22.04.

FAQs to Install GCC on Ubuntu 22.04

Can I install a specific version of GCC on Ubuntu 22.04?

Yes, you can. Use the command sudo apt install gcc-<version> to install a specific version of GCC, such as gcc-9 or gcc-10.

How can I check the installed GCC version?

Simply type gcc --version in the terminal, and it will display the installed GCC version along with other relevant information.

Do I need to install additional packages for C++ development?

Yes, you can install the necessary C++ development packages by running sudo apt install g++.

Can I compile programs with GCC using multiple CPU cores?

Yes, you can use the -j option followed by the desired number of cores. For example, make -j4 will utilize four cores for compilation.

Are there any GUI-based alternatives to installing GCC?

Yes, you can use the Ubuntu Software Center or Synaptic Package Manager to search for and install GCC.

How can I uninstall GCC from Ubuntu 22.04?

Execute the command sudo apt remove gcc to remove the GCC package from your system.

Can I use GCC for cross-compiling on Ubuntu 22.04?

Yes, you can install the required cross-compilers by adding the target architecture prefix to the package name. For example, sudo apt install gcc-arm-linux-gnueabi for ARM cross-compilation.

Is it necessary to keep GCC updated on Ubuntu 22.04?

It's generally recommended to keep your software updated for security and performance reasons. You can use the package manager "apt" to update GCC along with other system packages.

Conclusion

We hope this detailed tutorial helped you understand how to install GCC on Ubuntu 22.04.

If you have any suggestions or queries, kindly leave them in the comments section.