How to Install GCC on Ubuntu: The Only Guide You’ll Need

Install GCC on Ubuntu

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

This guide will walk you through everything you need to know if you don’t know how to install GCC on Ubuntu using the simplest steps. GCC is an essential tool for developers working with C, C++, and other programming language.

The GNU Compiler Collection (GCC) includes compilers and libraries for several programming languages, such as C, C++, Objective-C, Fortran, Ada, Go, and D. Numerous open-source projects, like the GNU tools and the Linux kernel, use GCC for compilation.

To begin software development on Ubuntu, you should first install the GCC (GNU Compiler Collection). This allows you to create and run programs in C and C++. Additionally, GCC supports various other programming languages as well.

Let me first give you guys a basic overview of what it is and how installing GCC on Ubuntu is actually a straightforward task!

What is GCC? & Why Do You Need GCC on Ubuntu?

The GCC, or GNU Compiler Collection, is a free compiler system created by the Free Software Foundation (FSF). It supports multiple programming languages like C, C++, Objective-C, and Fortran. Essentially, it’s a tool that converts the code you write—often referred to as “source code”—into a format that computers can understand. This process is called compilation.

Beyond just compiling code, GCC plays a vital role in producing code, debugging, and handling system administration tasks.

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

Prerequisites For Installing GCC on Ubuntu

Before you install GCC on Ubuntu, make sure you have the following as your prerequisites:

  • A user account with sudo or root access.
  • A computer running either Ubuntu 20.04 or 22.04.
  • A Functional Ubuntu System: You need to have Ubuntu up and running. If you haven’t set it up yet, check out the installation guides on the official Ubuntu website.
  • Terminal Access: Get comfortable with the Ubuntu terminal, which is where you’ll run the installation commands. You can usually open it by hitting Ctrl+Alt+T.
  • Sudo Access: Confirm that you have sudo privileges. You’ll need these to install software and make changes to the system. If you’re the main user or admin of your Ubuntu setup, you probably already have this access. If not, touch base with your system administrator.
  • Internet Connection: Ensure you have a working internet connection since we’ll be downloading GCC and its dependencies from online sources.
  • Basic Terminal Knowledge: You don’t need to be a pro at command-line stuff, but having some basic terminal skills will help. No worries; we’ll walk you through everything step by step.
  • Storage Space: Check that you have enough free space on your Ubuntu system. Installing GCC on Ubuntu and its libraries might take up a few hundred megabytes.

How To Install GCC on Ubuntu (A Step-by-Step Guide)

There is more than 1 way to install GCC on Ubuntu, But your customization needs to determine which one you prefer. Follow these simple steps

Method 1: Install GCC Compiler from Ubuntu Repositories

The quickest way to install GCC on Ubuntu is by using the apt package manager. The apt package manager is the fastest method to install GCC on Ubuntu. However, there are some downsides to using the Ubuntu repositories for this installation, such as limited customization options and possible dependency issues.

However, installing it from the Ubuntu repositories has some downsides, like limited customization options and possible dependency issues.

To install the GCC compiler from the Ubuntu repositories:

1. First, update the package list:

sudo apt update

2. Next, install the build-essential package:

sudo apt install build-essential

The build-essential package contains the GCC compiler along with other tools needed for software development.

3. Finally, run this command to check if the GCC compiler is installed correctly. This will display the version of GCC.

gcc --version

In this example, the GCC version is 11.4.0.

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0<br>Copyright (C) 2021 Free Software Foundation, Inc.<br>This is free software; see the source for copying conditions. There is NO<br>warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Method 2: Installing Multiple GCC Versions on Ubuntu

You can use GCC PPAs (Personal Package Archives) to simplify the installation of specific or multiple GCC versions:

1. First, update the Ubuntu package repository:

sudo apt update

2. Next, install the software-properties-common package with this command:

sudo apt install software-properties-common

3. Add the GCC PPA that includes all versions of the GCC compiler:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test

Press Enter to confirm.

4. Update the package list to include the new PPA packages:

sudo apt update

5. To install a specific version or multiple versions of GCC, use the following command. For instance, to install GCC 14 and 13, type:

sudo apt install gcc-14 g++-14 gcc-13 g++-13 -y

Note: You can install as many GCC versions as you need with this method.

6. Use the update-alternatives tool to manage the different GCC versions. Add GCC 14 as an alternative:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-14 14 --slave /usr/bin/g++ g++ /usr/bin/g++-14

