Comprehensive Guide to Installing and Using GCC on Ubuntu

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?

  1. Included as a standard package
    Ubuntu’s repositories include GCC by default, making it easy to install.
  2. Abundant support and documentation
    Because there are many users worldwide, there is a wealth of information available for troubleshooting and customization.
  3. Free to use
    You can build a powerful development environment at low cost.
  4. 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 upgrade
  • sudo 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-essential

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

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

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

This command installs GCC, G++, and the full set of development tools.

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

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

  1. Install G++ (C++ compiler)
sudo apt install g++
  1. Install debugging tools
sudo apt install gdb
  1. Install development manual pages
sudo apt install manpages-dev

This ensures you can immediately refer to GCC-related help and manual pages when needed.

Troubleshooting failed installations

  1. Package not found
E: Unable to locate package build-essential

Solution: Update repository information:

sudo apt update
sudo apt upgrade
  1. Permission errors occur
Permission denied

Solution: 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-essential to 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

  1. Create a sample program

First, create 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 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 hello

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

./hello

Expected output:

Hello, World!

If this is displayed, the program has been successfully compiled and executed.

Handling errors

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

  1. Compilation error

Example error:

gcc: command not found

Solution:
GCC may not be installed. Reinstall using:

sudo apt install build-essential
  1. Runtime error

Example error:

bash: ./hello: Permission denied

Solution:
If the file lacks execution permissions, use:

chmod +x hello
./hello

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

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

For example, install GCC 9 and GCC 10:

sudo apt install gcc-9 gcc-10

Once installation is complete, proceed to configure version switching.

How to switch versions

Ubuntu supports the update-alternatives command to easily switch GCC versions.

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

In this setup, GCC 10 is registered as the preferred default (priority 100).

  1. Select the version to use

Use the following command to manually choose the desired version:

sudo update-alternatives --config gcc

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

  1. Create or update link
sudo ln -sf /usr/bin/gcc-9 /usr/bin/gcc

This command sets GCC 9 as the default version.

  1. Verify the version
gcc --version

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

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

Additional remedy:

sudo add-apt-repository universe
sudo apt update

This may enable the package to be found.

Error example 2: Permission denied

Permission denied

Cause:
Commands are not being executed with administrative privileges.

Solution:
Prepend sudo to all installation commands.

sudo apt install build-essential

Errors during compilation and how to address them

Error example 1: Compiler not found

gcc: command not found

Cause:
GCC is not installed or the PATH is not set correctly.

Solution:
Check if GCC is installed:

sudo apt install gcc

If it is installed, check and adjust the symbolic link:

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

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

Errors at runtime and how to address them

Error example 1: No execution permission

bash: ./program: Permission denied

Cause:
The executable file lacks execution permission.

Solution:
Grant 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:
Required shared libraries are not installed.

Solution:
Check the missing library name and install it:

sudo apt install libXXX-dev

Errors during version switching and solutions

Error example: switch not applied

gcc --version

If the switched version is not shown, reexamine your update-alternatives settings.

Solution:

  1. Check the list of alternatives:
sudo update-alternatives --config gcc
  1. Select the correct number.
  2. Update the symbolic link.
sudo ln -sf /usr/bin/gcc-9 /usr/bin/gcc

Summary

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-alternatives adjustments.

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.

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

How 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 autoremove

If you also want to remove related tools, add:

sudo apt remove build-essential

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

  1. Install the version you need:
sudo apt install gcc-12
  1. Add it manually to the alternatives:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 120
  1. Select the version:
sudo update-alternatives --config gcc

What 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 upgrade

If that does not resolve it, automatically fix broken dependencies:

sudo apt --fix-broken install

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

  1. Create the local link:
ln -s /usr/bin/gcc-9 ./gcc
  1. Use it locally in compile commands:
./gcc -o program program.c

What 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 gcc

If GCC is not installed, reinstall it:

sudo apt install gcc

If it still fails, 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 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

  1. 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.
  1. Installation and preparation steps
  • We updated the system and installed the build-essential package.
  • We also verified versions and handled dependency troubleshooting.
  1. Basic usage
  • We explained how to create, compile, and run sample programs.
  • We discussed how to respond to errors and use optimization options.
  1. Managing multiple versions and switching
  • We explained how to use update-alternatives to switch between GCC versions per project.
  1. 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:

  1. Ubuntu official documentation
  1. GNU GCC official documentation
  1. Linux console guides
  • Linux Console publishes troubleshooting information across Linux topics.
  1. Learning sites and forums

Next steps

  1. Apply GCC in real projects
  • Use GCC in actual projects and carry out more advanced development.
  1. Use libraries and extensions
  • Install additional libraries as needed to extend your project’s features.
  1. Learn new languages and tools
  • Through learning other languages or build tools, aim for further skill advancement.
  1. 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!

年収訴求