How to install G++ the C++ compiler on Ubuntu 20.04 LTS Focal Fossa Linux - LinuxConfig.org

G++, the GNU C++ Compiler is a compiler in Linux which was developed to compile C++ programs. The file extensions that can be compiled with G++ are .c and .cpp. The aim of this tutorial is to install G++ the C++ compiler on Ubuntu 20.04 LTS Focal Fossa Linux. This will be achieved by installing the build-essential package.
This is a companion discussion topic for the original entry at https://linuxconfig.org/how-to-install-g-the-c-compiler-on-ubuntu-20-04-lts-focal-fossa-linux

i am facing this type of problem for installing gcc

sudo apt install build-essential
Reading package lists… Done
Building dependency tree
Reading state information… Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
build-essential : Depends: gcc (>= 4:9.2) but 4:7.4.0-1ubuntu2.3 is to be installed
Depends: g++ (>= 4:9.2) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

Hi Pk123456,

Welcome to our forums.

Your problem may be related to the recent release of this major version, which cause many changes in the repositories. Try to clean your apt cache:

$ sudo apt clean

And re-run the installation command. Let us know it that works for you.

I am also encountering the same issues. Performing the apt clean did not resolve the issue.

$sudo apt-get update

sudo apt-get install build-essential
Reading package lists… Done
Building dependency tree
Reading state information… Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
build-essential : Depends: gcc (>= 4:9.2) but 4:7.4.0-1ubuntu2.3 is to be installed
Depends: g++ (>= 4:9.2) but 4:7.4.0-1ubuntu2.3 is to be installed
E: Unable to correct problems, you have held broken packages.

$sudo apt clean

$sudo apt install build-essential
Reading package lists… Done
Building dependency tree
Reading state information… Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
build-essential : Depends: gcc (>= 4:9.2) but 4:7.4.0-1ubuntu2.3 is to be installed
Depends: g++ (>= 4:9.2) but 4:7.4.0-1ubuntu2.3 is to be installed
E: Unable to correct problems, you have held broken packages.

Hi Salty_mn_fan,

Welcome to our forums.

I have tested the installation of the build-essential package on one of my Ubuntu 20.04 machines, and it works without a problem. I suggest to upgrade your system first:

$ sudo apt-get update

$ sudo apt-get upgrade

And don’t forget to reboot your system after the upgrade process. When all this is done, try the installation again.

I have followed all the suggestions above, i.e.
$sudo apt-get update
$sudo apt-get upgrade
Then restarted my Ubuntu 20.04.3 LTS machine and the results are essentially the same as reported above, i.e. in my case:
$sudo apt clean
$sudo apt install build-essential
Reading package lists… Done
Building dependency tree
Reading state information… Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable distribution
that some required packages have not yet been created or been moved out of
Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
build-essential : Depends: gcc (>= 4:9.2) but 4:7.4.0-1ubuntu2.3 is to be installed
Depends: g++ (>= 4:9.2) but 4:7.4.0-1ubuntu2.3 is to be installed
E: Unable to correct problems, you have held broken packages.

Suggestions please on how to resolve this issue? Thank you

I have solved the problem on my system. Instead of using apt, I used aptitude. To install aptitude:
$sudo apt install aptitude
Then
$sudo aptitude install build-essential

This is where it gets interesting. Pay close attention to what it proposes to do where you have to answer 'Accept this solution? [Y/n/q/?] and don’t automatically say Y the first time as it may well propose not installing build-essential. In my case I selected n and it gave a different proposal of what it was going to do which involved removing a number of older packages and upgrading them. I selected n for several iterations of proposals until finally selecting Y and it removed a number of items which got things closer, but build-essential still not installed. I repeated the process, ie. re-entered
$sudo aptitude install build-essential
and went through the process above again. Things got closer again until finally when
$sudo aptitude install build-essential
was entered it reported that build-essential is already installed.

One last note… my system was originally a 16.04 LTS, then upgraded to 18.04 LTS and finally 20.04 LTS. One thing I noticed in the feedback list given by aptitude were lines like:
Depends: gcc-9-base (=9.3.0-17ubuntu1~20.04) but 9.4.0-1ubuntu1~16.04 is installed. I get the impression remnants of my older versions are what caused the original problems. Hope this helps!

I found the solution. Basically, use aptitude instead of apt:
$sudo aptitude install build-essential

Pay attention to the proposed solutions aptitude asks you to accept. The first one may well be to not install build-essential. If you select n, it proposes a different solution which involves removing and upgrading packages, each proposed solution more involved. Select Y and you may have to repeat the process until it finally reports build-essential is installed. Hope this helps.