How to Install GCC Compiler on Ubuntu 24.04?


Installing GCC compilers on Ubuntu 24.04 is critical for developers and programmers working within the Linux environment. GCC, or the GNU Compiler Collection, provides a robust set of compilers that support various programming languages such as C++, C, Ada, Fortran, Objective-C, and Go.

The objective of installing GCC on Ubuntu 24.04 is to enable the compilation of software from the source. This is essential for developers who must create executable programs or libraries from code they have written or obtained from other sources.

This article will explain possible methods to install the GCC Compiler on Ubuntu 24.04.

Let’s start the article with installation.

How to Install GCC Compiler on Ubuntu 24.04?

GCC compilers are often required for building and installing other software packages that are distributed in source form. To install the GCC compiler on Ubuntu 24.04, follow the stated methods:

Let’s start with the Ubuntu default repository.

Method 1: Installing GCC Compiler on Ubuntu 24.04 Using APT Package Manager

With GCC installed, developers can ensure compatibility with the system libraries. To install the GCC Compiler on Ubuntu 24.04, utilize the APT package manager, which makes easy the process. Let’s follow the below steps:

Step 1: Updating Package list

Begin by updating the package list and make the system up-to-date with the apt command:

sudo apt update

Step 2: Install GCC Package

After the update is complete, install the GCC package by executing the “build-essential” utility:

sudo apt install build-essential

It installs the GCC compiler as well as additional utilities such as “make” and “g++”.

Step 3: Check GCC Version

Now, users can authenticate the GCC installation by verifying its version:

gcc --version

Remove/Uninstall GCC Compiler

To remove/uninstall the GCC Compiler on Ubuntu 24.04, use the “autoremove” utility with the “build-essential” package as below:

sudo apt autoremove build-essential

In this way, the GCC compiler has been installed on Ubuntu 24.04.

Method 2: Installing GCC Compiler on Ubuntu 24.04 Using Source Code

Installing GCC compilers on Ubuntu 24.04 equips users with essential tools for software development, customization, and education, fostering an environment where code can be transformed into functional and efficient software applications.

To install the GCC Compiler on Ubuntu 24.04 from the source code, follow the below step-by-step instructions:

Step 1: Updating Packages List

Begins by ensuring that your system is up to date. It is necessary to install any package on the system:

sudo apt update

Step 2: Install Necessary Dependencies

Next, install the dependent packages with the “build-essential” utility to compile the code with any issues:

sudo apt install build-essential

Step 3: Download the GCC Source Code

Now, download the GCC source code from the official GNU website via the “wget” utility:

wget https://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz

Step 4: Extract Downloaded File

Now, extract the downloaded file with the “tar” command with the “xvf” option:

tar -xvf gcc-13.2.0.tar.gz

Step 5: Configure Build Environment

Then, navigate to the extracted directory via the “cd” command and configure the build environment by running the “.configure” command as below:

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

If users find any dependencies error, install these utilities to resolve it:

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

Step 6: Compile Source Code

After building the environment, users need to compile the source code with the “make” command:

make -j3

Step 7: Install GCC

Finally, install GCC along with all dependent packages with the “install” utility as below:

sudo make install

Step 8: Verify GCC Installation

For verification, users can check the GCC version by specifying the path as below:

/usr/local/gcc-13.2.0/bin/gcc-13.2.0 --version

This is all from the installation of GCC Compiler on Ubuntu 24.04 using source code.

Method 3: Installing GCC Compiler on Ubuntu 24.04 Using the PPA Repository

To install multiple GCC compilers on Ubuntu 24.04, you’ll need to follow a series of steps that involve adding the necessary repositories, updating the package list, and then installing the compilers.

Step 1: Add Toolchain PPA Repository

First, add the Toolchain test builds PPA repository to the Ubuntu system:

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

Step 2: Update Package List

Next, update the package list before installing the GCC compiler on Ubuntu:

sudo apt update

Step 3: Install GCC Compiler

Now, users can install specific versions of GCC by using “gcc-x” and “g++-x”. For instance, install the GCC 13 latest version:

sudo apt install gcc-13 g++-13

Install GCC Compiler (Specific Version)

Users can also install GCC 9 and GCC 10 up to 13 from the added PPA repository:

sudo apt install gcc-9 g++-9 gcc-10 g++-10

Step 4: Add GCC to the Filesystem

To add GCC 13 to the update-alternatives filesystem, execute the below command:

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

Step 5: Verify GCC Version

Finally, users can verify the installation of GCC by checking its version:

gcc --version

Optional: Switch GCC Installed Versions

To switch between the installed versions of the GCC compiler, use the “update-alternatives”. After that, type the selection number for switching to a different version or press the “Enter” button to keep the current choice:

sudo update-alternatives --config gcc

Using PPA, users can install any specific version of the GCC compiler on Ubuntu 24.04.

How to Compile a C or C++ Program Using the GCC Compiler?

The ability to compile code is fundamental to understanding the software development lifecycle and the behavior of programs on a Unix-like operating system such as Ubuntu.

To compile a basic C or C++ program using the GCC Compiler on Ubuntu 24.04, follow these steps:

Step 1: Install Build-essential Package

First, install this package that contains the GCC compiler and other necessary tools:

sudo apt install build-essential

Step 2: Create a C Program File

To create a file named “linuxgenie.c”, open the text editor such as “nano” (users can choose any name for your program):

nano linuxgenie.c

In the editor, enter the following C source code to print the message:

#include <stdio.h>
int main() {
printf("Linuxgenie enhances the skill set of linux users");
return 0;
}

Save the file, close the text editor, and return to the terminal.

Step 3: Compile a C Program

Now, navigate to the directory containing the C or C++ source file using the “cd” command. To compile a C program use the “gcc” command. It generates an executable file named “genie”

gcc -o genie linuxgenie.c

Here, “genie” is the name you wish to give to the compiled program, and “linuxgenie.c” is the source file:

For a C++ program, use the g++ along with the source file name with the extension .cpp:

g++ -o output_name source_file.cpp

Note: Remember to replace “output_name” and ‘source_file’ with your actual program name and file.

Step 4: Run Compiled Program

Finally, users run the ‘genie’ program by executing the below command:

./genie

That is all from the compile of the C++ program.

Conclusion

To install the GCC Compiler on Ubuntu 24.04, update package lists, and install the GCC package by running the “sudo apt install build-essential” command. It installs not only GCC, however, also additional utilities such as “g++” and “make”. Alternatively, install GCC on Ubuntu using the source code and PPA repository.

Users can also compile the C++ program using the “gcc -o output_name source_file.c” command. Here, the “output_name” is the name users wish to give to the compiled program, and “source_file.c” is your source file.

 

Print Friendly, PDF & Email
Categories