7. To switch between the installed GCC versions, use the update-alternatives tool:

sudo update-alternatives --config gcc

The system will show a list of installed GCC versions. Enter the number corresponding to the version you want to set as default.

8. Finally, check the current GCC version with this command:

gcc --version

This will display the current default GCC version, for example, 14.2.0.

Method 3: Installing GCC Compiler on Ubuntu from Source

Building GCC from source is a more advanced method that lets users tailor the GCC setup. This approach also allows you to choose installation paths and optimize GCC for specific hardware.

To download the GCC source code from official repositories and set up the GCC compiler, follow these steps:

1. First, install the required dependencies with this command:

sudo apt install build-essential

2. Next, install the libgmp3-dev, libmpfr-dev, and libmpc-dev packages to help compile GCC from source:

sudo apt install libmpfr-dev libgmp3-dev libmpc-dev -y

3. Use the wget command to download the GCC source code from the GCC website. For instance, the file gcc-14.2.0.tar.gz contains the source code for GCC version 14.2.0:

wget http://ftp.gnu.org/gnu/gcc/gcc-14.2.0/gcc-14.2.0.tar.gz

Make sure to adjust the URL to match the GCC version you wish to install.

4. Extract the GCC build files using the tar command:

tar -xf gcc-14.2.0.tar.gz

5. Navigate to the GCC directory:

cd gcc-14.2.0

6. Configure the GCC build with the following command:

./configure -v --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --prefix=/usr/local/gcc-14.2.0 --enable-checking=release --enable-languages=c,c++ --disable-multilib --program-suffix=-14.2.0

Check the official GNU GCC configuration page for more options and recommended configurations.

7. Start the GCC build process using the make command:

make -j3

This process can be resource-intensive and may take some time. The j3 option tells the system to use three cores for the build. Adjust the number of CPU cores based on your system’s specifications.

8. Once the build is finished, install GCC with this command:

sudo make install

Uninstalling or Upgrading GCC on Ubuntu

If you really, need to remove gcc-4.9, use this short and dangerous command:

sudo apt-get purge gcc-4.9

You get the following output:

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Final Thoughts on Installing GCC on Ubuntu

Installing GCC on Ubuntu is simple and essential for compiling C/C++ programs. By Following the steps explained in this guide a developer whether experienced or just a beginner can ensure a smooth installation process.

As you create, organize, and execute your programs confidently, you have learned three different methods to install the GCC compiler on Ubuntu. Utilizing GCC on an Ubuntu system allows you to engage in various programming tasks.

FAQ’S

1. What is GCC?

GCC (GNU Compiler Collection) is used to compile and create programs in languages like C, C++, Fortran, and more. It is commonly used in software development and system programming.

2. How to install GCC on Ubuntu without having sudo access?

If you lack sudo privileges, you can compile and install GCC from the source code. This process involves more steps and requires additional dependencies.

3. How can I update GCC to the latest version?

To update GCC, you need to add the required repositories and then install it:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test<br>sudo apt update<br>sudo apt install gcc-

4. How do I compile a basic C program using GCC?

First, create a file named hello.c and include the following code:

include
int main() {<br>printf("Hello, World!\n");<br>return 0;<br>}

Then, compile it with:

gcc hello.c -o hello

To run it, use:

./hello

5. Is GCC compatible with C++?

Absolutely! To compile C++ programs, use g++ instead of gcc.

g++ program.cpp -o program

6. What are the consequences of removing GCC?

If you remove GCC, you won’t be able to compile programs from source. However, you can reinstall it later using sudo apt install build-essential.

7. How can I check the available versions of GCC?

You can check by running:

apt-cache search gcc

8. Can I install GCC on Ubuntu WSL (Windows Subsystem for Linux)?

Yes! You can use the same installation commands as you would on a standard Ubuntu system.

9. How do I configure environment variables for GCC?

You can set custom paths with the following commands:

export CC=/usr/bin/gcc<br>export CXX=/usr/bin/g++

10. Why is my GCC installation taking a long time?

If the installation is slow, it might be due to network problems. Consider using a faster mirror:

Areeba Nauman
Areeba is a Content Writer with expertise in web content and social media, she can simplify complex concepts to engage diverse audiences. Fueled by creativity and driven by results, she brings a unique perspective and a keen attention to detail to every project she undertakes with her creativity and passion for delivering impactful content strategies for success. Let's connect on Linkedin: https://www.linkedin.com/in/areeba-bhatti/
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!