The GCC (GNU Compiler Collection) is a free software compiler system capable of compiling several programming languages, including C, C++, Objective-C, and Fortran.
Developers who want to build C or C++ software on a Linux-based system need a reliable compiler like GCC.
Find out how to install GCC on Ubuntu and start compiling code on your system in no time.
There are several ways to install GCC on Ubuntu, and the preferred method depends on your customization needs or if a project requires a specific GCC version.
The fastest way to install GCC on Ubuntu is to use the apt package manager. However, installing GCC from Ubuntu repositories has drawbacks, such as a lack of customization options and potential dependency conflicts.
To install the GCC compiler from Ubuntu repositories:
1. Update the Ubuntu package repository using the following command:
sudo apt update
2. Install the build-essential package:
sudo apt install build-essential
Install the build-essential package in Ubuntu." width="800" height="173" />
The build-essential package includes the GCC compiler and other utilities required for building software.
3. Use the following command to check the GCC version:
gcc --version
The GCC version in this example is 11.4.0.
Users can utilize GCC PPAs (Personal Package Archive) to streamline the installation process and install a specific GCC version (or multiple GCC versions).
1. Update the Ubuntu package repository:
sudo apt update
2. Install the software-properties-common package using the following command:
sudo apt install software-properties-common
3. Add the GCC PPA that contains all the versions of the GCC compiler:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
4. Update the packages list to include the PPA packages:
sudo apt update
5. Use the command below to install a specific version or multiple versions of GCC. For example, to install GCC 12 and GCC 13, enter:
sudo apt install gcc-12 g++-12 gcc-13 g++-13 -y
Note: There are no restrictions to the number of GCC versions you can install using this installation method.
6. The update-alternatives tool helps users manage multiple GCC versions:
Add the GCC 12 alternative to the update-alternatives filesystem:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 12 --slave /usr/bin/g++ g++ /usr/bin/g++-12
Add the GCC 13 alternative to the update-alternatives filesystem:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13 --slave /usr/bin/g++ g++ /usr/bin/g++-13
Note: Replace the GCC version in the example commands (GCC 12 and 13) with the GCC versions installed on your system.
7. Use the update-alternatives tool to switch between installed GCC versions:
sudo update-alternatives --config gcc
The system displays a list of the installed GCC versions and asks to select the default version. Enter the selection number for the version you want to use.
8. Check the current GCC version using the following command:
gcc --version
The current default GCC version in this example is 13.1.0.
Building GCC from source is an advanced installation method that allows users to customize the GCC configuration. Moreover, you can change install locations or optimize GCC for specific hardware.
To retrieve the GCC source code from official repositories and install the GCC compiler:
1. Install the necessary dependencies using the following command:
sudo apt install build-essential
2. Install the libgmp3-dev, libmpfr-dev, and libmpc-dev packages to facilitate compiling 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. In this example, the file gcc-13.2.0.tar.gz contains the source code for GCC version 13.2.0:
wget http://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz
Adjust the URL in the command to reflect the GCC version you want to install.
4. Extract the GCC build files using the following command:
tar -xf gcc-13.2.0.tar.gz
5. Access the GCC directory:
cd gcc-13.2.0
6. Configure the GCC build:
./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
Visit the official GNU GCC configuration page to review available options and recommended configuration procedures.
7. Use the make command to initiate the GCC build process:
make -j3
The build process can consume a lot of resources and take a while to complete. The j3 command tells the system to use three cores for the operation. Change the number of cores to reflect your system's capabilities and setup.
8. After the build process is complete, install GCC using the following command:
sudo make install
9. Enter the following command to verify the GCC version:
/usr/local/gcc-13.2.0/bin/gcc-13.2.0 --version
The system confirms GCC version 13.2.0 is installed.
You now know how to install the GCC compiler on Ubuntu using three different methods. Regardless of the method, installing GCC on an Ubuntu machine allows you to compile and run C and C++ code and complete many programming, debugging, and system administration tasks.
Check out these five different test automation frameworks that make automated testing faster and more reliable.