1. Introduction
What Is GCC?
GCC (GNU Compiler Collection) is an open-source compiler capable of compiling multiple programming languages, including C and C++. It is widely used as the standard compiler for many Linux distributions.
Main Features of GCC:
- Supports multiple languages such as C, C++, Fortran, and Java.
- Open-source and freely available for anyone to use.
- Enables fast and highly reliable compilation.
Why Use GCC on Ubuntu?
- Provided as a standard package
GCC is included in Ubuntu’s official repositories, making it easy to install. - Extensive documentation and community support
With a large global user base, troubleshooting and customization resources are abundant. - Free to use
You can build a powerful development environment at zero cost. - Easy customization
Multiple GCC versions can be managed, enabling you to configure the optimal environment for each project.
Summary
This article introduces the basics of GCC and the advantages of using it on Ubuntu. GCC is a powerful, multi-language compiler available at no cost, and installation on Ubuntu is especially straightforward.

2. Preparation
Updating the System and Checking Dependencies
First, update Ubuntu’s package information to the latest version. This helps prevent errors during installation.
1. Update your system
sudo apt update
sudo apt upgradesudo apt update: Updates the package list.sudo apt upgrade: Upgrades installed packages to the latest versions.
Notes:
- The update process may take several minutes.
- If prompted to reboot afterward, restart the system.
Checking Development Tools
To install GCC, basic development tools and packages are required. Run the following command to install them:
sudo apt install build-essentialThis command installs GCC and essential development tools.
Examples of installed packages:
- gcc (C compiler)
- g++ (C++ compiler)
- make (build tool)
Checking the Installation Status
Use the following command to verify installation and check the version:
gcc --versionExample output:
gcc (Ubuntu 9.4.0-1ubuntu1) 9.4.0
Copyright (C) 2021 Free Software Foundation, Inc.If this result appears, GCC is installed correctly.
Preparation Summary
You have now completed the necessary preparations for installing GCC.
- Updated and upgraded the system.
- Installed required packages to set up the environment.
- Confirmed that GCC is installed and checked its version.
3. Installing GCC
Basic Installation Procedure
On Ubuntu, GCC can be easily installed from the official repository. Follow the steps below:
- Install the build-essential package
sudo apt install build-essentialThis command installs GCC, G++, and other essential development tools.
- Confirm installation progress
If prompted with “Continue? (Y/n)”, enter “Y” and press Enter.
Checking the Installation
Once installation is complete, verify the GCC version:
gcc --versionExample output:
gcc (Ubuntu 9.4.0-1ubuntu1) 9.4.0
Copyright (C) 2021 Free Software Foundation, Inc.If 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.cWhen 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, and then press Y to confirm.
Compiling the Program
Next, compile the program using GCC.
gcc hello.c -o helloCommand explanation:
gcc: The compiler command.hello.c: The source file to compile.-o hello: Specifies the output executable file name.
Running the Compiled Program
Execute the compiled program with the following command:
./helloExample output:
Hello, World!If you see this output, your program has been compiled and executed correctly.
Handling Errors
- Errors caused by code mistakes
Example error message:
hello.c: In function ‘main’:
hello.c:3:5: error: expected ‘;’ before ‘return’
return 0;Solution:
The error message shows the line number where the issue occurred. Review and correct the code.
- Compiler not found
Example error message:
gcc: command not foundSolution:
GCC may not be installed. Reinstall it with the following command:
sudo apt install build-essential- Runtime errors
Example error message:
bash: ./hello: Permission deniedSolution:
Grant execute permissions to the file:
chmod +x hello
./helloOptimization Options
GCC allows you to optimize your code for better performance.
Example: Specify optimization levels
gcc -O2 hello.c -o hello-O1: Basic optimizations.-O2: More advanced optimizations.-O3: Maximum optimization for performance.
These options help improve execution speed and reduce code size.
Summary
This section explained how to write, compile, and execute a basic program using GCC.
Key takeaways:
- Learned how to create and compile a sample program.
- Reviewed ways to handle common errors.
- Learned how to use optimization options to enhance program performance.
5. Managing Multiple Versions
Installing Multiple Versions
Ubuntu allows you to install different versions of GCC simultaneously. Follow the steps below to set them up.
- Check available versions
sudo apt search gcc-This command lists available GCC versions in the repository.
Example output:
gcc-9 - GNU C compiler
gcc-10 - GNU C compiler
gcc-11 - GNU C compiler- Install the required versions
For example, install GCC 9 and GCC 10:
sudo apt install gcc-9 gcc-10After installation, proceed to configure version switching.
Switching Between Versions
Use the update-alternatives command to switch GCC versions easily.
- Register installed GCC versions
Run the commands below to register GCC versions with update-alternatives:
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 100This configuration gives GCC 10 a higher priority.
- Select the version you want to use
sudo update-alternatives --config gccExample 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:Enter the number of the desired version.
Using a Specific Version per Project
If you want to use a different GCC version for specific projects, adjust symbolic links accordingly.
- Create the symbolic link
sudo ln -sf /usr/bin/gcc-9 /usr/bin/gcc- Verify the version
gcc --versionConfirm that the selected version is active.
Summary
This section covered how to install multiple GCC versions and switch between them using update-alternatives.
Key takeaways:
- Install required versions and manage them with
update-alternatives. - Configure symbolic links to use specific versions per project.

