How to Install GCC Compiler on Ubuntu

thelinuxterminal

Author - Sanjay

Introduction

If you are a programmer or a software developer, you must have heard of the GCC compiler. It is one of the most popular compilers available and is used to compile code written in C, C++, and other programming languages. If you are using Ubuntu, you may wonder how to install GCC compiler on your system. In this article, we will provide you with a step-by-step guide on how to install GCC compiler on Ubuntu. If you're just getting into programming and don't know what a compiler is, look it up. But if you already know what a compiler is and want a quick rundown of installing one in Ubuntu, keep reading.

Table of Contents

Pre-Requisistes

A Ubuntu 22.10 server or a Desktop with root privilages.

What is a GCC Compiler ?

Before we dive into the installation process, let us first understand what the GCC compiler is. GCC, which stands for GNU Compiler Collection, is a set of compilers that are used to compile code written in various programming languages. It was originally developed by the Free Software Foundation (FSF) for the GNU operating system but can now be used on many different operating systems, including Ubuntu.

GCC compiler supports various programming languages such as C, C++, Objective-C, Fortran, Ada, and others. It also comes with a number of useful features such as optimization options, debugging options, and code profiling tools. Installing GCC compiler on Ubuntu is a straightforward process and can be done in a few simple steps.

Install GCC on UBUNTU Step By Step

  1. Lets start the first step by updating our pacakge repo .
$thelinux $ sudo apt-get update 

Update repo

  1. Next step is to install the build-essential package using below command
$thelinux $ sudo apt install build-essential

Build Essential

This command as shown above installs a bunch of new packages including gcc, g++ and make , it comes prepackaged in most of linux repo .

  1. Now Install Manual Pages , Although its optional
sudo apt-get install manpages-dev

Man Pages GCC

  1. Check the Gcc Version
$ gcc --version

 GCC Version

Step by Step Guide to Execute Simple C Program Using GCC

  1. First step is create a C file by executing below command
thelinuxt$  sudo vi hello.c

This will open a text editor, where you can write your C/C++ program and save in it. Check if your file hello.c has the permission to execute using the command:

  1. Next step is to compile your program using the following command .
gcc hello.c -o hello

 Compile C Program

  1. Last step is to execute the program as shown below
./hello

Installing Multiple GCC Versions in Ubuntu

  1. Lets first add the ubuntu-toolchain-r/test PPA to our system using below command.
thelinuxt $ sudo add-apt-repository ppa:ubuntu-toolchain-r/test

 Tool Chain

  1. Next step install the desired GCC and G++ versions as shown below , We will install gcc version 9,10 and 11.
thelinuxt $  sudo apt install gcc-9 g++-9 gcc-10 g++-10 gcc-11 g++-11

Multiple GCC Compiler

Install G++ Compiler (Optional)

If you are planning to write code in C++, you may need to install G++ compiler as well. To install G++ compiler on Ubuntu, enter the following command in the terminal

thelinuxt $  sudo apt install g++

Install Build-Essential Package (Optional)

If you are planning to compile and build software on your Ubuntu system, you may need to install the build-essential package. This package includes various tools such as GCC, G++, make, and other essential tools needed for building software. To install the build-essential package on Ubuntu, enter the following command in the terminal:

thelinuxt $ sudo apt install build-essential

Conclusion

We have done GCC installation on our Ubuntu system . We have also seen how we can write a program and Compile a C program using GCC. GCC compiler is a powerful tool for compiling code written in various programming languages and comes with a number of useful features. Installing GCC compiler on Ubuntu is a simple process that can be done in a few simple steps.

If you face and difficulty or any problem , let us know on comments.