To install GCC on Ubuntu, open a terminal and run sudo apt update followed by sudo apt install gcc. This installs the GNU C compiler; for C++ support, install g++ as well. The rest of this guide covers prerequisites, alternative installation methods, and common troubleshooting.
What Is GCC and Why Install It?
GCC (GNU Compiler Collection) is a free, open-source compiler system that supports C, C++, Objective-C, Fortran, Ada, and other languages. It is the default compiler on most Linux distributions, including Ubuntu. Developers install GCC to compile source code into executable programs, build software from source, or learn programming. Ubuntu’s default repositories contain pre-built GCC packages, making installation straightforward.
Prerequisites and System Preparation
Before installing GCC, ensure your system meets these basic requirements:
- An Ubuntu installation with internet access (wired or Wi-Fi).
- A user account with
sudoprivileges (needed to install system packages). - At least 500 MB of free disk space for the compiler and related tools.
- An up-to-date package list (run
sudo apt updatefirst).
Most Ubuntu installations already have apt and the necessary repositories configured. If you are using a minimal installation, you may also need build-essential, which includes GCC, G++, make, and other tools commonly required to compile software.
Method 1: Install GCC via APT (Recommended)
The easiest and most reliable method is using Ubuntu’s package manager. Follow these steps:
- Open a terminal (Ctrl+Alt+T).
- Update the package index:
sudo apt update. - Install GCC:
sudo apt install gcc. - For C++ support, install G++:
sudo apt install g++. - Verify the installation:
gcc --version.
This installs the default GCC version available in your Ubuntu release’s repositories. For example, Ubuntu 22.04 LTS ships with GCC 11, while Ubuntu 24.04 LTS includes GCC 13. To install a specific version, you can specify it, e.g., sudo apt install gcc-12.
Method 2: Install build-essential (Full Toolchain)
If you plan to compile C/C++ programs that use make or need standard libraries and headers, install the build-essential meta-package:
sudo apt install build-essential
This installs GCC, G++, GNU Make, dpkg-dev, and other essential tools. It is the standard recommendation for developers who want a complete compilation environment. After installation, verify with gcc --version and make --version.
Method 3: Install a Specific GCC Version
Sometimes you need a different GCC version for compatibility with a project or to test new features. Ubuntu’s repositories often contain multiple GCC versions. To see available versions, run:
apt list --all-versions gcc
Then install a specific version, for example:
sudo apt install gcc-10 g++-10
You can invoke that version directly using gcc-10 or g++-10. To make it the default, use update-alternatives to configure the system’s default compiler.
Verifying the Installation and Compiling Your First Program
After installation, confirm GCC works correctly:
- Check the version:
gcc --version. - Create a simple C file:
nano hello.c. - Write the following code:
#include <stdio.h> int main() { printf("Hello, GCC!\n"); return 0; } - Compile it:
gcc hello.c -o hello. - Run the executable:
./hello.
If you see “Hello, GCC!” the installation is working. For C++ programs, use g++ instead.
Common Installation Issues and Fixes
While installing GCC is usually smooth, you might encounter these problems:
- Package not found: Run
sudo apt updateto refresh the package list. If the issue persists, check your/etc/apt/sources.listfile for valid repositories. - Permission denied: Use
sudobefore the install command, or ensure your user is in thesudogroup. - Broken dependencies: Run
sudo apt --fix-broken installto repair dependency issues. - GCC installed but not found: Some versions are installed as
gcc-12rather thangcc. Usels /usr/bin/gcc*to see available binaries. - Compiler errors during compilation: These are usually code errors, not installation problems. Double-check your syntax and include correct headers.
Alternatives to Installing GCC
If you prefer not to install GCC directly, consider these alternatives:
- Clang: A C/C++ compiler that is often faster and provides better error messages. Install with
sudo apt install clang. - Online compilers: Websites like Compiler Explorer or OnlineGDB let you compile code without local installation.
- Using a container: Run a Docker container with GCC pre-installed, e.g.,
docker run --rm -it gcc:latest. - Ubuntu’s default package: If you only need to run pre-compiled software, you don’t need GCC at all.
Frequently Asked Questions
Do I need to install GCC separately for C and C++?
No. The gcc package installs the C compiler, and the g++ package installs the C++ compiler. Both are part of the GNU Compiler Collection. Installing build-essential gives you both.
How do I update GCC to the latest version?
Run sudo apt update and then sudo apt upgrade to update all packages, including GCC. To get a newer version than what your Ubuntu release provides, you may need to add a third-party repository like the Ubuntu Toolchain PPA, but that is not officially recommended for production systems.
Can I have multiple GCC versions installed simultaneously?
Yes. You can install multiple versions, and each will have a versioned binary (e.g., gcc-11, gcc-12). Use update-alternatives to set a default if needed.
Why do I get “gcc: command not found” after installation?
This usually means the binary is not in your PATH. Check if it is installed as gcc-12 instead. If so, either use that name or create a symlink: sudo ln -s /usr/bin/gcc-12 /usr/bin/gcc.
Is GCC free to use?
Yes, GCC is free software distributed under the GNU General Public License (GPL). You can use, modify, and redistribute it freely.
Does installing GCC affect my system’s performance?
No. GCC is a compiler; it only runs when you invoke it. It does not run in the background or consume resources when not in use.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Good Password: How to Create One
- Remove Write Protection on External Hard Drive
- How to Disable Chkdsk at Windows Startup
- Restore WiFi and Secure Your Netgear Router After Reset
- Fix Error 0x80004005: Top Easy Ways That Work
- How to Cool Down an Overheating Laptop