Categories: TutorialsUbuntu

How to Install g++ in Ubuntu

Before we get started, you need to understand that a computer cannot directly give commands to a computer to execute. A computer only understands ones and zeros. If you want your computer to understand and execute what you want it to do, you need to program certain commands using a high-level programming language such as C++, etc. But wait, you cannot just write C++ code and expect your computer to execute the commands on its own, you need a compiler to convert your code into machine language, which your computer can easily understand. Or simply put, a compiler converts your code into executables for your operating system.

Sponsored

Not knowing about a tech ecosystem can make even the smallest tasks seem cumbersome. For anyone starting with their programming journey, getting the environment set up is the biggest task. In Windows you get all-encompassing solutions that set up everything for you, such isn’t the case on Ubuntu. While there are PPAs and packages, setting up a programming environment for C++ isn’t as simple. So, let’s dive deep into it and learn how to install g++ on Ubuntu 20.04.

What is g++?

g++ is a compiler for Linux based systems, used to compile C++ programs. It can compile files with both “.c” and “.cpp” file extensions. g++ creates the executable by compiling the file, pre-processing, linking and then ultimately assembling the source code. It comes loaded with a plethora of commands to handle our C++ code.

Installing g++

As always, we will be utilizing the terminal for the installation process. You can fire it up either through the applications search bar as such:

Or if you want a quicker way, you can go with pressing “Ctrl + Alt + T” on your keyboard. Now proceed to update the system repositories through our trusty old command:

$sudo apt update

After this, you are ready to install g++, with:

$sudo apt install g++

After confirming that you are willing to give up 121MB of your HDD space to proceed with the installation, the installation will proceed and complete depending on the speed of your computer.

Verifying the installation

Once the installation has been completed, you can verify the installation by running the following:

$g++ --version

This shows you the version and confirms that the g++ compiler has been installed on your Ubuntu.

Compiling C++ with g++

Now that we have the compiler installed, let’s take a look at the steps you need to do for compiling a C++ program.

Let’s use the nano editor to create our C++ code file.

Sponsored
$nano helloWorld.cpp

Here helloWorld is the name of our file. You can keep the name of the file as you like. Now open up the file and let’s type the following basic C++ code. It’s almost like a ritual that anyone who starts programming starts with a Hello World program.

#include 

 int main ()

 {

                cout

                return 0;

 }

Here we are just displaying “Hello World” on the terminal. Save the file and exit out of it by pressing “Ctrl + X”. Now we are going to compile this with the following:

$g++ helloWorld.cpp -o helloWorld

g++ takes up time to compile your application depending on the amount of code in your file. In my case, it was done instantaneously. After running this, you will see a new file is created in your current directory.

Here the .cpp file is the one with the code and the one without any file extension is the executable. Now we run the newly created executable file, through this:

$./helloWorld

As you can see the terminal has our coded text displayed right before the prompt. This shows that we have been successful in achieving our objective of setting up the g++ compiler and have succeeded in compiling and then executing our first C++ code on Ubuntu.

Conclusion

C++ is a very popular programming language for back-end development. It’s an awfully fast and even more so efficient language. Many of the industry-leading tools are leveraging C++ at their back-end.

If you have followed all of these steps carefully, you are now ready to start writing C++ code on your Ubuntu machine. If you run into issues with any of these commands, or if you want to know more about programming in C++, feel free to drop a message below, and let’s have a chat.

Ubuntu Server Admin

Recent Posts

Canonical Releases Ubuntu 25.04 Plucky Puffin

The latest interim release of Ubuntu introduces “devpacks” for popular frameworks like Spring, along with…

2 days ago

Ubuntu 25.04 (Plucky Puffin) Released

Ubuntu 25.04, codenamed “Plucky Puffin”, is here. This release continues Ubuntu’s proud tradition of integrating…

2 days ago

Extended Security Maintenance for Ubuntu 20.04 (Focal Fossa) begins May 29, 2025

Ubuntu released its 20.04 (Focal Fossa) release 5 years ago, on March 23, 2020. As…

3 days ago

Ubuntu 20.04 LTS End Of Life – activate ESM to keep your fleet of devices secure and operational

Focal Fossa will reach the End of Standard Support in May 2025, also known as…

4 days ago

Ubuntu MATE 25.04 Release Notes

Ubuntu MATE 25.04 is ready to soar! 🪽 Celebrating our 10th anniversary as an official…

4 days ago

Ubuntu Weekly Newsletter Issue 887

Welcome to the Ubuntu Weekly Newsletter, Issue 887 for the week of April 6 –…

5 days ago