How to install GCC compiler on Ubuntu 18.04

The GNU Compiler Collection (GCC) is a collection of compilers, including C, C++, Objective-C, Fortran, Ada, Go, and D languages. Many open source projects including GNU tools and Linux Kernel are compiled with GCC.

This guide explains how to install the GCC compiler on Ubuntu 18.04. We will show you how to install the stable version and the latest version of GCC.

The same instructions apply to Ubuntu 16.04 and any Ubuntu-based distribution, including Kubuntu, Linux Mint and Elementary OS.

1. Prerequisites##

To add new software sources and install software packages on your Ubuntu system, you need to log in to the system as root or another user with sudo privileges.

2. Install GCC on Ubuntu

The default Ubuntu software source contains a software set called build-essential, which contains the GCC compiler and many other necessary software for compilation.

Perform the following steps to install the GCC compiler on Ubuntu 18.04:

  1. Update software list:
sudo apt update
  1. Install the build-essential package, enter:
sudo apt install build-essential

This command installs some new software, including gcc, g++ and make.

You can install the manual to learn how to develop with GNU/Linux:

sudo apt-get install manpages-dev
  1. To verify whether the GCC compiler is successfully installed, use the gcc --version command, which will print the GCC version information:
gcc --version

The default GCC version available on the Ubuntu 18.04 software source is 7.4.0:

gcc(Ubuntu 7.4.0-1ubuntu1~18.04)7.4.0Copyright(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.

that's it. GCC has been installed on your system, and you can start using it.

Three, compile a Hello World example##

Using GCC to compile a basic C or C++ program is very simple. Open your text editor and create the following file:

nano hello.c
# include <stdio.h>
int main(){printf("Hello World!\n");return0;}

Save the file and compile it into an executable file using the following command:

gcc hello.c -o hello

This will create a binary file named hello in the same directory where you ran the command.

Execute the hello program:

. /hello

The program should print out:

Hello World!

Fourth, install multiple versions of GCC

This chapter provides instructions on how to install and use multiple versions of GCC on Ubuntu 18.04. The new version of the GCC compiler includes support for new languages, better performance, and more new features.

At the time of writing this article, several GCC versions are included in the default Ubuntu software source, ranging from 5.xx to 8.xx. The latest GCC version, 9.1.0 is available on Ubuntu ToolChain PPA.

In the following example, we will install the latest three versions of GCC and G++.

First, add the ubuntu-toolchain-r/test PPA software source to your system:

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

To install the GCC and G++ versions you want to install, enter:

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

The following command will configure alternative and set priority. The default version is the one with the highest priority, in our case it is gcc-9.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-990--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-880--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-770--slave /usr/bin/g++ g++/usr/bin/g++-7--slave /usr/bin/gcov gcov /usr/bin/gcov-7

Later, if you want to change your default version, you can use the update-alternatives command:

sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc(providing /usr/bin/gcc).

 Selection    Path            Priority   Status
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - *0 /usr/bin/gcc-990        auto mode
 1 /usr/bin/gcc-770        manual mode
 2 /usr/bin/gcc-880        manual mode
 3 /usr/bin/gcc-990        manual mode

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

There should be a list of GCC versions on the system displayed in front of you. Enter the number you want to set as the default version and press the Enter key.

This command will create a virtual link to the specified version of GCC and G++.

Five, summary##

You have successfully installed GCC on your Ubuntu 18.04.

You can browse GCC official documentation page and learn how to use GCC and G++ to compile your C and C++ programs.

Recommended Posts

How to install GCC compiler on Ubuntu 18.04
How to install GCC compiler on CentOS 7
How to install gcc compiler faster under Ubuntu
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install PostgreSQL on Ubuntu 16.04
How to install Git on Ubuntu 20.04
How to install GCC on CentOS 8
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Jenkins on Ubuntu 18.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Teamviewer on Ubuntu 16.04
How to install MariaDB on Ubuntu 20.04
How to install Nginx on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to install Nginx on Ubuntu 16.04
How to install OpenCV on Ubuntu 20.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install Go 1.6 on Ubuntu 16.04
How to install Go on Ubuntu 18.04
How to install MySQL on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 20.04
How to install VLC on Ubuntu 18.04
How to install TeamViewer on Ubuntu 20.04
How to install Webmin on Ubuntu 20.04
How to install Docker Compose on Ubuntu 18.04
How to install Ubuntu on Raspberry Pi
How to install Bacula Server on Ubuntu 14.04
How to install MySQL on Ubuntu 18.04 (linux)
How to install Ubuntu 19.10 on Raspberry Pi 4
How to install Apache Kafka on Ubuntu 18.04
How to install Apache Maven on Ubuntu 20.04
How to install Apache Tomcat 8 on Ubuntu 16.04
How to install Python2 on Ubuntu20.04 ubuntu/focal64
How to install Graylog 1.x on Ubuntu 14.04.
How to install Zabbix on Ubuntu 16.04 Server