1

I used the information here How to install g++ 10 on Ubuntu 18.04? to install gcc/g++ 10 on Ubuntu 20 and when I try to do it again:

sudo apt install g++-10

Reading package lists... Done Building dependency tree
Reading state information... Done g++-10 is already the newest version (10.2.0-5ubuntu1~20.04). 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

but when I ask for the version:

g++ --version

g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Why is that and how can I fix it?

Update

However when I try:

g++-10 --version

g++-10 (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.0 Copyright (C) 2020 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

It seems like g++10 is installed and usable under the name g++-10 but how can I make it the default g++ compiler?

newbie
  • 113

1 Answers1

5

You'll need to use update-alternatives to specify which version of g++ should be used by default. Here's how:

  1. Open Terminal (if it's not already open)
  2. Configure update-alternatives:
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 40
    sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 60
    sudo update-alternatives --config g++
    

After running the last command, you'll be presented with a menu of g++ versions and you will have the option to choose your default g++ version. The message will look something like this:

Press <enter> to keep the current choice[*], or type selection number:

Type the number from the menu you would like to have as default, then press Enter. If you need to change the default version of g++ again in the future, use the command again.

Note: If you need to change the default version of gcc as well, follow this same procedure, replacing g++ with gcc. Do not forget to change the version numbers as well.

Hope this helps