6. Troubleshooting
Installation Errors and Solutions
Error Example 1: Package not found
E: Unable to locate package build-essentialCause:
The package list is outdated, or the repository settings are incorrect.
Solution:
Update the repository information using the following commands:
sudo apt update
sudo apt upgrade
sudo apt install build-essentialAdditional Solution:
sudo add-apt-repository universe
sudo apt updateThis may allow the package to be found.
Error Example 2: Permission error
Permission deniedCause:
The command was not executed with administrator privileges.
Solution:
Run installation commands with sudo:
sudo apt install build-essentialCompile-Time Errors and Solutions
Error Example 1: Compiler not found
gcc: command not foundCause:
GCC is not installed, or the PATH is not configured correctly.
Solution:
Check whether GCC is installed:
sudo apt install gccIf already installed, repair the symbolic link:
sudo ln -s /usr/bin/gcc-10 /usr/bin/gccError Example 2: Library link errors
undefined reference to 'main'Cause:
The main function is missing, or there are link errors.
Solution:
Ensure that your code contains a main function. Recompile with the correct options:
gcc -o output main.c -lmRuntime Errors and Solutions
Error Example 1: No execution permission
bash: ./program: Permission deniedCause:
The executable does not have permission to run.
Solution:
Add execution permission:
chmod +x program
./programError Example 2: Missing libraries
error while loading shared libraries: libXXX.so: cannot open shared object file: No such file or directoryCause:
A required shared library is not installed.
Solution:
Identify the missing library and install it:
sudo apt install libXXX-devVersion Management Errors and Solutions
Error Example: Version switch not applied
gcc --versionIf the output does not reflect the switched version, recheck update-alternatives settings.
Solution:
- Check available configurations.
sudo update-alternatives --config gcc- Select the correct version.
- Update the symbolic link.
sudo ln -sf /usr/bin/gcc-9 /usr/bin/gccSummary
This section explained common errors during GCC installation and usage, along with specific solutions.
Key takeaways:
- Installation errors can be resolved by updating repositories and fixing settings.
- Compile-time issues often require checking code or link options.
- Runtime errors are frequently caused by missing permissions or libraries.
- Version control issues can be adjusted using symbolic links and
update-alternatives.
7. FAQ Section
How can I install the latest version of GCC?
Question:
I want to install the latest version of GCC, but Ubuntu’s repository provides only older versions. What can I do?
Answer:
Add the PPA repository and install the latest version.
- Add the PPA:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test- Update packages:
sudo apt update- Install the latest GCC:
sudo apt install gcc-12- Check the version:
gcc --versionHow do I uninstall GCC?
Answer:
Run the following commands:
sudo apt remove gcc
sudo apt autoremoveTo remove related development tools:
sudo apt remove build-essentialWhat if only old versions appear in update-alternatives?
Answer:
Add the required version manually:
sudo apt install gcc-12sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120sudo update-alternatives --config gccHow do I resolve dependency errors?
Answer:
Try updating the system:
sudo apt update
sudo apt upgradeIf unresolved:
sudo apt --fix-broken installHow do I use different GCC versions for specific projects?
Answer:
- Create a local symbolic link:
ln -s /usr/bin/gcc-9 ./gcc- Use it during compilation:
./gcc -o program program.cWhat should I do if I get “command not found”?
Answer:
Check whether GCC is installed:
dpkg -l | grep gccIf not installed, reinstall it:
sudo apt install gccIf the symbolic link is broken, fix it:
sudo ln -sf /usr/bin/gcc-10 /usr/bin/gccSummary
This section covered common questions and practical solutions related to GCC usage.
Key takeaways:
- The latest version can be installed via a PPA repository.
- Uninstallation and version management are handled via
update-alternatives. - Practical examples were provided to solve common errors.

8. Conclusion and Next Steps
Review of Key Points
- Overview and purpose of GCC
- GCC supports multiple programming languages such as C and C++.
- Ubuntu provides easy access to GCC through official repositories.
- Installation and preparation steps
- Updated the system and installed the
build-essentialpackage. - Reviewed version checks and dependency troubleshooting.
- Basic usage
- Learned how to create, compile, and run sample code.
- Explored optimization and error-handling techniques.
- Managing multiple versions
- Used
update-alternativesto switch GCC versions depending on projects.
- Troubleshooting and FAQ
- Discussed common errors and step-by-step solutions.
Additional Resources
- Official Ubuntu Documentation
- Ubuntu Official Site provides detailed guides on package management and development tools.
- Official GCC Documentation
- GCC Manual offers advanced usage instructions and configuration options.
- Linux Troubleshooting Resources
- Linux Console contains various Linux troubleshooting information.
- Learning Platforms and Forums
- Qiita and Stack Overflow offer helpful examples and community support.
Next Steps
- Apply GCC in real development
- Use GCC to build more advanced programs in your projects.
- Expand functionality using libraries
- Include additional libraries to enhance project capabilities.
- Learn new tools and languages
- Master related development tools and languages to expand your skill set.
- Join the open-source community
- Participate in forums or open-source projects to gain practical experience and share knowledge.
Final Thoughts
This article walked through every step of installing and using GCC on Ubuntu. By following the methods and troubleshooting practices introduced here, anyone can build a reliable development environment.
One last note:
Use GCC to bring your software ideas to life. If you encounter issues, revisit the FAQ or referenced resources to resolve them efficiently.
The next article will explore core syntax and advanced development techniques in C and C++. Stay tuned for updates!


