Learn How to Install GCC on Ubuntu with Examples

Install GCC ubuntu

GCC, an acronym standing for GNU Compiler Collection, is a collection of compilers for programming languages including Java, C, and C++. In this guide, we will show you how to install GCC compilers and related tools in Ubuntu and Debian

Install GCC Compiler using apt

The first step in installing GCC compiler is to update and upgrade the system. This can be done in a single command on the terminal as shown

# sudo apt update && sudo apt upgrade

Next, install the build-essential package. The build-essential package contains a collection of packages which are considered crucial for building Ubuntu packages. This includes the GCC compiler, make and other essential tools.

To install the build-essential package run

# sudo apt install build-essential

Sample Output

Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version.

Verifying Installation

To verify that you have installed gcc and make run the command below

# whereis gcc make

Output

gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/man/man1/gcc.1.gz
make: /usr/bin/make /usr/share/man/man1/make.1posix.gz /usr/share/man/man1/make.1.gz

Alternatively, you can run

# gcc --version

Output

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609
Copyright (C) 2015 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.

To get the version of make run

make -v

OR

make --version

Output

GNU Make 3.81
Copyright (C) 2006  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 program built for x86_64-pc-linux-gnu

Next, we are going to install the dev man pages on Ubuntu/Debian distributions

Installation of dev man pages

To install the dev man pages run the following command

sudo apt-get install manpages-dev man-db manpages-posix-dev

Sample Output

Reading package lists... Done
Building dependency tree
Reading state information... Done
manpages-dev is already the newest version.
manpages-dev set to manually installed.
man-db is already the newest version.
The following extra packages will be installed:
  manpages-posix
The following NEW packages will be installed:
  manpages-posix manpages-posix-dev
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,291 kB of archives.
After this operation, 6,459 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

To view library calls, run the following commands

# man 3 scanf

Output

install GCC compilers

# man 2 execve

Output

install GCC compilers

# man 2 fork

Output

install GCC compilers

Now that we have confirmed successful installation of the compilers and main components, let's test the GNU GCC compiler

Testing the GCC compiler

Let's first create a simple C++ program

vim test_app.cpp

Add the following code

// Simple C++ program to display "Hello World" 

// Header file for input output functions 
#include 

using namespace std; 

// main function - 
// where the execution of program begins 
int main() 
{ 
	// prints hello world 
	cout<<"Hello World ! \n"; 
	
	return 0; 
} 

Next, compile the code as follows

g++ test_app.cpp -o test

Be sure to find a file called test in your directory using the ls command.

ls -l

Output

install gcc compiler

Finally, run it as shown

./test

Output

Hello world !

And that's it! We have successfully installed, tested and verified that our GCC compiler is working! Thank you for taking time in this tutorial on how to install GCC compilers on Ubuntu. Feel free to share on your social media handles.  Your feedback is very welcome.

Read Also:

Winnie Ondara 1:44 am

About Winnie Ondara

I’m Winnie, a passionate Linux administrator and an open source enthusiast. I enjoy keeping myself up to date with the latest in the Linux world and trying out the latest tools, features and services in the FOSS community.

Author's All Posts
Like to become part of Linoxide Team and contribute tips? Contact us here.

Comments

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

All comments are subject to moderation.