1. Introduction
What is GCC?
GCC (GNU Compiler Collection) is an open-source compiler capable of compiling multiple programming languages such as C and C++. It is widely used as the default compiler in many Linux distributions.
Key Features of GCC:
- Supports multiple languages, including C, C++, Fortran, Java, and more.
- Open source and freely usable by anyone.
- Enables fast and reliable compilation.
Why Use GCC on Ubuntu?
- Included as a standard package
Ubuntu’s repositories include GCC by default, making it easy to install. - Abundant support and documentation
Because there are many users worldwide, there is a wealth of information available for troubleshooting and customization. - Free to use
You can build a powerful development environment at low cost. - Easy to customize
You can manage multiple GCC versions, allowing you to build an environment suited to each project.
Summary
In this article, we introduced the overview of GCC and the benefits of using it on Ubuntu. GCC is a powerful compiler that supports multiple languages and is free to use, and on Ubuntu it’s especially easy to install.

2. Prerequisites
Update the system and check dependencies
First, update Ubuntu’s package information to its latest state. This helps prevent errors during installation.
1. Update the system to the latest state
sudo apt update
sudo apt upgradesudo apt update: Updates the package list to the latest version.sudo apt upgrade: Upgrades the installed packages to their newest versions.
Notes:
- The update may take several minutes.
- If after the update a “restart required” message appears, reboot the system.
Check development tools
To install GCC, basic development tools and packages are required. Run the following command to install the necessary packages in advance.
sudo apt install build-essentialThis command installs essential development tools, including GCC.
Examples of packages installed:
- gcc (C compiler)
- g++ (C++ compiler)
- make (build tool)
Check installation status
To confirm which packages are installed and check their versions, use the following command:
gcc --versionSample output:
gcc (Ubuntu 9.4.0-1ubuntu1) 9.4.0
Copyright (C) 2021 Free Software Foundation, Inc.If you see this output, you can confirm GCC has been installed correctly.
Summary of prerequisites
Up to this point, you have completed the prerequisites necessary to install GCC.
- Updated and upgraded the system to its latest state.
- Installed the required packages to prepare the environment.
- Verified GCC installation status and version.
3. How to Install GCC
Basic installation steps
On Ubuntu, GCC can be installed easily from the official repository. Follow the steps below to perform the installation.
- Install the build-essential package
sudo apt install build-essentialThis command installs GCC, G++, and the full set of development tools.
- Confirm installation progress
During installation, if “Proceed? (Y/n)” is displayed, type “Y” and press Enter.
How to verify after installation
After installation is complete, verify the GCC version to confirm everything is installed correctly:
gcc --versionSample output:
gcc (Ubuntu 9.4.0-1ubuntu1) 9.4.0
Copyright (C) 2021 Free Software Foundation, Inc.If version information is displayed as shown, GCC has been installed successfully.
Installing additional tools or libraries
Sometimes installing GCC alone is not sufficient, so you may want to install the following additional packages:
- Install G++ (C++ compiler)
sudo apt install g++- Install debugging tools
sudo apt install gdb- Install development manual pages
sudo apt install manpages-devThis ensures you can immediately refer to GCC-related help and manual pages when needed.
Troubleshooting failed installations
- Package not found
E: Unable to locate package build-essentialSolution: Update repository information:
sudo apt update
sudo apt upgrade- Permission errors occur
Permission deniedSolution: Use sudo at the beginning of commands to run with administrator privileges.
Summary of install steps
In this section, we explained how to install GCC and verify the installation, as well as how to install additional packages.
Key takeaways:
- Use
sudo apt install build-essentialto install quickly. - Check version to verify installation status.
- Add G++, gdb, and other tools as needed.

