How to Install GCC Compiler on Ubuntu


The GNU Compiler Collection also known as GCC is a package that contains compilers for programming languages such as C, C++, Objective-C, Go, Rust, Ada, and Fortran.

It is a free program and is distributed under the GNU license. GCC is a cross-platform handy tool to compile programs right from the terminal.

Note: The GCC is included in all the latest Ubuntu. The installed GCC version may not always be the latest, potentially leading to errors when compiling code using the latest libraries.

This guide will cover details about how to install different versions of GCC on Ubuntu and how to manage them.

Install GCC on Ubuntu

The first method to install GCC on Ubuntu is by installing the build-essential meta package. This package contains various dependencies that help in software development and compilation.

sudo apt install build-essential

It contains the following dependencies:

libc It is a C library that contains standard functions
gcc It is GNU compiler for C programs
g++ It is GNU compiler for C++ programs
make It is a tool to build software
dpkg-dev It is a tool used to manage and build software for Debian based distributions
The build-essential package essentially installs the GCC compiler along with other packages. To verify, check the GCC version using the --version option.
gcc --version

The GCC can also be installed using the command given below:

sudo apt install gcc

Install and Manage Multiple Versions of GCC on Ubuntu

Ubuntu comes with a tool called software-properties-common which is an abstraction to manage the APT repositories. It allows you to add and remove repositories through the terminal.

Important considerations: Installing multiple versions of GCC can have dependency conflicts and can cause system stability issues. Installing them from a third-party repository may not give you a tested version. Moreover, they can also cause namespace conflicts.

To obtain multiple versions of GCC on Ubuntu, we need to add the repositories that host all the versions of GCC. To add a repository, the add-apt-repository command is used, which is a part of software-properties-common. The repository that manages the GCC package is ubuntu-toolchain-r/test.

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

This is a simple approach to adding the repository. To list the added repository use the –list option.

apt-add-repository --list

Note: Both add-apt-repository and apt-add-repository commands can be used interchangeably because they serve the same purpose.

After adding the repository, we will have access to the GCC archive. The currently installed version is 11.4.0. To install the GCC 12, we will use the apt install command with the version number.

sudo apt install gcc-12 gcc-10

The above command is installing the 2 GCC versions, GCC 12 and GCC 10. Well, by default, Ubuntu will use version 11.4.0. To set up another version, we need a manager that can help with switching the compilers.

The update-alternatives command is used to manage different versions of the package. In order to use, this utility for the GCC version manager, we need to assign versions to it using the –install option. For instance, to set the GCC version 10 to update-alternatives manager use the command given below:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 10

In the command mentioned above, the first occurrence of /usr/bin/gcc refers to the command for which we want to install an alternative. The gcc after it is the name of the alternative. Next, is the path of the alternative /usr/bin/gcc-10 and 10 is the priority. The priority is determined by the assigned general role; higher numbers indicate higher priority.

To install GCC version 12, use the same command mentioned above, with changes in version path and priority.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 12

We have added two additional options to the gcc command and to list the alternatives, use the –config option with the alternative name.

sudo update-alternatives --config gcc

By default, the compiler with the higher priority will be selected. To change the alternative, mention the number and press ENTER.

Using this method, any GCC version can be installed and managed.

Install GCC using Source

The GCC can also be installed by downloading the source code file and then building it using the make command.

Downloadable codes of all GCC versions can be accessed from here. I will download the GCC version 12.3.0, however, you can download any other version.

Now, download the code using the wget command.

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

After downloading the code, untar the archive file using the tar command.

tar -xf gcc-12.3.0.tar.gz

It will extract the content of the gcc-12.3.0 file to a directory with the same name. To enter the directory for further process.

cd gcc-12.3.0

Now, configure using the ./configure script, which will generate the make file for the final build.

./configure --build=arm-linux-gnu --host=arm-linux-gnu --target=arm-linux-gnu --prefix=/usr/local/gcc-12.3.0 --disable-multilib --enable-langauges=c

In the above command, –build is the system type on which the package will be built. The –host is the system that will host the build. The –target option is used to specify the architecture of the system.

In my case, I am building it on an arm processor with a system named linux-gnu. So, for the above-mentioned fields, the system name will be the same arm-linux-gnu. For more system types, read the GNU GCC building documentation here.

Next, the –prefix option is used to mention the directory where the file will be compiled. It is /usr/local directory where locally created executables are stored. The –disable-multilib option is essentially disabling to make builds for both 32 and 64-bit systems.

The –enable-language is used to set programming languages that you need to install a compiler for, I am setting it for C programming language only, however, other languages can also be added using the comma separator (c,c++).

Note: You must have gcc, g++, and gnat installed before going for the building process.

After that, we will use the make command to begin the building process.

make

The make command takes a lot of system resources, it is a good idea to mention the number of cores to complete the task swiftly.

make -j2

The -j2 means that 2 cores have been assigned to complete the making process.

Next, use the following command to install the build.

sudo make install

You can set find this version in the /usr/local directory once the above operation is completed. To properly manage it, install it to the update-alternatives.

Conclusion

The GCC is an open-source package that is used to compile programming languages from the terminal window. It comes by default in all the latest Ubuntu, however, it does not mean the latest version will be installed. In order to obtain the most recent version, you’ll have to install it manually.

In this guide, I covered different methods to install GCC on Ubuntu and how to install multiple versions of GCC and manage them with the update-alternative command.

Print Friendly, PDF & Email
Categories