How to install G++ the C++ compiler on Ubuntu 22.04 LTS Jammy Jellyfish Linux

G++, the GNU C++ Compiler is a compiler in Linux systems 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 22.04 LTS Jammy Jellyfish Linux. This will be achieved by installing the build-essential package.

In this tutorial you will learn:

  • How to install G++ compiler on Ubuntu 22.04
  • How to Check C++ compiler version
  • How to Compile basic C++ program from source code
  • How to run G++ to compile C++ program
Using G++ to compile C++ program and executing Hello World program
Using G++ to compile C++ program and executing Hello World program
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu 22.04 Jammy Jellyfish
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 22.04 step by step instructions



Twitter icon Follow LinuxConfig.org on Twitter for the latest tips and tricks about Linux!


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

  1. Get started by opening a command line terminal and typing the following two commands to install C++ compiler by installation of the development package build-essential:
    $ sudo apt update
    $ sudo apt install build-essential
    
    DO YOU NEED MULTIPLE C AND/OR C++ COMPILER VERSIONS?
    Visit “How to switch between multiple GCC and G++ compiler versions on Ubuntu 22.04” to see how to install multiple compiler versions on the same Ubuntu system.
  2. Check C compiler version to verify installation:
    $ gcc --version
    gcc (Ubuntu 11.2.0-16ubuntu1) 11.2.0
    
  3. 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 
    using namespace std;
    
    int main() 
    {
        cout << "Hello, World!";
        return 0;
    }
  4. Save the above code within hello.cc file, compile and execute it:
    $ g++ -o hello hello.cc 
    $ ./hello 
    Hello, World!
    

Closing Thoughts

In this tutorial, we saw how to install G++, the very simple C++ compiler on Ubuntu 22.04 Jammy Jellyfish. We also learned how to create a simple Hello World C++ program in order to test the new compiler.