4. Basic Usage of GCC
Create and compile a simple program
- Create a sample program
First, create 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 inputting, press Ctrl + X to save, then press Y to confirm and exit.
Compile the program
Next, compile this program using GCC:
gcc hello.c -o helloExplanation of the command:
gcc: The compiler command.hello.c: The source code file to compile.-o hello: Specifies the output file name as “hello.”
Run the compiled program
Run the compiled program using the following command:
./helloExpected output:
Hello, World!If this is displayed, the program has been successfully compiled and executed.
Handling errors
- Errors due to coding mistakes
Example error message:
hello.c: In function ‘main’:
hello.c:3:5: error: expected ‘;’ before ‘return’
return 0;Solution:
The error message indicates the location (e.g. line 3). Check your code and correct the syntax errors.
- Compilation error
Example error:
gcc: command not foundSolution:
GCC may not be installed. Reinstall using:
sudo apt install build-essential- Runtime error
Example error:
bash: ./hello: Permission deniedSolution:
If the file lacks execution permissions, use:
chmod +x hello
./helloOptimization options
GCC allows you to use optimization options to improve program performance.
Example: Specify optimization level
gcc -O2 hello.c -o hello-O1: Basic optimizations.-O2: More advanced optimizations.-O3: Maximum optimization (prioritizing execution speed).
This helps you optimize execution speed or code size more efficiently.
Summary
In this section, we covered creating, compiling, and running a basic program using GCC.
Key takeaways:
- You learned how to create sample code and compile it.
- We examined how to handle errors when they occur.
- We introduced optimization options to boost performance.
5. Managing Multiple Versions
Installing multiple versions
On Ubuntu, you can install different versions of GCC simultaneously. Let’s install multiple versions using the steps below.
- Check available versions
sudo apt search gcc-This command lets you see the list of GCC versions in the repository.
Example output:
gcc-9 - GNU C compiler
gcc-10 - GNU C compiler
gcc-11 - GNU C compiler- Install required versions
For example, install GCC 9 and GCC 10:
sudo apt install gcc-9 gcc-10Once installation is complete, proceed to configure version switching.
How to switch versions
Ubuntu supports the update-alternatives command to easily switch GCC versions.
- Register installed GCC versions with update-alternatives
Run:
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 100In this setup, GCC 10 is registered as the preferred default (priority 100).
- Select the version to use
Use the following command to manually choose the desired version:
sudo update-alternatives --config gccSample 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 your desired number and press Enter.
Using a specific version per project
If you want to use a particular version for each project, you can switch symbolic links accordingly.
- Create or update link
sudo ln -sf /usr/bin/gcc-9 /usr/bin/gccThis command sets GCC 9 as the default version.
- Verify the version
gcc --versionConfirm that the version you configured is properly applied.
Summary
In this section, we explained how to install multiple GCC versions and use update-alternatives to switch easily.
Key takeaways:
- Install the versions you need and manage them with
update-alternatives. - You can also set up symbolic links to use a specific version per project.

