How to Install GCC Compiler on Ubuntu 18.04
-
Install
GCC
onUbuntu
-
Verify the Installation of
GCC
-
Compile Programs Using
GCC
-
Install Multiple
GCC
Versions
GCC
stands for GNU Compiler Collection
, and it complies different programming languages such as R, C, C++, Objective-C, Fortran, Ada, Go, and D. We can install the GCC
compiler on Ubuntu
using the apt
command-line tool. We must log in as a superuser to install the GCC
using apt
.
Install GCC
on Ubuntu
A meta-package name build-essential
is present in default Ubuntu
repositories that contain GCC
and various other compilers like g++
and make
.
We also install GCC
on our system by installing the build-essentials
package. Once we install the build-essential
package, GCC
is also installed in our system.
To install build-essential
we follow the following steps:
-
Update the package list with the command:
sudo apt update
-
Install the
build-essential
package using the command:sudo apt install build-essential
It installs
GCC
along with other compilers in our system.
Verify the Installation of GCC
We use the following command to verify whether GCC
is installed successfully or not,
gcc --version
If the GCC
is successfully installed we get an output with version and other information about GCC
.
Output:
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.
Compile Programs Using GCC
To compile programs using GCC
, we use the following command.
gcc <filename> -o <name_of_executable>
It compiles the program <filename>
to produce an executable named <name_of_executable>
. When we run this executable, we get the output.
gcc hello_world.c -o hello_world
It compiles the program hello_world.c
to produce an executable with the name hello_world
.
To run the executable, we use the command:
./hello_world
Install Multiple GCC
Versions
We can also have multiple versions of GCC
in our system.
To install multiple versions of Ubuntu, we follow the following steps:
-
Add
ubuntu-toolchain-r/test
to our system using the command:sudo apt install software-properties-common sudo add-apt-repository ppa:ubuntu-toolchain-r/test
-
Install the desired versions using the command:
sudo apt install gcc-7 gcc-8 gcc-9
It installs the three versions of
gcc
:gcc-7
,gcc-8
andgcc-9
in our system. -
Configure to manage the priorities for different installed versions:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 80 --slave sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave
Here, the highest priority is set for
gcc-9
and hence it will be the defaultgcc
for our system.
Suraj Joshi is a backend software engineer at Matrice.ai.
LinkedIn