1. Introduction
What is GCC?
GCC (GNU Compiler Collection) is an open-source compiler that can compile multiple programming languages, including C and C++. It is widely used as the standard compiler for Linux distributions.
Main Features of GCC:
- Supports multiple languages, including C, C++, Fortran, and Java.
- Open-source and freely available for anyone.
- Offers fast and reliable compilation.
Why Use GCC on Ubuntu?
- Available as a standard package
GCC is included in Ubuntu’s repository by default, making it easy to install. - Extensive support and documentation
With a large global user base, there is a wealth of information available for troubleshooting and customization. - Completely free to use
Allows you to build a powerful development environment without any cost. - Easy to customize
Supports multiple versions, enabling you to set up the best environment for your projects.
Summary
In this article, we introduced GCC and its benefits when used on Ubuntu. GCC is a powerful, multi-language, and free-to-use compiler that can be easily installed in an Ubuntu environment.

2. Preparation
Updating the System and Checking Dependencies
First, update your Ubuntu package list to ensure everything is up to date. This helps prevent errors during installation.
1. Update the system
sudo apt update
sudo apt upgrade
sudo apt update
: Updates the package list.sudo apt upgrade
: Upgrades installed packages to their latest versions.
Important Notes:
- The update process may take a few minutes.
- If prompted to restart the system after updating, restart it to apply changes.
Checking Development Tools
To install GCC, some essential development tools are required. Run the following command to install them in advance.
sudo apt install build-essential
This command installs fundamental development tools, including GCC.
Example of installed packages:
- gcc (C compiler)
- g++ (C++ compiler)
- make (Build tool)
Verifying the Installation
To check if GCC is installed and verify its version, use the following command:
gcc --version
Example Output:
gcc (Ubuntu 9.4.0-1ubuntu1) 9.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
If this output appears, it means GCC has been successfully installed.
Summary of Preparation
At this point, you have completed the necessary preparations for installing GCC.
- Updated and upgraded the system to the latest state.
- Installed the required packages for the development environment.
- Checked the installation status and version of GCC.
3. Installing GCC
Basic Installation Steps
In Ubuntu, GCC can be easily installed from the official repository. Follow these steps:
- Install the build-essential package
sudo apt install build-essential
This command installs GCC, G++, and other essential development tools.
- Confirm installation progress
If prompted with “Do you want to continue? (Y/n),” type “Y” and press Enter.
Verifying the Installation
Once the installation is complete, check the GCC version to confirm the installation.
gcc --version
Example Output:
gcc (Ubuntu 9.4.0-1ubuntu1) 9.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
If the version information appears, GCC is installed successfully.
4. Basic Usage of GCC
Creating and Compiling a Simple Program
- Create a sample program
Let’s start by creating a simple “Hello, World!” program.
nano hello.c
Once the editor opens, enter the following code:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
After entering the code, press Ctrl + X to save, then press Y and hit Enter.
Compiling the Program
Next, use GCC to compile the program.
gcc hello.c -o hello
Explanation of the command:
gcc
: The compiler command.hello.c
: The source code file to be compiled.-o hello
: Specifies the output file name as “hello”.
Running the Compiled Program
Run the compiled program with the following command:
./hello
Example Output:
Hello, World!
If this output appears, your program has been successfully compiled and executed.
Handling Errors
- Syntax Errors
Example error message:
hello.c: In function ‘main’:
hello.c:3:5: error: expected ‘;’ before ‘return’
return 0;
Solution:
The error message points to the problematic line (e.g., line 3). Check your code and correct any syntax mistakes.
- Compiler Not Found
Example error message:
gcc: command not found
Solution:
GCC might not be installed. Reinstall it using:
sudo apt install build-essential
Optimization Options
GCC allows optimization options to improve the performance of compiled programs.
Example: Specifying Optimization Level
gcc -O2 hello.c -o hello
-O1
: Basic optimization.-O2
: Higher-level optimization.-O3
: Maximum optimization (prioritizes speed).
Summary
In this section, we covered the process of creating, compiling, and running a simple program using GCC.
Key Takeaways:
- How to write and compile a sample program.
- How to troubleshoot common compilation errors.
- How to use optimization options to improve performance.

