Cannot install build-essential/gcc/g++ after upgrade to 17.04

Since upgrading from Ubuntu 16.04 to 17.04, I can no longer install gcc-7 or g++-7 from the http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu repository. In fact I cannot even install build-essential.

Under Software & Updates > Other Software, the repository is listed like this:

http://ppa.launchpad.net/ubuntu-toolchain-r/test/ubuntu zesty main

Trying to install build-essential:

$ 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:5.2) but it is not going to be installed
                   Depends: g++ (>= 4:5.2) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

However:

$ gcc --version
gcc (Ubuntu 5.4.1-8ubuntu1) 5.4.1 20170304
...
$ g++ --version
g++ (Ubuntu 5.4.1-8ubuntu1) 5.4.1 20170304
...

So how can I install build-essential again?


So I was able to install build-essential again by following these steps:

$ sudo apt install build-essential
...
The following packages have unmet dependencies:
 build-essential : Depends: gcc (>= 4:5.2) but it is not going to be installed
                   Depends: g++ (>= 4:5.2) but it is not going to be installed

A gcc version greater 5.2 is required, so lets try to install gcc.

$ sudo apt install gcc
...
The following packages have unmet dependencies:
gcc : Depends: cpp (>= 4:6.3.0-2ubuntu1) but it is not going to be installed
      Depends: gcc-6 (>= 6.3.0-9~) but it is not going to be installed

Okay. Something else is missing. Let’s try installing that.

$ sudo apt install cpp
...
The following packages have unmet dependencies:
 cpp : Depends: cpp-6 (>= 6.3.0-9~) but it is not going to be installed

Still nothing. Trying again.

$ sudo apt install cpp-6
...
The following packages have unmet dependencies:
 cpp-6 : Depends: gcc-6-base (= 6.3.0-12ubuntu2) but 6.3.0-18ubuntu2~16.04 is to be installed

Once again.

$ sudo apt install gcc-6-base
...
gcc-6-base is already the newest version (6.3.0-18ubuntu2~16.04).

So here I don’t know what exactly went wrong. It should be noted that there is a 16.04 string at the end of that version number. That seemed odd. I removed that package.

$ sudo apt remove gcc-6-base

Now I was able to install build-essential again. This will also reinstall gcc-6-base.

$ sudo apt install build-essential

Remaining problem:

$ sudo apt upgrade
...
The following packages have been kept back:
  cpp g++ gcc

Root of the problem is similar as above, gcc-7-base is faulty but I can’t simply remove it, since I get this:

The following packages have unmet dependencies:
 libgcc1 : Depends: gcc-7-base (= 7.1.0-5ubuntu2~16.04) but it is not going to be installed
 va-driver-all : Depends: mesa-va-drivers but it is not going to be installed or
                          vdpau-va-driver but it is not going to be installed
E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.

Danger Zone:

I managed to resolve this issue with the following steps:

$ sudo apt install aptitude
$ sudo dpkg --force-all -P gcc-7-base
$ sudo dpkg --force-all -P gcc-7-base:i386
$ sudo aptitude install gcc-7-base

This is super dangerous and might break your installation. Executing dpkg --force-all -P will remove a package forcefully, ignoring any dependencies on this package. Following this by a regular apt upgrade might remove almost all relevant software from your installation.

I was only able to recover from there by usind aptitude which resolved every dependency issue I had.


I was facing the same issue while upgrading from 14.04 LTS to 16.04 LTS. I have written the detailed solution here. But for the sake of convenience I will reproduce the key points here.

Remove all dependent libraries (autoremove) and install all missing libraries using sudo apt-get -f install (-f means --fix-missing)

sudo apt-get autoremove
sudo apt-get -f install
sudo apt-get update
sudo apt-get upgrade 

Install ubuntu toolchain from PPA

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

Install aptitude.

sudo apt install aptitude

Recursively try installing the broken libraries until you solve the conflict by upgrading or fixing a specific library. In my case I had to fix libstdc++6

$ sudo aptitude -f install build-essential
The following NEW packages will be installed:
  build-essential cpp{a} cpp-5{a} g++{a} g++-5{a} gcc{a} gcc-5{a} libasan2{a} libcc1-0{ab} 
  libcilkrts5{a} libgcc-5-dev{a} liblsan0{ab} libmpx0{a} libstdc++-5-dev{a} 
The following packages will be upgraded:
  gcc-5-base libstdc++6{b} 
2 packages upgraded, 14 newly installed, 0 to remove and 5 not upgraded.
Need to get 29.6 MB of archives. After unpacking 100 MB will be used.
The following packages have unmet dependencies:
 liblsan0 : Depends: gcc-9-base (= 9.3.0-10ubuntu2~16.04) but 9.3.0-11ubuntu0~14.04 is installed.
 libcc1-0 : Depends: gcc-9-base (= 9.3.0-10ubuntu2~16.04) but 9.3.0-11ubuntu0~14.04 is installed.
 libstdc++6 : Depends: gcc-9-base (= 9.3.0-10ubuntu2~16.04) but 9.3.0-11ubuntu0~14.04 is installed.

As build-essential needs gcc-5.

$ sudo apt-get -f install gcc-5

The following packages have unmet dependencies:
 gcc-5 : Depends: cpp-5 (= 5.5.0-12ubuntu1~16.04) but it is not going to be installed
         Depends: gcc-5-base (= 5.5.0-12ubuntu1~16.04) but 5.4.0-6ubuntu1~16.04.12 is to be installed
         Depends: libcc1-0 (>= 5.5.0-12ubuntu1~16.04) but it is not going to be installed
         Depends: libgcc-5-dev (= 5.5.0-12ubuntu1~16.04) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

But gcc-5 in turn depends on cpp-5

$ sudo apt-get -f install cpp-5

The following packages have unmet dependencies:
 cpp-5 : Depends: gcc-5-base (= 5.5.0-12ubuntu1~16.04) but 5.4.0-6ubuntu1~16.04.12 is to be installed

cpp-5 depends on gcc-5-base. Here you can see there is a specific conflict. Aptitide gives multiple choices to solve the conflict. In my case upgrading the libstdc++6 solvd the problem.

$ sudo aptitude -f install gcc-5-base
The following packages will be REMOVED:  
  gcc-5-base{u} 
0 packages upgraded, 0 newly installed, 1 to remove and 6 not upgraded.
Need to get 0 B of archives. After unpacking 67.6 kB will be freed.
The following packages have unmet dependencies:
 libstdc++6 : Depends: gcc-5-base (= 5.4.0-6ubuntu1~16.04.12) but it is not going to be installed.
open: 115; closed: 488; defer: 35; conflict: 58                                                          .The following actions will resolve these dependencies:

     Keep the following packages at their current version:                        
1)     gcc-5-base [5.4.0-6ubuntu1~16.04.12 (now, xenial-security, xenial-updates)]

Accept this solution? [Y/n/q/?] n
The following actions will resolve these dependencies:

      Upgrade the following packages:                                                                     
1)      libstdc++6 [5.4.0-6ubuntu1~16.04.12 (now, xenial-security, xenial-updates) -> 9.3.0-10ubuntu2~16.0

Finally, now the conflict is resolved try installing build-essential again.

$ sudo aptitude -f install build-essential

Good Luck!