How to Install and Use GCC on Ubuntu: A Complete Guide for Developers

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?

  1. Provided as a standard package
    GCC is included in Ubuntu’s official repositories, making it easy to install.
  2. Extensive documentation and community support
    With a large global user base, troubleshooting and customization resources are abundant.
  3. Free to use
    You can build a powerful development environment at zero cost.
  4. 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 upgrade
  • sudo 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-essential

This 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 --version

Example 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:

  1. Install the build-essential package
sudo apt install build-essential

This command installs GCC, G++, and other essential development tools.

  1. 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 --version

Example 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

  1. Create a sample program

Let’s start by creating a simple “Hello, World!” program.

nano hello.c

When 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 hello

Command 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:

./hello

Example output:

Hello, World!

If you see this output, your program has been compiled and executed correctly.

Handling Errors

  1. 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.

  1. Compiler not found

Example error message:

gcc: command not found

Solution:
GCC may not be installed. Reinstall it with the following command:

sudo apt install build-essential
  1. Runtime errors

Example error message:

bash: ./hello: Permission denied

Solution:
Grant execute permissions to the file:

chmod +x hello
./hello

Optimization 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.

  1. 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
  1. Install the required versions

For example, install GCC 9 and GCC 10:

sudo apt install gcc-9 gcc-10

After installation, proceed to configure version switching.

Switching Between Versions

Use the update-alternatives command to switch GCC versions easily.

  1. 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 100

This configuration gives GCC 10 a higher priority.

  1. Select the version you want to use
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:

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.

  1. Create the symbolic link
sudo ln -sf /usr/bin/gcc-9 /usr/bin/gcc
  1. Verify the version
gcc --version

Confirm 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-essential

Cause:
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-essential

Additional Solution:

sudo add-apt-repository universe
sudo apt update

This may allow the package to be found.

Error Example 2: Permission error

Permission denied

Cause:
The command was not executed with administrator privileges.

Solution:
Run installation commands with sudo:

sudo apt install build-essential

Compile-Time Errors and Solutions

Error Example 1: Compiler not found

gcc: command not found

Cause:
GCC is not installed, or the PATH is not configured correctly.

Solution:
Check whether GCC is installed:

sudo apt install gcc

If already installed, repair the symbolic link:

sudo ln -s /usr/bin/gcc-10 /usr/bin/gcc

Error 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 -lm

Runtime Errors and Solutions

Error Example 1: No execution permission

bash: ./program: Permission denied

Cause:
The executable does not have permission to run.

Solution:
Add execution permission:

chmod +x program
./program

Error Example 2: Missing libraries

error while loading shared libraries: libXXX.so: cannot open shared object file: No such file or directory

Cause:
A required shared library is not installed.

Solution:
Identify the missing library and install it:

sudo apt install libXXX-dev

Version Management Errors and Solutions

Error Example: Version switch not applied

gcc --version

If the output does not reflect the switched version, recheck update-alternatives settings.

Solution:

  1. Check available configurations.
sudo update-alternatives --config gcc
  1. Select the correct version.
  2. Update the symbolic link.
sudo ln -sf /usr/bin/gcc-9 /usr/bin/gcc

Summary

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.

  1. Add the PPA:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
  1. Update packages:
sudo apt update
  1. Install the latest GCC:
sudo apt install gcc-12
  1. Check the version:
gcc --version

How do I uninstall GCC?

Answer:
Run the following commands:

sudo apt remove gcc
sudo apt autoremove

To remove related development tools:

sudo apt remove build-essential

What if only old versions appear in update-alternatives?

Answer:
Add the required version manually:

sudo apt install gcc-12
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120
sudo update-alternatives --config gcc

How do I resolve dependency errors?

Answer:
Try updating the system:

sudo apt update
sudo apt upgrade

If unresolved:

sudo apt --fix-broken install

How do I use different GCC versions for specific projects?

Answer:

  1. Create a local symbolic link:
ln -s /usr/bin/gcc-9 ./gcc
  1. Use it during compilation:
./gcc -o program program.c

What should I do if I get “command not found”?

Answer:
Check whether GCC is installed:

dpkg -l | grep gcc

If not installed, reinstall it:

sudo apt install gcc

If the symbolic link is broken, fix it:

sudo ln -sf /usr/bin/gcc-10 /usr/bin/gcc

Summary

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

  1. Overview and purpose of GCC
  • GCC supports multiple programming languages such as C and C++.
  • Ubuntu provides easy access to GCC through official repositories.
  1. Installation and preparation steps
  • Updated the system and installed the build-essential package.
  • Reviewed version checks and dependency troubleshooting.
  1. Basic usage
  • Learned how to create, compile, and run sample code.
  • Explored optimization and error-handling techniques.
  1. Managing multiple versions
  • Used update-alternatives to switch GCC versions depending on projects.
  1. Troubleshooting and FAQ
  • Discussed common errors and step-by-step solutions.

Additional Resources

  1. Official Ubuntu Documentation
  1. Official GCC Documentation
  • GCC Manual offers advanced usage instructions and configuration options.
  1. Linux Troubleshooting Resources
  • Linux Console contains various Linux troubleshooting information.
  1. Learning Platforms and Forums

Next Steps

  1. Apply GCC in real development
  • Use GCC to build more advanced programs in your projects.
  1. Expand functionality using libraries
  • Include additional libraries to enhance project capabilities.
  1. Learn new tools and languages
  • Master related development tools and languages to expand your skill set.
  1. 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!