VOOZH about

URL: https://linuxconfig.org/how-to-install-g-the-c-compiler-on-ubuntu-20-04-lts-focal-fossa-linux

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


Skip to content

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.

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

Software Requirements

👁 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 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
To install G++ on Ubuntu, open your terminal and run: sudo apt update && sudo apt install build-essential. This installs the latest G++ compiler along with essential development tools. Verify installation with g++ --version.
Quick Steps to Install G++ Ubuntu
Step Command/Action
1. Update package list sudo apt update
2. Install build-essential sudo apt install build-essential
3. Verify installation g++ --version
4. Test with sample code g++ hello.cc -o hello

G++ Compiler Installation Steps on Ubuntu System

1. Install the Build-Essential Package

To install G++ along with all necessary compilers and libraries on Ubuntu, 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. Therefore, when you install G++ Ubuntu systems benefit from having all essential development tools in one package.

$ 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. After you install G++ Ubuntu will have all the necessary compilation tools ready to use.

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. This flexibility allows developers to install G++ Ubuntu versions that match their project requirements.

  1. Check for Available G++ Versions: Before you install G++ Ubuntu provides a way to find out which versions are available.
    $ apt search '^g\+\+-[0-9]+$'

    This command lists all the available G++ versions. Choose the version that suits your needs. Consequently, you can install G++ Ubuntu versions that are specifically required for your development work.

    👁 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. Moreover, when you install G++ Ubuntu allows multiple versions to coexist on the same system.
    $ sudo apt install g++-14

    Replace ’14’ with whichever version number you need. As a result, you successfully install G++ Ubuntu specific versions for different projects.

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

    This command checks which version is currently active after you install G++ Ubuntu on your system.

3. Test Your Install G++ Ubuntu Compiler

Using the G++ compiler to compile a basic C++ program tests the correctness of your installation. Furthermore, after you install G++ Ubuntu systems need verification to ensure everything works properly.

  1. First, create a basic C++ code source. For example, let’s create a 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. Subsequently, you’ll compile this test program.

  2. Next, compile and execute it:
    $ g++ hello.cc -o hello
    $ ./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

Setting up alternatives allows you to switch between different installed versions of G++ easily. Therefore, when you install G++ Ubuntu provides a convenient way to manage multiple compiler versions.

  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. Consequently, after you install G++ Ubuntu will use this priority system for version management.

  2. Add Other Versions: Next, 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. As a result, when you install G++ Ubuntu systems can easily switch between versions.

  3. Switch Between G++ Compiler Versions: Finally, 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. Therefore, even after you install G++ Ubuntu allows flexible version switching.

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

Conclusion

By following these steps, you can successfully install G++ 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. Additionally, the official GCC documentation provides comprehensive information about compiler features and options. For more information about Ubuntu’s G++ packages, visit the official Ubuntu package repository. Now, you’re ready to compile and run C++ programs efficiently, enhancing your development workflow on Ubuntu systems.