How to install G++ the C++ compiler on Ubuntu 20.04 LTS Focal Fossa Linux

G++, the GNU C++ Compiler is a compiler in Linux which was developed to compile C++ programs. The file extensions that can be compiled with G++ are .c and .cpp. The aim of this tutorial is to install G++ the C++ compiler on Ubuntu 20.04 LTS Focal Fossa Linux. This will be achieved by installing the build-essential package.

In this tutorial you will learn:

  • How to install G++ compiler on Ubuntu 20.04
  • How to Check C++ compiler version
  • How to Compile basic C++ program from source code
  • How to run G++ to compile C++ program

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Installed or upgraded Ubuntu 20.04 Focal Fossa
Software GCC
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Installing G++ the C++ compiler on Ubuntu 20.04 step by step instructions

Although you can install the C++ compiler separately by installation of the gcc package, the recommended way to install the C++ compiler on Ubuntu 20.04 is by installation of the entire development package build-essential.

  1. Install C++ compiler by installation of the development package build-essential:
    $ sudo apt install build-essential
    
  2. DO YOU NEED MULTIPLE C AND/OR C++ COMPILER VERSIONS?
    Visit “How to switch between multiple GCC and G++ compiler versions on Ubuntu 20.04” to see how to install multiple compiler versions on the same Ubuntu system.
  3. Check C compiler version:
    $ g++ --version
    gcc (Ubuntu 9.2.1-17ubuntu1) 9.2.1 20191102
    


  4. Create a basic C++ code source. For example let’s create hello world C++ program. Save the following code as hello.cc text file:
    #include <iostream>
    using namespace std;
    
    int main() 
    {
        cout << "Hello, World!";
        return 0;
    }
    

    Save the above code within hello.cc file, compile and execute it:

    $ g++ -o hello hello.cc 
    $ ./hello 
    Hello, World!