6. Troubleshooting
Errors during installation and how to address them
Error example 1: Package not found
E: Unable to locate package build-essentialCause:
The package list is not up to date, or the repository configuration has issues.
Solution:
Run the following commands to update repository information:
sudo apt update
sudo apt upgrade
sudo apt install build-essentialAdditional remedy:
sudo add-apt-repository universe
sudo apt updateThis may enable the package to be found.
Error example 2: Permission denied
Permission deniedCause:
Commands are not being executed with administrative privileges.
Solution:
Prepend sudo to all installation commands.
sudo apt install build-essentialErrors during compilation and how to address them
Error example 1: Compiler not found
gcc: command not foundCause:
GCC is not installed or the PATH is not set correctly.
Solution:
Check if GCC is installed:
sudo apt install gccIf it is installed, check and adjust the symbolic link:
sudo ln -s /usr/bin/gcc-10 /usr/bin/gccError example 2: Library linking error
undefined reference to 'main'Cause:
The main function is not defined in your program, or linking failed.
Solution:
Ensure the main function is included, and recompile with link options, e.g.:
gcc -o output main.c -lmErrors at runtime and how to address them
Error example 1: No execution permission
bash: ./program: Permission deniedCause:
The executable file lacks execution permission.
Solution:
Grant 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:
Required shared libraries are not installed.
Solution:
Check the missing library name and install it:
sudo apt install libXXX-devErrors during version switching and solutions
Error example: switch not applied
gcc --versionIf the switched version is not shown, reexamine your update-alternatives settings.
Solution:
- Check the list of alternatives:
sudo update-alternatives --config gcc- Select the correct number.
- Update the symbolic link.
sudo ln -sf /usr/bin/gcc-9 /usr/bin/gccSummary
In this section, we explained common errors occurring during GCC installation and usage, and their solution strategies.
Key takeaways:
- Installation errors can often be addressed by updating repositories or adjusting repository settings.
- Compilation errors can be resolved by checking code and link options.
- Runtime errors often arise from permission issues or missing libraries.
- Version switching issues can be handled by symbolic links and
update-alternativesadjustments.
7. FAQ
How can I install the latest version of GCC?
Question:
I want to install the latest version of GCC, but the default repository only offers older versions. What should I do?
Answer:
To install the latest version of GCC, you can add 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 version:
sudo apt install gcc-12- Verify the version:
gcc --versionHow do I uninstall GCC?
Question:
I want to uninstall GCC. How should I proceed?
Answer:
You can remove GCC via the following commands:
sudo apt remove gcc
sudo apt autoremoveIf you also want to remove related tools, add:
sudo apt remove build-essentialWhat should I do if only older versions are selectable?
Question:
Even when I use update-alternatives --config gcc, only older versions appear. How can I add a newer version?
Answer:
Manually add the newer version to the alternatives settings.
- Install the version you need:
sudo apt install gcc-12- Add it manually to the alternatives:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120- Select the version:
sudo update-alternatives --config gccWhat if dependency errors occur?
Question:
Dependency errors appear during GCC installation. How can I resolve them?
Answer:
Dependency errors may result from the system not being up to date. Run:
sudo apt update
sudo apt upgradeIf that does not resolve it, automatically fix broken dependencies:
sudo apt --fix-broken installHow to use a specific GCC version for a particular project?
Question:
I want to use different GCC versions per project. How can I set that up?
Answer:
Create a symbolic link inside the project directory pointing to your desired GCC binary.
- Create the local link:
ln -s /usr/bin/gcc-9 ./gcc- Use it locally in compile commands:
./gcc -o program program.cWhat should I do if “command not found” occurs?
Question:
Even though I installed GCC, I see “gcc: command not found.” What should I do?
Answer:
First, confirm the installation:
dpkg -l | grep gccIf GCC is not installed, reinstall it:
sudo apt install gccIf it still fails, check the symbolic link:
ls -l /usr/bin/gccIf the link is broken, fix it:
sudo ln -sf /usr/bin/gcc-10 /usr/bin/gccSummary
In this section, we presented frequently asked questions and their concrete solutions for GCC.
Key takeaways:
- The latest version can be installed via PPA repositories.
- Uninstallation and version management are easily handled with
update-alternatives. - We also provided concrete command examples for troubleshooting.

8. Conclusion and Next Steps
Recap of this article
- GCC overview and role
- GCC is a powerful compiler supporting multiple languages such as C and C++.
- On Ubuntu, it can be easily installed from the official repository, making it ideal for building development environments.
- Installation and preparation steps
- We updated the system and installed the
build-essentialpackage. - We also verified versions and handled dependency troubleshooting.
- Basic usage
- We explained how to create, compile, and run sample programs.
- We discussed how to respond to errors and use optimization options.
- Managing multiple versions and switching
- We explained how to use
update-alternativesto switch between GCC versions per project.
- Troubleshooting and FAQ
- We covered possible installation and usage errors and gave concrete solutions.
Additional resources
Here are resources to aid further learning or applications:
- Ubuntu official documentation
- Ubuntu Official Site provides detailed guides on package management and development tools.
- GNU GCC official documentation
- GCC Official Manual lets you dive into advanced settings and usage.
- Linux console guides
- Linux Console publishes troubleshooting information across Linux topics.
- Learning sites and forums
- Qiita and Stack Overflow are great for browsing code examples and answers.
Next steps
- Apply GCC in real projects
- Use GCC in actual projects and carry out more advanced development.
- Use libraries and extensions
- Install additional libraries as needed to extend your project’s features.
- Learn new languages and tools
- Through learning other languages or build tools, aim for further skill advancement.
- Participate in the community
- Join forums or open-source projects, share knowledge, and develop practical skills collaboratively.
Final words
In this article, we explained step by step how to install and use GCC on Ubuntu. It covers everything from the basics to troubleshooting, making it easy even for beginners to set up their development environment.
One final note:
Use this article as a reference to apply GCC in your own projects, enjoy programming, and refer to the FAQ or additional resources if new questions arise.
In the next article, I plan to cover the basics of C and C++ syntax and advanced development techniques. Please stay tuned!



