C Notes
Complete guide to install GCC C compiler on Linux using apt, dnf, and pacman. Covers Ubuntu, Fedora, Arch Linux, and other distributions with verification steps and troubleshooting.
Linux is the most natural environment for C programming — after all, the Linux kernel itself is written in C. Most Linux distributions either come with GCC pre-installed or make it incredibly easy to install through their package manager. In this guide, we'll cover how to install and verify GCC on all major Linux distributions.
Check if GCC is Already Installed
Before installing anything, check if GCC is already available on your system:
gcc --versionIf you see version information, you're already set! If you get "command not found," follow the installation steps below for your distribution.
You can also check with:
which gcc/usr/bin/gcc
If this returns a path, GCC is installed and ready to use.
Ubuntu / Debian / Linux Mint
These Debian-based distributions use the apt package manager.
Install build-essential
The build-essential package is a meta-package that installs everything you need for C/C++ development:
sudo apt update
sudo apt install build-essentialThis single command installs:
- gcc — The GNU C Compiler
- g++ — The GNU C++ Compiler
- make — Build automation tool
- libc6-dev — C standard library development headers
- dpkg-dev — Package development tools
Install Additional Useful Tools
sudo apt install gdb valgrind cmake- gdb — GNU Debugger for debugging your programs
- valgrind — Memory leak detection and profiling tool
- cmake — Cross-platform build system generator
Verify Installation
gcc --version
make --version
gdb --versiongcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0 Copyright (C) 2023 Free Software Foundation, Inc. This is free software; see the source for copying conditions.
Fedora / RHEL / CentOS / Rocky Linux
These Red Hat-based distributions use dnf (or yum on older versions).
Install Development Tools Group
sudo dnf groupinstall "Development Tools"Or install GCC individually:
Install Additional Tools
sudo dnf install gdb valgrind cmakeFor Older RHEL/CentOS (using yum)
Verify Installation
gcc --versiongcc (GCC) 13.2.1 20231205 (Red Hat 13.2.1-6) Copyright (C) 2023 Free Software Foundation, Inc.
Arch Linux / Manjaro
Arch-based distributions use the pacman package manager.
Install Base Development Group
sudo pacman -S base-develWhen prompted to select packages, press Enter to install all (recommended). This includes GCC, Make, and other essential tools.
Install GCC Only
If you just want the compiler:
sudo pacman -S gccInstall Additional Tools
sudo pacman -S gdb valgrind cmakeVerify Installation
gcc --versiongcc (GCC) 13.2.1 20230801 Copyright (C) 2023 Free Software Foundation, Inc.
openSUSE
openSUSE uses zypper as its package manager.
Or install packages individually:
Alpine Linux
Alpine uses apk and is commonly used in Docker containers:
apk add build-baseThis installs GCC, G++, Make, and musl-dev (Alpine uses musl instead of glibc).
Installing a Specific GCC Version
Sometimes you need a specific GCC version for a project or course requirement.
On Ubuntu/Debian
On Fedora
# Install a specific version (if available)
sudo dnf install gcc-toolset-13
scl enable gcc-toolset-13 bashComplete Verification Test
After installation on any distribution, run this comprehensive test:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
printf("=== GCC Installation Test ===\n\n");
// Basic output
printf("1. printf works: Hello, Linux!\n");
// Math operations
printf("2. Math library: sqrt(256) = %.0f\n", sqrt(256));
// Memory allocation
int *arr = (int *)malloc(5 * sizeof(int));
if (arr != NULL) {
printf("3. Dynamic memory: malloc works\n");
free(arr);
}
// Data type sizes
printf("4. sizeof(int) = %zu bytes\n", sizeof(int));
printf("5. sizeof(long) = %zu bytes\n", sizeof(long));
printf("6. sizeof(void*) = %zu bytes\n", sizeof(void*));
printf("\n=== All tests passed! ===\n");
return 0;
}Save as test.c and compile:
gcc -Wall -Wextra test.c -o test -lm
./test=== GCC Installation Test === 1. printf works: Hello, Linux! 2. Math library: sqrt(256) = 16 3. Dynamic memory: malloc works 4. sizeof(int) = 4 bytes 5. sizeof(long) = 8 bytes 6. sizeof(void*) = 8 bytes === All tests passed! ===
Notice that on 64-bit Linux, long is 8 bytes (unlike Windows where it's 4 bytes). This is an important difference to be aware of when writing portable code.
Understanding What Gets Installed
Here's what the development packages typically include:
| Component | Purpose | Location |
|---|---|---|
gcc | C compiler binary | /usr/bin/gcc |
cc | Symbolic link to gcc | /usr/bin/cc |
cpp | C preprocessor | /usr/bin/cpp |
as | GNU Assembler | /usr/bin/as |
ld | GNU Linker | /usr/bin/ld |
ar | Archive tool (static libs) | /usr/bin/ar |
make | Build automation | /usr/bin/make |
| C headers | Standard library headers | /usr/include/ |
| C library | Shared libraries | /usr/lib/ or /lib/ |
Setting Up a Project Directory
A good practice is to create a dedicated workspace for your C projects:
# Create project directory structure
mkdir -p ~/c-projects/hello
cd ~/c-projects/hello
# Create your source file
nano hello.cFor more complex projects:
mkdir -p ~/c-projects/myproject/{src,include,build,tests}This gives you a clean structure:
Using Make for Build Automation
Create a simple Makefile for your projects:
CC = gcc
CFLAGS = -Wall -Wextra -g
TARGET = program
$(TARGET): main.c
$(CC) $(CFLAGS) main.c -o $(TARGET)
clean:
rm -f $(TARGET)
run: $(TARGET)
./$(TARGET)Now you can build with:
make # Compile
make run # Compile and run
make clean # Remove executableTroubleshooting
"Permission denied" When Running Executable
chmod +x ./program
./programOr simply recompile — GCC sets the executable permission automatically.
Missing Header Files
If you get errors about missing headers like stdio.h:
# Ubuntu/Debian
sudo apt install libc6-dev
# Fedora
sudo dnf install glibc-develMultiple GCC Versions Conflict
Use update-alternatives to manage multiple versions:
sudo update-alternatives --config gccThis shows a menu where you can select the default version.
"Cannot find -lm"
Install the math library development files:
# Ubuntu/Debian
sudo apt install libc6-dev
# Fedora
sudo dnf install glibc-develInterview Questions
Q1: What is the build-essential package in Ubuntu?
build-essential is a meta-package in Debian-based distributions that installs the minimal set of packages needed to compile software from source. It includes GCC (C compiler), G++ (C++ compiler), Make (build tool), libc development headers, and dpkg-dev. It's called "essential" because almost every source code compilation requires these tools.
Q2: How do you manage multiple GCC versions on Linux?
On Debian/Ubuntu, you use the update-alternatives system. First, register each version with sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-XX priority. Then switch between them using sudo update-alternatives --config gcc. On Fedora/RHEL, you can use Software Collections (SCL) with scl enable gcc-toolset-XX bash.
Q3: What is the difference between gcc and cc on Linux?
On most Linux systems, cc is a symbolic link that points to the system's default C compiler, which is typically gcc. The distinction exists for historical reasons and POSIX compatibility — scripts that use cc will work regardless of which specific compiler is installed. You can check with ls -la /usr/bin/cc to see where it points.
Q4: Why do we need to link with -lm for math functions on Linux?
On Linux with GCC, the math library (libm) is separate from the C standard library (libc) for historical and performance reasons. Functions like sqrt(), sin(), cos(), and pow() are implemented in libm, which must be explicitly linked with -lm. This separation allows programs that don't use math functions to avoid loading the math library, saving memory.
Q5: What is the difference between /usr/include and /usr/local/include?
/usr/include contains header files installed by the system's package manager (apt, dnf, etc.) and is managed by the distribution. /usr/local/include is for header files installed manually by the system administrator (e.g., software compiled from source). GCC searches both directories by default, but /usr/local/include takes precedence to allow local overrides of system headers.
Summary
- Most Linux distributions include GCC or make it easy to install via package managers
- Use
build-essential(apt), "Development Tools" group (dnf), orbase-devel(pacman) - Always verify installation with
gcc --versionafter installing - The
-lmflag is needed when using math functions likesqrt()andsin() - Use
update-alternativesto manage multiple GCC versions - Install
gdbfor debugging andvalgrindfor memory leak detection - Create organized project directories with separate
src/,include/, andbuild/folders - Use Makefiles to automate the build process for multi-file projects
- Linux is the ideal platform for learning C due to its Unix heritage and excellent tooling
With GCC installed on your Linux system, you have access to one of the most powerful development environments available. The combination of GCC, GDB, Make, and the command line gives you everything professional C developers use in production.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Install C Compiler on Linux - GCC Setup Guide for All Distros.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, setup, install, linux, install c compiler on linux - gcc setup guide for all distros
Related C Programming Topics