How to Install G++ (C++ Compiler) on Ubuntu

Welcome to this comprehensive guide on installing and managing different versions of the G++ compiler on Ubuntu. The G++ compiler is a vital tool for developers who need to compile C++ applications, and having the correct version installed can significantly affect the functionality and compatibility of developed software. This tutorial is designed to help students, hobbyists, and professional developers successfully install the G++ compiler on their Ubuntu systems and navigate between different versions as needed. Whether you are setting up a development environment, learning C++, or managing multiple C++ projects, this guide will provide you with detailed steps and explanations to ensure that you have the necessary tools to work efficiently with Ubuntu, a popular choice for developers due to its stability and robust community support.

In this tutorial you will learn:

  • How to install the latest version of G++ using the Ubuntu package manager
  • How to install a specific version of G++
  • How to compile basic C++ code
  • How to set up and manage alternatives for different G++ versions
How to Install G++ (C++ Compiler) on Ubuntu
How to Install G++ (C++ Compiler) on Ubuntu
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Ubuntu 16.04, 18.04, 20.04, 22.04, 24.04 or newer
Software apt package manager
Other Internet connection for downloading packages
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
G++ VS C++: A BEGINNER’S EXPLANATION
G++ and C++ are often mentioned together, but it’s important to understand that C++ is a programming language used for computer software development, which allows procedural, object-oriented, and generic programming features, while G++ is a compiler that translates C++ code into executable machine language so computers can perform tasks written in C++ code. Essentially, think of C++ as the language itself and G++ as a tool that helps turn the instructions written in C++ into actions that the computer can execute.

G++ Compiler Installation Steps on Ubuntu System

1. Install the Build-Essential Package

To install G++ along with all necessary compilers and libraries, the best approach is to install the build-essential package. This package includes G++ and other tools necessary for compiling C++ and other programming languages.

$ sudo apt update && sudo apt install build-essential

This command installs the latest default version of G++ available in your current Ubuntu distribution’s repositories and is sufficient for most users.

2. Installing a Specific Version of G++

If a specific version of G++ is required, perhaps for compatibility or testing purposes, you can install it alongside the default version:

  1. Check for Available G++ Versions: To find out which versions of G++ are available for installation.
    $ apt search '^g\+\+-[0-9]+$'

    This command lists all the available G++ versions. Choose the version that suits your needs.

    Check for Available G++ Versions
    Check for Available G++ Versions
  2. Install a Specific Version of G++: After deciding which version you need, you can install it.
    $ sudo apt install g++-14

    Replace ‘14’ with whichever version number you need.

  3. Verify Installation: Ensure the specified version of G++ is correctly installed.
    $ g++ --version

    This command checks which version is currently active.

3. Test your G++ compiler installation

COMPILING C++ ON UBUNTU: WHAT FILES CAN G++ HANDLE?
G++ is a versatile compiler capable of compiling several types of files containing C++ code. The most common file extensions for C++ source code are .cpp, .cc, and .cxx. Additionally, G++ can also compile .h and .hpp header files which contain declarations used across multiple source files. Aside from these, G++ handles .c files for compiling C code, allowing mixed-language programming alongside C++. This makes G++ a comprehensive tool for developers working with C++ and its related file types, facilitating the development of complex multi-file projects, and executable applications.

Using G++ compiler to to compile basic C++ program and to the test the correctness of the G++ compiler installation on your Ubuntu system.

  1. 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!" << endl;
        cout << "This code is compiled with G++ version: " << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__ << endl;
        return 0;
    }

    Save the above code within hello.cc file.

  2. compile and execute it:
    $ ./hello 
    Hello, World!
    This code is compiled with G++ version: 13.2.0
    Using G++ compiler to compile basic C++ code
    Using G++ compiler to compile basic C++ code



4. Set Up Alternatives for G++ for a quick switch between G++ compiler versions

MANAGING G++ VERSIONS WITH UBUNTU’S UPDATE-ALTERNATIVES PRIORITY
In Ubuntu, the update-alternatives system manages multiple versions of the same program, such as the G++ compiler, using a concept called “priority” to determine which version should be used by default. Each version is assigned a priority number; the version with the highest number becomes the default. This system allows users to easily switch between installed versions based on their needs. For instance, if you install G++ versions 9 and 14, and assign a higher priority to version 14, it will be set as the default compiler. This setup is especially useful in development environments where projects may require different compiler versions.

Setting up alternatives allows you to switch between different installed versions of G++ easily:

  1. Add the Default G++ to Alternatives: First, add the default version of G++ to the alternatives system.
    $ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 20

    Here, ‘g++-9’ is the default version, and ‘20’ is its priority. Adjust the version and priority according to your needs.

  2. Add Other Versions: Add any other installed versions.
    $ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-14 50

    This command adds G++ 14 to alternatives and sets it as a higher priority, making it the default version.

  3. Switch Between G++ Compiler Versions: You can switch between the versions using the following command.
    $ sudo update-alternatives --config g++

    This will prompt you to select which version of G++ you wish to be the default by typing the selection number.

    Switching between G++ versions on Ubuntu
    Switching between G++ versions on Ubuntu

Conclusion

By following these steps, you can install G++ on Ubuntu and set up a flexible development environment by managing multiple versions of G++. This setup is particularly beneficial for developers needing to switch between versions based on project requirements. Now, you’re ready to compile and run C++ programs efficiently, enhancing your development workflow on Ubuntu systems.