How to Install G++ on Ubuntu?

Compiler is a software program that converts the code written in high-level programming into a low-level machine-readable language. For example, the Linux compiler has various types, such as “AOOC”, “GHC”, “clang”, “G++”, and many more. The “G++” compiler is pre-installed on the latest version of Ubuntu 22.04. If it is not available, follow this article in which the installation method of G++ on Ubuntu is described in detail.

The content of this guide is illustrated below:

  1. What is G++ on Ubuntu?
  2. How to Install G++ on Ubuntu? 
  3. Method 1: Using the apt Package Manager
  4. Method 2: Using Build-Essential Package
  5. Method 3: Using PPA Repository
  6. Method 4: Using the Source Code
  7. How to Run/Execute the C++ Program Using G++ Compiler on Ubuntu?
  8. How to Install Various G++ Compilers on Ubuntu? 
  9. How to Remove G++ From Ubuntu?
  10. How to Fix/Resolve the “gcc no such file or directory” Error in Ubuntu?
  11. How to Install GCC on Ubuntu 22.04?
  12. How to Compile a C++ File in Linux?

What is G++ on Ubuntu?

G++ is a compiler for the C++ language. It is part of the GNU Compiler Collection (GCC), which is a free and open-source software project. It can compile files with .cpp and .c extensions on Ubuntu via this guide. There are several methods to install G++ on Ubuntu, depending on your preferences and needs. 

How to Install G++ on Ubuntu?

The main objective of the G++ compiler is to compile the  C++ source code files into executable files, which can be executed from the Linux command line. This section demonstrates the step-by-step procedure to install the G++ compiler in the Linux distribution Ubuntu.

Method 1: Using the apt Package Manager

To install G++ on Ubuntu, users can use the apt package manager. It is a tool that allows users to install, update, and remove software packages on Ubuntu. To install G++ via apt, follow these steps:

Step 1: Update the System Packages

First, press the shortcut key (Ctrl+Alt+T) to open the terminal on the desktop. Type the “apt update” command to update all the system packages with the sudo privileges:

sudo apt update

Step 2: Install the G++ Compiler

Now, install the “g++” compiler with the help of apt package manager. The “G++” compiler comes by default in Ubuntu 22.04, as shown in the below screenshot:

sudo apt install g++

The output shows that g++ is already installed with the newest “11.2.0” version.

If Ubuntu system does not have the “G++” compiler then the output would be like shown in the following image:

The output displays that the “G++” is successfully installed.

Step 3: Check the G++ Compiler Version 

Once the installation is completed, check the “G++” compiler version by typing the below-mentioned command on the terminal:

g++ --version

The latest version of g++ “11.4.0” has been installed on Ubuntu 22.04.

This method installs the latest version of G++ along with its dependencies from the Ubuntu repositories. Let’s move on to the second method.

Method 2: Using Build-Essential Package

This method is an alternative way to get G++ on Ubuntu. If the g++ is not available in the official repository, then first install the “build-essential” packages tool. To do so, run the following command on the terminal:

sudo apt install build-essential libgmp-dev libmpfr-dev libmpc-dev

The “build-essential” tool is successfully installed along with G++.

Verify the G++ Compiler Version 

Once the installation is completed, check the “G++” compiler version by typing the below-mentioned command on the terminal:

g++ --version

The latest version of g++ “11.4.0” has been installed in Ubuntu 22.04.

Method 3: Using PPA Repository

G++ is part of the GNU Compiler Collection (GCC), which also includes compilers for other languages such as C, Fortran, Ada, and Java. To install G++ on Ubuntu, use the official PPA repository maintained by the GCC developers.

Here are the steps to do so:

Step 1: Add the PPA Repository 

Open a terminal and enter the below script to add the PPA repository to your system:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test

Step 2: Update System Repository

Update the system to fetch the latest packages from the PPA repository via the below command:

sudo apt update

Step 3: Install the G++ Compiler

Now, type the below command to install the “G++” compiler in Ubuntu 22.04, as shown in the below screenshot:

sudo apt install g++

The output displays that the “G++” is successfully installed.

Step 4: Check the G++ Compiler Version 

Once the installation is completed, check the “G++” compiler version by typing the below-mentioned command on the terminal:

g++ --version

The latest version of g++ “11.4.0” has been installed in Ubuntu 22.04.

You have installed G++ successfully on Ubuntu using the PPA repository. You can now use it to compile and run your C++ programs.

Method 4: Using the Source Code

If you want to have more control over the installation process or use the latest features of G++, compile it from the source code. This requires more time and skills, but it can be rewarding if you are interested in how G++ works. 

To install G++ from the source code, you need to follow these steps:

Step 1: Download Source Code of GCC

Download the source code of GCC from https://gcc.gnu.org/releases.html official site. Users can download the most recent version based on the official release:

wget https://ftp.gnu.org/gnu/gcc/gcc-13.2.0/gcc-13.2.0.tar.gz

In this way, the “tar.gz” file has been downloaded.

Step 2: Extract the Archive and Change to the Directory