5. Managing Multiple Versions of GCC
Installing Multiple Versions
Ubuntu allows you to install multiple versions of GCC simultaneously. Follow these steps:
- Check Available Versions
sudo apt search gcc-
This command lists all available GCC versions in the repository.
Example Output:
gcc-9 - GNU C compiler
gcc-10 - GNU C compiler
gcc-11 - GNU C compiler
- Install Specific Versions
For example, install GCC 9 and GCC 10:
sudo apt install gcc-9 gcc-10
Switching Between Versions
Use the update-alternatives
command to switch between different GCC versions.
- Register Installed Versions
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100
In this example, GCC 10 is set as the default (priority 100).
- Choose a Version
sudo update-alternatives --config gcc
Example Output:
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/gcc-10 100 auto mode
1 /usr/bin/gcc-9 90 manual mode
2 /usr/bin/gcc-10 100 manual mode
Press <enter> to keep the current choice[*], or type selection number:
Select the desired version by entering the corresponding number.
Summary
In this section, we covered how to install and switch between multiple versions of GCC using update-alternatives
.
Key Takeaways:
- Install multiple GCC versions and manage them with
update-alternatives
. - Switch between versions as needed for different projects.

6. Troubleshooting
Common Installation Errors and Solutions
Error 1: Package Not Found
E: Unable to locate package build-essential
Solution:
sudo apt update
sudo apt upgrade
sudo apt install build-essential
If the issue persists, enable the universe
repository:
sudo add-apt-repository universe
sudo apt update
Common Compilation Errors and Solutions
Error 1: Compiler Not Found
gcc: command not found
Solution:
sudo apt install gcc
If installed but not recognized, check symbolic links:
sudo ln -s /usr/bin/gcc-10 /usr/bin/gcc
Summary
This section covered common installation and compilation issues and their solutions.
7. FAQ Section
How Can I Install the Latest Version of GCC?
Question:
I want to install the latest version of GCC, but the default repository only has older versions. How can I install the latest version?
Answer:
You can install the latest version of GCC by adding a PPA repository.
- Add the PPA repository:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
- Update the package list:
sudo apt update
- Install the latest GCC version:
sudo apt install gcc-12
- Verify the installed version:
gcc --version
How Do I Uninstall GCC?
Question:
I want to uninstall GCC. What are the necessary steps?
Answer:
Run the following commands to remove GCC:
sudo apt remove gcc
sudo apt autoremove
To remove additional development tools:
sudo apt remove build-essential
What If I Can’t Select the Latest GCC Version?
Question:
I tried using update-alternatives --config gcc
, but it only lists old versions. How can I add the latest version?
Answer:
Manually add the new version:
- Install the required version.
sudo apt install gcc-12
- Add it to the alternatives list.
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120
- Select the version.
sudo update-alternatives --config gcc
How to Fix Dependency Errors During Installation?
Question:
I encountered dependency errors while installing GCC. How can I fix them?
Answer:
Dependency errors usually occur when your system is not up to date. Run these commands:
sudo apt update
sudo apt upgrade
If the issue persists, try fixing broken dependencies:
sudo apt --fix-broken install
How Can I Use a Specific GCC Version for a Project?
Question:
I need to use a specific GCC version for a particular project. How can I do that?
Answer:
You can create a symbolic link inside the project directory:
- Create a local GCC link.
ln -s /usr/bin/gcc-9 ./gcc
- Use this local GCC for compiling.
./gcc -o program program.c
How to Fix “command not found” for GCC?
Question:
Even though I installed GCC, I get gcc: command not found
. What should I do?
Answer:
First, check if GCC is installed:
dpkg -l | grep gcc
If not installed, reinstall it:
sudo apt install gcc
If the issue persists, check the symbolic link:
ls -l /usr/bin/gcc
If the link is broken, fix it:
sudo ln -sf /usr/bin/gcc-10 /usr/bin/gcc
Summary
In this section, we covered common questions and solutions related to GCC.
Key Takeaways:
- You can install the latest GCC using a PPA repository.
- Use
update-alternatives
to manage versions. - Fix common installation and execution errors with specific commands.

8. Conclusion and Next Steps
Recap of This Article
- Overview and Role of GCC
- GCC is a powerful compiler supporting multiple programming languages.
- Ubuntu provides an easy installation method via the official repository.
- Installation and Preparation
- Updated the system and installed the
build-essential
package. - Checked version and resolved dependency issues.
- Basic Usage
- Created, compiled, and executed a simple program.
- Discussed error handling and optimization options.
- Managing Multiple Versions
- Installed and switched between different GCC versions using
update-alternatives
.
- Troubleshooting and FAQ
- Provided solutions for common installation, compilation, and execution issues.
Recommended Resources
- Ubuntu Official Site – For package management and development tool guides.
- GCC Official Documentation – For advanced configurations and usage.
- Stack Overflow – For community discussions and troubleshooting.
Next Steps
- Apply GCC in real-world projects.
- Explore additional libraries to expand functionality.
- Continue learning other programming tools and languages.
- Engage with the open-source community.
Final Thoughts
We covered everything from installing GCC to troubleshooting common issues. Use this guide as a reference to set up and optimize your development environment.
In the next article, we will dive into C and C++ programming techniques. Stay tuned for updates!