How to Install GCC on Linux: A Practical Engineer’s Guide
No Linux development node is fully functional without GCC. Whether building bespoke software, troubleshooting legacy binaries, or bootstrapping tools for continuous integration, the GNU Compiler Collection remains an essential component.
Scenario: You’re tasked with compiling a modern C++ application on Ubuntu 22.04 or provisioning a CentOS 7 staging host for automated build pipelines. Is
gcc
already present, and is the version sufficient? Let’s check and, if needed, install (and test) GCC with minimal disruption.
Quick Check: Is GCC Already Present?
Seasoned engineers rarely skip the initial check:
gcc --version
Expected:
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
...
No output or "command not found"?
Plan for install; the system isn’t ready for C-family software builds.
Installation: Major Linux Distributions
Installation is invariably tied to system package managers. Each brings subtle differences.
Ubuntu/Debian
-
Synchronize package index (always safe; outdated indexes often cause 404s):
sudo apt update
-
Install
build-essential
(not justgcc
; bringsg++
,make
, headers):sudo apt install build-essential
- To install just GCC:
sudo apt install gcc
Gotcha:
Fresh minimal containers may lack basic build tooling—using onlygcc
may leave outlibc6-dev
andmake
. - To install just GCC:
-
Verify:
gcc --version
- Confirm major version:
gcc-10
or higher is recommended for modern codebases using C++17+.
- Confirm major version:
Fedora
Fedora’s process is direct. For C/C++:
sudo dnf install gcc gcc-c++
-
Group install for build environments:
sudo dnf groupinstall "Development Tools"
This installs
make
,gdb
, and other critical build tools, not merely the C/C++ compilers.
CentOS / RHEL
CentOS 7 and up (including Stream and RHEL variants) often default to older compiler versions—somewhat risky for new codebases. Still, for “stock” installs:
sudo yum groupinstall "Development Tools"
or, on newer releases:
sudo dnf groupinstall "Development Tools"
-
Note:
On CentOS 7, default GCC is v4.8—ancient by modern standards. For contemporary C++ projects (e.g., requiring C++17), activate Software Collections (scl
) or consider devtoolset:sudo yum install centos-release-scl sudo yum install devtoolset-11-gcc devtoolset-11-gcc-c++ scl enable devtoolset-11 bash
Sanity Test: Compile a Program
Create and compile a trivial “Hello, world” (save as hello.c
):
#include <stdio.h>
int main() { printf("Hello, World\n"); return 0; }
Build:
gcc -Wall -O2 hello.c -o hello
-Wall
enables common warnings (miss them and common bugs remain hidden)-O2
is typical for production-grade builds; omit for debugging
Run:
./hello
Expected:
Hello, World
Troubleshooting & Side Notes
Issue/Observation | Explanation or Command |
---|---|
gcc: command not found | Package not installed or $PATH alteration. Confirm via which gcc . |
Multiple gcc versions coexist | On Debian/Ubuntu, manage via sudo update-alternatives --config gcc . |
Permission denied (during install) | Installation requires sudo or root privileges. For ephemeral containers, note user context. |
Shared library dependencies missing | Often means headers/libs uninstalled. On Debian, ensure libc6-dev is present. Also needed: build-essential |
Need newer GCC on old distros | Use scl /devtoolset on CentOS 7+. Alternatively, build GCC from source (resource intensive). |
-
Known issue:
Sometimesapt
installations, especially in Docker, produce temporary DNS resolution problems—retry or use a mirror. -
Alternative:
For version pinning or reproducibility, consider containers (e.g.,gcc:latest
from Docker Hub).
Closing Thoughts
GCC installation is routine—but details matter. Distributions differ; minimal setups require extra awareness. Always validate version, and don’t neglect the rest of the toolchain.
For persistent build environments or CI/CD, freeze compiler versions to avoid subtle build regressions.
Practical tip:
For one-off, isolated compiles, the official GCC Docker image saves time:
docker run --rm -v "$PWD":/src -w /src gcc:13 bash -c "gcc -o hello hello.c"
Further questions or notes on toolchain quirks? Capture practical findings before moving on. No two environments are exactly alike.