What Is build-essential and How to Install It on Ubuntu

This guide will explain what the build-essential meta-package is and what it includes when installed on your Ubuntu system.

build-essential on Ubuntu

build-essential is what is called a meta-package. It in itself does not install anything. Instead, it is a link to several other packages that will be installed as dependencies.

In the case of the build-essential meta-package, it will install everything required for compiling basic software written in C and C++.

On Ubuntu, this meta-package includes five individual packages that are crucial to compiling software.

  • gcc – This tool is the GNU compiler for the C Programming language.
  • g++ – This package is the GNU compiler for the C++ programming language.
  • libc6-dev – This is the GNU C library. This package contains the development libraries and header files used to compile simple C and C++ scripts.
  • make – This is a useful utility that is used for directing the compilation of programs. The make tool interprets a file called a “makefile” that directs the compiler how to work.
  • dpkg-dev – We can use this package to unpack, build and upload Debian source packages. This utility is useful if you want to package your software for Debian based system.

Basically, by installing the build-essential package, you give yourself everything you need to compile basic C and C++ software on Ubuntu.

You could install each of these packages individually if you wanted to. However, the build-essential meta-package makes it simple to get everything you need with a single package

While build-essential provides a good starting point on Ubuntu, you may need to install additional libraries to compile more complicated software.

Installing build-essential on Ubuntu

The build-essential meta-package is available directly from the official Ubuntu repository making it a straightforward installation process.

For the following steps, you will need to be using the terminal on your Ubuntu device. You can open the terminal easily by pressing CTRL + ALT + T.

Alternatively, you can use SSH to interact with your Ubuntu device remotely.

1. Before we can install the build-essential package on Ubuntu, we should first run an update.

Running an update ensures that the package list we have is pointing to the latest packages.

sudo apt update

The package list is updated from the repositories listed within the sources file and its subdirectories.

On a new installation of Ubuntu, this will only be reading from the official package repositories managed by Canonical.

2. We can easily install the build-essential package using apt by running the command below.

sudo apt install build-essential

By running this command, the apt package manager will look for build-essential within the package list.

Once found, it will check to see what dependencies the package requires. In this case, apt will be installing the gcc, g++, libc6-dev, dpkg-dev, and make packages.

Verifying that build-essential is Installed

Verifying that we installed the build-essential meta-package to your Ubuntu device is a relatively straightforward task.

All we need to do is get the “gcc” and “g++” compilers to output their versions.

Doing this will indicate to us that both packages have been installed successfully.

1. Let us start by checking the version of gcc by running the command below.

gcc is the GNU compiler for the C programming language.

gcc --version

Below is what you should get from running this command on your Ubuntu system.

The version numbers will differ slightly depending on what version of Ubuntu you are running. For example, we are running Ubuntu 20.04.

gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.

2. While we are at it, we can also check what version of g++ got installed by using the following command.

g++ is a compiler much like gcc but is used to compile software written in C++.

g++ --version

After running this command, you should see a message similar to what we have below.

The version number displayed below will, of course, be different depending on what version of Ubuntu you are using.

++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.

Compiling your First C Program

Our next step will be to write a simple C program that prints a single line of text to the command line.

In this case, we will be showing how the gcc compiler works from the build-essential package.

1. Let us begin by writing a small C program.

You can start writing this script by using the nano text editor on your Ubuntu device.

nano helloworld.c

2. Within this file, enter the following lines of code.

#include <stdio.h>

void main()
{
    printf("Hello World\n");
}

This code is incredibly straightforward as its whole purpose is to print some text to the terminal.

We start by including the standard input-output library header (stdio.h). This library contains the IO functionality we need for talking with the command line.

We then have the “main()” function. This function is called whenever the C program is run.

Within this function, we make a simple call to “printf()” that will print the text “Hello World” to the terminal. We use “\n” to add a new line at the end of the text.

3. Once you have entered the code into the file, you can now save it.

In nano, you can save and quit by pressing CTRL + X, then Y, and finally ENTER.

4. With our little script written, we can now compile it into a program.

As this program was written in C, we will be using gcc to compile it. This package was installed on our Ubuntu system as a part of the build-essential meta-package.

To compile our script, we need to run the following command on our system.

gcc helloworld.c -o helloworld

Using this command, we use gcc to compile the script we wrote called “helloworld.c“.

We use the “-o” option to tell the compiler to save the compiled version of the script as “helloworld“.

5. Your device should compile the script almost instantly.

Once compiled, we can try running it to verify that everything worked correctly.

./helloworld

From this, you should see the text “Hello World” appear in your command line.

Conclusion

You should now know what the build-essential package is and what gets installed alongside it on Ubuntu.

The meta-package contains everything you need to compile the most basic C and C++ scripts.

During this guide, you will also have gotten a chance to test one of the compilers the build-essential package installs.

If you are still unsure what exactly the build-essential package is or how to install it on Ubuntu, please leave a comment below.

You can also check out our many other Ubuntu guides, such as installing Docker on Ubuntu.

Leave a Reply

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

One Comment

  1. Avatar for Mike
    Mike on

    Very well written instructions. Consider adding method for determining which c++ standard is used in compiler or how to specify which version to compile in.