How to fix this apt mess?

Firefox is trying to create a diversion, when a diversion with that name already exists.

You need to remove the existing diversion first.

This is the error line:

dpkg-divert: error: 'diversion of /usr/bin/firefox to /usr/bin/firefox.real by firefox-esr' clashes with 'diversion of /usr/bin/firefox to /usr/bin/firefox.real by purebrowser'

From this solution:

sudo dpkg-divert --remove /usr/bin/firefox

Note: if the diversion is not caused by /usr/bin/firefox, you may need to identify the diversion with either sudo dpkg-divert --list | grep firefox or sudo dpkg-divert --list | grep purebrowser On a side note, if you don't like the locale error message thrown by Perl, you can get rid of it by exporting your locale, as shown here:

export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8

If all else fails, open up the package record and remove iceweasel by hand:

sudo -i gedit /var/lib/dpkg/status

(you'll probably see a block that starts something like Package: Iceweasel Status: Install broken). See this similar issue: Unable to fix broken packages with sudo apt-get install -f


Use this: sudo dpkg -i --force-all /var/cache/apt/archives/firefox-esr_52.6.0esr-1~deb9u1_amd64.deb and you will force-install firefox-esr. 99.9% it'll fix your problem. Then follow it up with sudo dpkg --configure -a and you'll most probaly be in the clear.

As a general rule, when mixing stuff in installations you'll find a package that needs to overwrite a file from another package. Using dpkg with the --force-all option in these cases lets you play God, but it also overrules all those nice security checks built in in Debian's package management system. But after all, that's one of the burdens of being the root user.