How to Install GCC Compiler on Ubuntu

thelinuxterminal

Author - Sanjay

Introduction

In this guide, I'll go over how to install GCC compiler on Ubuntu. There are multiple programming languages and many different compilers available. 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.

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

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. If you face and difficulty or any problem , let us know on comments.