How to Install C and GCC Compiler on Linux?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Linux is a set of open-source UNIX-like operating systems, and Ubuntu is a Linux-based operating system commonly used to run Linux-based applications. To install C on Linux and to build and run our C program file on Ubuntu, we need to install the GCC Compiler. In Ubuntu repositories, GCC Compiler is a part of the build-essential package we need to install.

We understand that installing C on Linux might seem daunting, but don't worry; we've got you covered with our step-by-step guide!

Installing the GCC Compiler Using Terminal in Linux

GCC Compiler (GNU Compiler Collection) is a collection of compilers and libraries for the programs written in C, C++, Ada, GO, D, Fortran, and Objective-C programming languages and it is distributed under the GNU General Public License. We are going to install the GCC compiler using the Linux terminal to install C on Linux.

The GCC Compiler, and several other libraries and utilities required for building applications, can be found in the default Ubuntu repositories (Ubuntu is a Linux-based Operating System) under the build-essential meta-package.

What is the build-essential Package?

This build-essential meta-package on Ubuntu comes with five separate packages that are required during a software compilation process, and it contains the GCC compiler that will be used to build and run your C Programs. These five packages are listed below:

  • gcc: The GNU Compiler Collection (GCC Compiler) is used to compile programs written in C Language.
  • g++: The GNU C++ compiler is used to compile programs written in C++ Language.
  • libc6-dev: This is the GNU library files package. The development libraries and header files needed to compile simple C and C++ programs are included in this package.
  • make: This is a handy tool for controlling the compilation of program. This tool reads a file called a "makefile", which tells the compiler how to do its job.
  • dpkg-dev: This package can be used to unzip, compile, and upload Debian source packages. If you wish to package your program for a Debian-based system, this tool will come in handy.

So, we have to perform the following steps to install the build-essential package in our Linux operating system:

First, we have to open up the terminal; we can use the shortcut Ctrl + Alt + T or we can manually open it up by searching it in the menu (⋮⋮⋮ icon).

1. To begin, update the packages list using the below command:

The apt command in Linux is in charge of installing, uninstalling, and updating applications on our system. Consider your computer to be a factory, and the apt command to be the factory manager who manages the installation of new equipments, removing obsolete equipments, and updating the desired equipments to the latest versions while keeping track of the equipment names and versions currently in use. So, sudo apt update command is used to update the track of the software versions that helps in installing the newest available software version in your systems.

Note:

sudo (Super User DO) enables a permitted user to run a command as the superuser or another user, depending on the security policy. So, if required, enter your system password to proceed.

OUTPUT:

2. Install the package build-essential using the following command:

If required enter your system password to proceed.

OUTPUT:

Press the Y key and then Enter key once you get the similar output as above on your screen to continue the installation process.

It will take 3-5 minutes to install the build-essential meta-package completely. This process will install C on Linux operating system and we can use the GCC compiler to compile and run our C Programs.

Congratulations! Now you have completed the installation of the build-essential package and the GCC Compiler (C Lanuguage) in Linux.

Note:

We are using Ubuntu 20.04 LTS version in this tutorial. This version of Ubuntu comes with a pre-installed version of the GCC compiler, so you can first check the installed compiler version using the command mentioned in the next section.

For Red Hat Linux / Fedora / CentOS

There is no build-essentials package in Red Hat Linux, Fedora or CentOS but we can install C on Linux through a similar package in these operating systems required for software development. So, if you are using a Red Hat Linux / Fedora / CentOS based Linux operating system, then you have to install the Development Tools package to get the GCC compiler in your system.

1. First update the packages list using the below command as root user:

2. Install Development Tools using the below command as root user:

or

Check the Installed Compiler Version

Now that you have installed the build-essentials/Development Tools package, you can easily check/validate your GCC Compiler version. In this section, you will see how you can check/validate the installed GCC compiler version by using the below command in your Linux terminal:

Note:

--version flag under the gcc command is compatible with almost all the terminal development software in Linux.

OUTPUT:

If you see an output like the above-displayed output on your screen, you are good to go.

Note:

If the output looks like the below output, there was some discrepancy during the installation process. Repeat the steps mentioned above to install build-essential package again.

Creating a C Program

In this section, you will to create a simple C program on Linux using a pre-installed text editor.

There are many Linux terminal commands to create a new file like using the touch, > (redirect operator), cat, echo commands, or terminal editors like vi, vim, or nano. We are using the touch command in this tutorial in the terminal to create an empty C program file. Before creating a new file, you can also change the directory of the terminal to any desired location in your system. We have chosen the Desktop directory in this tutorial. You can change to Desktop directory using cd Desktop (change directory to Desktop) command or you can choose any directory of your choice, just pass the location address of the directory after the cd command. You have to make sure that the C program file is compiled in the same directory as where the C program file is present.

Let's see this in action in Ubuntu 20.04 OS.

Step 1: touch hello.c command in the terminal will create an empty hello.c C program file in the desktop directory.

creating a c program on linux step one

Step 2: Open the hello.c file in the in-built text editor of Linux (or you can use any editor of your choices like Sublime Text or Visual Studio Code) and type the Hello World C program in the editor as given below.

Hello World Program in C

You can also check and run this program here. (IDE by InterviewBit)

OUTPUT:

Hello, World!

creating a c program on linux step two

Congratulations! You're now just one step away from compiling a C program under the Linux operating system. So far, You have created a new file hello.c through the terminal and successfully written the Hello World program in the in-built text editor.

Now let us see how to compile the above C program file in the Linux terminal.

Compiling and Executing the C Program with GCC Compiler

To compile our C Program file, we can use the below command in the terminal (use this command in the same directory where the hello.c file is present):

The above command will generate an executable file with hello as given in the command after -o. We can give any name to the executable file. We don't need to give the same name as the C program file.

Note:

We also have to ensure that the above command is used in the same directory where the hello.c file is present.

Now to run this executable file, we just need to run the below command and the output of the C program will be shown on the terminal screen.

This command will give Hello, World! output on the terminal if you have written the same C program as mentioned in the above section.

Note :

Use this command in the same directory where the hello executable file is present.

Let's see this in action in Ubuntu 20.04 OS.

Step 1: Use gcc hello.c -o hello command to compile the hello.c program and generate a hello executable file. We can see from the image that a hello file is created in the Desktop directory.

creating a c program on linux step three

Step 2: To run the executable file just enter the ./hello command and output will be visible on the terminal screen as shown in the below image.

Note:

Use this command in the same directory where the hello executable file is present. For example, we compile and execute our program file on the Desktop directory.

This command will give Hello, World! output on the terminal if you have written the same C program as mentioned in the above section.

creating a c program on Linux step four

Congratulations!: You have completed the compilation and running process of a C Program under a Linux Operating System.

Conclusion

  • We learned how to install C on Linux.
  • We need to install the build-essential/Development Tools package to install C on Linux and get the GCC Compiler.
  • build-essential meta-package comes with five separate packages that are required during a software compilation process i.e. gcc, g++, libc6-dev , make and dpkg-dev.
  • GCC compiler is used to compile and run a C program on the Linux operating system.
  • We can use the in-build text editor or other editors like Visual Studio Code or Sublime Text to write our C Programs.

Read More: