0
0
CHow-ToBeginner · 3 min read

How to Install GCC on Linux for C Programming

To install gcc on Linux, open the terminal and run sudo apt update followed by sudo apt install gcc on Debian-based systems. For other Linux distributions, use the appropriate package manager like yum or dnf to install gcc.
📐

Syntax

To install gcc on Linux, use your system's package manager with the following commands:

  • Debian/Ubuntu: sudo apt update updates package lists, then sudo apt install gcc installs the compiler.
  • Fedora: Use sudo dnf install gcc.
  • CentOS/RHEL: Use sudo yum install gcc.

This installs the GNU Compiler Collection, which includes the C compiler.

bash
sudo apt update
sudo apt install gcc
💻

Example

This example shows how to install gcc on a Debian-based Linux system and compile a simple C program.

bash
# Step 1: Install gcc
sudo apt update
sudo apt install gcc

# Step 2: Create a C program file
cat <<EOF > hello.c
#include <stdio.h>

int main() {
    printf("Hello, Linux GCC!\n");
    return 0;
}
EOF

# Step 3: Compile the program
gcc hello.c -o hello

# Step 4: Run the program
./hello
Output
Hello, Linux GCC!
⚠️

Common Pitfalls

Some common mistakes when installing or using gcc on Linux include:

  • Not running sudo apt update before installing, which can cause package not found errors.
  • Trying to compile C code without gcc installed, leading to command not found errors.
  • Using the wrong package manager for your Linux distribution.
  • Not having build-essential tools installed on Debian/Ubuntu, which may be needed for compiling complex programs.

Always check your Linux distribution and use the correct commands.

bash
## Wrong way (missing update):
sudo apt install gcc

## Right way:
sudo apt update
sudo apt install gcc
📊

Quick Reference

Linux DistributionInstall Command
Debian/Ubuntusudo apt update && sudo apt install gcc
Fedorasudo dnf install gcc
CentOS/RHELsudo yum install gcc
Arch Linuxsudo pacman -S gcc

Key Takeaways

Use your Linux distribution's package manager to install gcc with sudo privileges.
Always run package list update commands before installing new software.
Check your Linux version to use the correct install command.
After installation, compile C programs using the gcc command.
Common errors come from missing updates or wrong package managers.