How to Install GCC on Ubuntu 20.04 | 18.04

This brief tutorial shows students and new users how to install GCC Compiler (build-essential) on Ubuntu 18.04 | 16.04.

The GNU Compiler Collection (GCC) is a collection of compilers and libraries developed by GNU Project that supports programming languages such as, C, C++, Objective-C, Fortran, Go and many more.

GCC is a core part of the Linux operating system and many open-source projects and tools. Linux kernel is also compiled with GCC, so it’s a big deal in the open source community.

If you’re a new users or student looking for a Linux system to start learning on, the easiest place to start is Ubuntu Linux OS. It’s a great Linux operating system for beginners and folks looking for easier Linux distribution to use.

Ubuntu is an open source Linux operating systems that runs on desktops, laptops, server and other devices.

When using Ubuntu, you will find that Linux isn’t so different than Windows and other operating systems in so many ways, especially when it comes to using the system to get work done.

For more about GCC, please visit its project page.

To get started with installing GCC compiler on Ubuntu, follow the steps below:

Option 1: Installing GCC on Ubuntu

Out of the box, Ubuntu repositories contain build-essential package which contains the GCC compiler, g++, make and a lot of libraries and other utilities required for compiling other packages and tools.

So installing the GCC compiler on Ubuntu is as simple as installing build-essential package.

To do that, run the commands below:

sudo apt update
sudo apt install build-essential

The commands install many other packages including, gcc, g++ and make.

If you’re a developer and want to install the manual page for GCC, simply run the commands below:

sudo apt-get install manpages-dev

After installing, to verify that GCC is successfully installed and functioning, simply use the gcc --version command as shown below:

gcc --version

That should print out an output as shown below:

gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 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.

This is how to install the GCC compiler on Ubuntu if you don’t already have it installed.

Option 2: Installing Multiple GCC Versions

For those who want to install multiple versions of the GCC compiler, they can use option #2.

New versions of the GCC compilers support for new languages, better performance, extended features. If you want to take advantage of these additional features, then installing the latest or multiple should be ideal.

To install the latest versions of the GCC compilers, you will have to add a third party PPA repository to Ubuntu and install it from there.

To do that, simply run the commands below:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ubuntu-toolchain-r/test

After adding the repository above, you can then install multiple GGC versions using their version number.

Example:

sudo apt install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9

Because you have multiple versions of the GCC compiler installed, there’s a priority number that gets associated with each. The version with the highest priority will be the default system compiler.

You will have to run the commands below to set the default system compiler associating each with a priority number.

The the case below, gcc-9 is the version with the highest priority [ 90 ].

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 --slave /usr/bin/gcov gcov /usr/bin/gcov-9
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave /usr/bin/g++ g++ /usr/bin/g++-8 --slave /usr/bin/gcov gcov /usr/bin/gcov-8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7 --slave /usr/bin/gcov gcov /usr/bin/gcov-7

Later if you want to change the default version use the update-alternatives command below:

sudo update-alternatives --config gcc

There should be multiple choices depending on the number of compilers installed.

  Selection    Path            Priority   Status
------------------------------------------------------------
* 0            /usr/bin/gcc-9   90        auto mode
  1            /usr/bin/gcc-7   70        manual mode
  2            /usr/bin/gcc-8   80        manual mode
  3            /usr/bin/gcc-9   90        manual mode

Press  to keep the current choice[*], or type selection number: 

Select your choice and press Enter.

That should update the system with the current default.

Conclusion

You have learned how to installed the GCC compiler on Ubuntu 18.04 | 16.04. If you find any error above, please use the comment form below to report it.

Thanks,

You may also like the post below:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.