Cannot install Git on Ubuntu 16.04 LTS

When I was trying to install Git on Ubuntu 16.04, I get an error. The command I used to install git is:

sudo apt-get install git

The error I encountered was:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
  git : Depends: liberror-perl but it is not going to be installed
        Depends: git-man (> 1:2.7.4) but it is not going to be installed
        Depends: git-man (< 1:2.7.4-.) but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or
specify a solution).

Even though I used the suggested command (apt-get -f install) to install git the error still persists. Anyone who has the idea to fix this problem?


Some times our systems may not be up-to-date to receive an install so we can update with:

  • update package information:

    sudo apt-get update
    
  • upgrade packages on system and fix broken packages in the process:

    sudo apt-get -f dist-upgrade
    
  • only fix broken packages:

    sudo apt-get -f install
    

I had the same problem with liberror-perl when trying to install git. The other answers (sudo apt-get update && sudo apt-get dist-upgrade && apt-get -f install) didn't work for me.

From manually following the chain of dependency problems by trying to install each package directly, it looks like the problem is with perl-base:

$ sudo apt install liberror-perl
  liberror-perl : Depends: perl but it is not going to be installed

$ sudo apt install perl
  perl : Depends: perl-base (= 5.22.1-9) but 5.22.1-9ubuntu0.5 is to be installed

$ sudo apt install perl-base
  perl-base is already the newest version (5.22.1-9ubuntu0.5).

So the perl package depends on an outdated version of perl-base. I'm not sure how that was caused, but I suspect at one point a newer version was available, perhaps from a temporary apt source that was later removed on my system. I fixed the problem by downgrading perl-base to the version perl wants:

$ sudo apt install -f perl-base=5.22.1-9

After that, git installs properly. Just in case there was a newer perl-base version available, I tried upgrading it, but the above version was also the latest version:

$ sudo apt install perl-base=\*
perl-base is already the newest version (5.22.1-9).
Selected version '5.22.1-9' (Ubuntu:16.04/xenial [amd64]) for 'perl-base'

As far as I know this should not cause any problems, but YMMV.