Learn The Web in about a week!

Learning should be fun and quick; no need to spend ages in just one single course.

Start learning  

#34 Tooling
11 mins   /

How to set up C on Linux (Ubuntu)?

Quickly learn what exactly are GCC and Clang and how to set them up to compile C programs.

C is a compiled language. This means that before you can run a C program, the source code needs to translated, i.e. compiled, into machine code in binary. This process is performed by something called a compiler.

There are numerous compilers out there for C depending on the operating system under question. Some compilers are oriented towards Windows whereas others are oriented towards Unix-like operating systems such as Linux and MacOS.

Theoretically speaking, even you could create a C compiler yourself once you have an official specification of the language with you. But of course because this requires a lot of resources, and that's why it's usually done by large engineering teams.

In this article, I'll cover how to set up a C compiler on Ubuntu, which is a Linux distribution (fancily called a Linux distro). The two compilers I'll explore are GCC and Clang.

What is GCC?

GNU Compiler Collection (GCC) is a whole array of compilers that can understand a wide range of languages ranging from the well-known C, C++, Go, to the slightly less known Ada, Modula-2, and D (and many more). For now, of course, what concerns us is the C language.

Being part of the GNU project, GCC is completely free to use. You can find more details on its official website.

As part of this GCC project, there is a program called gcc which compiles C programs. This naming might be confusing because GCC refers to the whole toolchain yet gcc is only capable of compiling C. Actually, this naming mismatch has to do with history.

Difference between GCC and the gcc program

In the early days, the gcc program referred to the GNU C compiler, hence abbreviated as "gcc." The gcc command still abides by the same principle — it compiles only C. Later on, compilers were built for many more languages — for example, g++ for C++ and gnat for Ada — and grouped under one project, named GCC (for "GNU Compiler Collection").

Therefore, GCC (the entire collection of compilers) and gcc (the C compiler) are two different things. So when you say something like "I'm installing GCC to compile C," it's well-understood that you're referring to one of the compiler programs from the GCC catalog; not the entire catalog itself.

Setting up GCC

There are a plethora of ways of setting up GCC on your Linux operating system. I've covered the two simplest below.

Installing build-essential

By far, the easiest and quickest way to set up GCC is to use Linux's standard package repository tool apt to install the build-essential package (and its associated libraries). build-essential is a complete package of all the necessary build tools around development with C.

It's not just the C compiler but a multitude of other things, for e.g. the make command which is useful in order to build C programs, and the g++ command for executing C++ code (once you learn C, you're not really far away from C++).

Open the terminal and run the following:

 sudo apt install build-essential

Upon completion, you can check gcc by running this:

 gcc --version
gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
Copyright (C) 2023 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.

If you get an output similar to this (of course your program's version might be different), you're all good to start writing and running C programs!

Install gcc only

Although build-essential is the way to go, for some reason, if you're only interested in the gcc command and nothing else with it, you can just install it from the package repository. Go ahead and run the following:

 sudo apt install gcc

As before, once this command runs to completion, test out gcc with a quick version check:

 gcc --version
gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0
Copyright (C) 2023 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.

What is Clang?

As I stated at the start of this article, GCC isn't the only C compiler. There are other equally good options too. Clang is one such example. Built for the LLVM project, Clang is a collection of compilers for multiple languages like C and C++, just like GCC. You can learn more about it from its official website.

Perhaps, the best thing about Clang is its highly expressive error messaging and it's great support for IDE integration which can be really handy when you're debugging a complex C application — debugging is a nightmare with cryptic error messages.

In fact, one of Clang's motivation was to improve error messaging which wasn't that elegant in GCC (although in recent years, GCC has really improved in this area). Clang ensures that it's easy enough for you to use it if you're already familiar with the gcc command (therefore, it offers many identical features when compiling C).

Setting up Clang

To set up Clang, again no need to worry when you have Linux's official repository with you. Just launch the terminal and enter the following command:

 sudo apt install clang

Wait for a while to let clang install and once done, check it using... you got it... clang --version:

 clang --version
Ubuntu clang version 18.1.3 (1ubuntu1)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

If you get an output similar to this, you're all good to clang. (Wait a minute... that didn't make sense.)

Bilal Adnan

Hi there! 👋 I'm the founder of Codeguage — basically the guy who's trying to make life easy for absolute beginners entering into computer science. You can follow me on LinkedIn or Medium to stay up-to-date with my conversations.