Now, extract the archive via the “tar” command along with the “xf” option to force extraction. Then, change to the directory where the user wants to install G++. Let’s execute the command: 

tar -xf gcc-13.2.0.tar.gz && cd gcc-13.2.0

The compressed file has been extracted.

Step 3: Configure the Build Options

After navigating to the directory where users extracted GCC, run the below command to configure the build options:

./configure -v --build=x86_64-linux-gnu --enable-checking=release --host=x86_64-linux-gnu --target=x86_64-linux-gnu --prefix=/usr/local/gcc-13.2.0 --enable-languages=c,c++ --disable-multilib --program-suffix=-13.2.0

It takes a lot of time to build the configuration.

Important: If the user faces errors, install the required dependencies for GCC by running the below command:

sudo apt install build-essential libgmp-dev libmpfr-dev libmpc-dev

It reconfigures all dependent packages.

Step 4: Compile the Source Code

Run the command “make” to compile GCC and G++. It takes a while depending on the system specifications:

make

In this way, a compilation of GCC and G++ has been done.

Step 5: Installed Binaries

To install GCC and G++ to the specified prefix directory, run the below command:

sudo make install

In this way, binary files associated with the GCC have been executed.

Step 6: Verify G++ Version

Verify that G++ is installed by running the command “g++ –version”. It see the version number of G++ that you just installed:

g++ --version

The output shows that the latest G++ version “11.4.0” has been installed on Ubuntu.

How to Run/Execute the C++ Program Using G++ Compiler on Ubuntu?

The above section verifies that the “G++” is installed and is now ready for the compilation of the “C++” program. In this section, we will create a “C++”  program and compile it using the “G++” compiler. Follow the step-by-step instructions to perform this task:

Step 1: Create a C++ Program

Ubuntu 22.04 offers the “nano file editor” that can be used to edit the files having shortcut keys for various tasks. To create the basic “myFirstProgram” C++ program, run the below-written command in the terminal:

nano myFirstProgram.cpp

The above command opens the “nano editor” window to write the program in .cpp extension:

#include <stdio.h>
int main()
{
  printf ("itslinufoss\n");
  return 0;
}

Now, users can write their own code to execute in the terminal.

Save the program and press the “Ctrl+X” to exit the nano editor.

Step 2: Compile the Program

Now, convert “myFirstProgram.cpp” into an executable “myFirstProgram” file. Let’s compile the “myFirstProgram” C++ program by executing the command on the terminal:

g++ -o myFirstProgram myFirstProgram.cpp

In the “Files” window, there is a “myFirstProgram” C++ program available in the “Home” directory:

Step 3: Run the Executable File

After that, type the below-written command on the terminal to show the output of the “myFirstProgram” on the terminal:

./myFirstProgram

The output “itslinufoss” is displayed on the terminal.

How to Install Various G++ Compilers on Ubuntu? 

To install multiple G++ compiler versions on Ubuntu 22.04, execute the “apt” command by mentioning the G++ compiler names. For instance, specify the “g++-9” and “g++-10” in the terminal:

sudo apt install g++-9 g++-10

In this way, multiple G++ compilers have been installed in the current system.

How to Remove G++ From Ubuntu?

The removal method of the G++ compiler is quite easy just by executing the “apt autoremove” command on the terminal. To remove the G++ compiler with dependencies and temporary files, execute the below script:

sudo apt remove g++       # Remove Package sudo apt autoremove g++   # Remove Package and Dependencies
sudo apt purge g++        # Remove Package, Dependencies and Temporary Files

Hence, the “G++” compiler has been removed along with dependencies via the “autoremove” command.

How to Fix/Resolve the “gcc no such file or directory” Error in Ubuntu?

One possible reason/cause of this error in Ubuntu is that the gcc package is not installed or updated. To fix this, run the “sudo apt install gcc” command in the terminal. Or users can execute “sudo apt install build-essential”. This will update the package list and install the build-essential package, which includes gcc and other tools for compiling C programs. 

To read more about the error, users can navigate to this “gcc no such file or directory”.

How to Install GCC on Ubuntu 22.04?

To install GCC on Ubuntu 22.04, you need to open a terminal and type the “sudo apt update && sudo apt install build-essential” command. This will update your system and install the GCC compiler along with other essential tools for building software.

To investigate more about it, users can follow this guide “Install GCC on Ubuntu”.

How to Compile a C++ File in Linux?

To compile a C++ file in Linux, you need to install a compiler such as g++ or clang++. Then, you can use the code line to invoke the compiler with the name of the source file and the desired output file.

To explore more about this query, check out our detailed guide on “Compile a C++ file in Linux”.

Conclusion

On Ubuntu, the G++ compiler can be installed using the command “sudo apt install g++” or ”sudo apt install build-essential” package. On recent versions of Ubuntu, the G++ package comes by default. However, if not, you have learned the ways to install it on your Ubuntu system. Apart from that, you have also learned to get G++ on Ubuntu by installing the build-essential-packages. This guide has explained possible methods to install a G++ compiler on Ubuntu.