To get GCC working on Linux, you’ll need to use your distribution’s package manager to install the build-essential package. This guide walks you through exactly how to install GCC on Linux, covering every major distro and common pitfalls.
GCC (GNU Compiler Collection) is essential for compiling C, C++, and other languages on Linux. Whether you’re a developer, student, or hobbyist, getting it set up correctly saves hours of frustration later.
How To Install Gcc On Linux
Understanding GCC And Its Importance
GCC is the backbone of open-source software development on Linux. It translates human-readable code into machine-executable binaries. Without it, you cannot compile most software from source.
Most Linux distributions do not include GCC by default. You must install it manually using the package manager. The exact command varies depending on your distribution.
Prerequisites Before Installation
- A working internet connection
- Sudo or root access to your system
- Basic familiarity with terminal commands
- Updated package repository cache
Before installing, update your package list. This ensures you get the latest version available for your distribution.
Installing GCC On Ubuntu And Debian
Ubuntu and Debian use the APT package manager. The easiest way to install GCC is through the build-essential meta-package.
- Open a terminal (Ctrl+Alt+T)
- Update package lists:
sudo apt update - Install build-essential:
sudo apt install build-essential - Verify installation:
gcc --version
The build-essential package installs GCC, G++, make, and other development tools. This is the recommended method for most users.
Alternative: Installing GCC Individually
If you only need GCC (not G++ or make), you can install just the compiler:
sudo apt install gcc
This installs a minimal setup. However, many projects require additional tools like make and binutils.
Installing Specific GCC Versions
Sometimes you need an older or newer GCC version. Ubuntu repositories often include multiple versions.
sudo apt install gcc-10 g++-10
You can then switch between versions using update-alternatives or by calling the specific binary (e.g., gcc-10).
Installing GCC On Fedora And RHEL
Fedora and Red Hat Enterprise Linux use the DNF package manager. The process is similar but uses different package names.
- Update repositories:
sudo dnf update - Install GCC and tools:
sudo dnf install gcc gcc-c++ make - Check version:
gcc --version
On RHEL, you may need to enable the EPEL repository first. Run sudo dnf install epel-release before installing GCC.
Installing Development Tools Group
Fedora offers a “Development Tools” group that includes GCC and many other tools:
sudo dnf groupinstall "Development Tools"
This is equivalent to Ubuntu’s build-essential package. It installs everything you need for compiling software.
Installing GCC On Arch Linux
Arch Linux uses Pacman. The base-devel group includes GCC and essential build tools.
- Update system:
sudo pacman -Syu - Install base-devel:
sudo pacman -S base-devel - Verify:
gcc --version
Arch’s base-devel is mandatory for building packages from the AUR. Most Arch users install it immediately after setting up the system.
Installing GCC Without Base-Devel
If you only want GCC itself:
sudo pacman -S gcc
This installs the compiler but not make or other tools. You might need them later.
Installing GCC On OpenSUSE
OpenSUSE uses Zypper. The pattern devel_basis includes GCC and related tools.
- Refresh repositories:
sudo zypper refresh - Install devel_basis:
sudo zypper install -t pattern devel_basis - Check installation:
gcc --version
Alternatively, install GCC individually: sudo zypper install gcc gcc-c++
Installing GCC On Gentoo
Gentoo uses Portage. GCC is typically installed during the initial system setup, but you can reinstall or update it.
- Sync repositories:
emerge --sync - Install GCC:
emerge sys-devel/gcc - Set default version:
gcc-config -lthengcc-config
Gentoo users often compile GCC from source with custom flags. This process can take a long time.
Verifying Your GCC Installation
After installation, verify GCC works correctly. Run these commands in terminal:
gcc --version
g++ --version
make --version
You should see version numbers for each tool. If any command returns “command not found,” the installation failed.
Writing A Test Program
Create a simple C program to confirm compilation works:
- Create file:
nano test.c - Add code:
#include <stdio.h> int main() { printf("GCC works!\n"); return 0; } - Compile:
gcc test.c -o test - Run:
./test
If you see “GCC works!” printed, your installation is successful.
Troubleshooting Common Issues
Package Not Found Errors
If your package manager cannot find GCC, update your repositories first. On Ubuntu, run sudo apt update. On Fedora, sudo dnf update.
Sometimes the package name differs. For example, on older Debian versions, you might need gcc-4.9 instead of just gcc.
Permission Denied
Always use sudo when installing system packages. If you get permission errors, you likely forgot sudo.
If sudo is not configured, log in as root: su - then run the install command.
Missing Dependencies
GCC requires several libraries. The package manager handles this automatically. If you see dependency errors, run a full system update first.
On Ubuntu: sudo apt --fix-broken install
On Fedora: sudo dnf distro-sync
Multiple GCC Versions Conflict
If you install multiple GCC versions, the system might use the wrong one. Use update-alternatives to set the default:
sudo update-alternatives --config gcc
This lets you choose which version to use system-wide.
Compiling Your First Program With GCC
Now that GCC is installed, let’s compile something useful. Create a file named hello.c with this content:
#include <stdio.h>
int main() {
printf("Hello, Linux!\n");
return 0;
}
Compile it: gcc hello.c -o hello
Run it: ./hello
You should see “Hello, Linux!” printed. This confirms GCC works and you can compile programs.
Compiling C++ Programs
For C++ code, use g++ instead:
g++ hello.cpp -o hello
The process is identical, just the compiler name changes.
Updating GCC To A Newer Version
GCC releases new versions regularly. To update, use your package manager:
Ubuntu/Debian: sudo apt upgrade gcc
Fedora: sudo dnf update gcc
Arch: sudo pacman -Syu (updates all packages)
Sometimes the latest GCC is not in official repositories. In that case, you may need to add a PPA or compile from source.
Adding A PPA For Latest GCC (Ubuntu)
Ubuntu’s official repositories may lag behind. The Ubuntu Toolchain PPA provides newer versions:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install gcc-12 g++-12
This installs GCC 12 alongside your existing version.
Uninstalling GCC
If you need to remove GCC, use the package manager:
Ubuntu: sudo apt remove build-essential
Fedora: sudo dnf remove gcc gcc-c++
Arch: sudo pacman -R base-devel
Be careful: removing GCC breaks many development tools. Only uninstall if you are sure you don’t need it.
Frequently Asked Questions
What Is The Difference Between GCC And G++?
GCC compiles C code, while G++ compiles C++ code. Both are part of the GNU Compiler Collection. Installing build-essential gives you both.
Do I Need GCC If I Use An IDE?
Most IDEs (like VS Code or Eclipse) rely on an external compiler. You still need GCC installed on your system for the IDE to compile code.
Can I Install GCC Without Internet?
Yes, but it’s complicated. You can download the .deb or .rpm packages on another machine and transfer them via USB. Then install manually using dpkg or rpm.
Why Does GCC Installation Fail On My System?
Common reasons include outdated repositories, missing dependencies, or disk space issues. Run sudo apt update and check disk space with df -h.
Is GCC The Same On All Linux Distributions?
The GCC source code is identical, but package names and versions differ. The installation method varies, but the compiler itself works the same way everywhere.
Additional Tips For GCC Users
Always compile with warnings enabled: gcc -Wall -Wextra program.c -o program. This catches common mistakes.
Use the -O2 flag for optimized code: gcc -O2 program.c -o program. This makes your programs run faster.
For debugging, add the -g flag: gcc -g program.c -o program. This includes debug symbols for GDB.
Learn to use Makefiles. They automate compilation for multi-file projects. The make tool is included in build-essential.
Check the official GCC documentation for advanced options. The manual is extensive and well-written.
Conclusion
Installing GCC on Linux is straightforward once you know your distribution’s package manager. Whether you use Ubuntu, Fedora, Arch, or any other distro, the process takes only a few minutes.
Remember to verify your installation with a test program. This confirms everything works before you start serious development.
With GCC installed, you can compile C, C++, Fortran, and other languages. You are now ready to build software from source, contribute to open-source projects, or develop your own applications.
If you encounter issues, refer to the troubleshooting section above. Most problems are easy to fix with a quick package update or version check.
Now go ahead and compile something awesome. Your Linux system is fully equipped for development.