Install g++ 11 on Ubuntu 20.04

The g++ is a compiler of the GNU Compiler Collection (GCC). The g++ compiler is mainly used to compile C++ programs.

This tutorial explains how to install g++ 11 on Ubuntu 20.04.

Install g++

Add the Toolchain repository:

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

Install g++ 11 by using the following command:

sudo apt install -y g++-11

Now you can verify that the installation finished successfully by checking g++ version:

g++-11 --version

Testing g++

Create a main.cpp file:

nano main.cpp

Add the following lines of code:

#include <iostream>
 
int main() {
    std::cout << "Hello world" << std::endl;
 
    return 0;
}

Compile a code with command:

g++-11 main.cpp -o test

Run a program:

./test

Uninstall g++

If you decided to completely remove g++ and related dependencies, execute the following command:

sudo apt purge --autoremove -y g++-11

Remove GPG key and repository:

sudo rm -rf /etc/apt/trusted.gpg.d/ubuntu-toolchain-r_ubuntu_test.gpg
sudo rm -rf /etc/apt/sources.list.d/ubuntu-toolchain-r-ubuntu-test-focal.list

Leave a Comment

Your email address will not be published. Required fields are marked *