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

| 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 |
sudo apt update && sudo apt install build-essential. This installs the latest G++ compiler along with essential development tools. Verify installation with g++ --version.
| 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.
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.
- 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 - 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.
- 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
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 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.
- First, create a basic C++ code source. For example, let’s create a hello world C++ program. Save the following code as
hello.cctext 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.ccfile. Subsequently, you’ll compile this test program. - 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
4. Set Up Alternatives for G++ for a quick switch between G++ compiler versions
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. Therefore, when you install G++ Ubuntu provides a convenient way to manage multiple compiler versions.
- 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.
- 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.
- 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
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.


