How do I revert all packages to their official versions?

Solution 1:

Well you can remove and reinstall the packages

ppa-purge is probably still your best bet for a clean escape. Just re-adding the PPA the package came from and then using ppa-purge to kill it. I'm not sure how many PPAs you have installed but if it's fewer than 10, I'd be looking at doing this.

If you think that method is too soft I've just written some bash-porn to help identify package versions whose installation source now only exists locally in /var/lib/dpkg/status. This is not the same as "orphaned" packages.

for p in `dpkg-query --showformat='${Package} ' -W`; do
    if [[ $(apt-cache policy $p | grep -Pzo "\*\*\* [^\n]+\s+100") ]]; then
        echo $p;
    fi;
done

I'm not sure if this is perfect yet but give it a go. Note it's only going to print out the names of the packages. You're going to have to manually uninstall/reinstall each package.

To do that, first look at what is available for that package by running apt-cache policy <package> and you'll see a list of package versions (including the /var/lib/dpkg/status version). Find the nearest external one and run:

sudo apt-get install <package>=<version>

You might need to add a --reinstall after the install but see how it goes.

Solution 2:

I've written a more complete script that will recognize packages whose current version is not from a PPA, and they have an alternative PPA-available version. After it runs, it prints a ready-to-run command that will downgrade such packages to their PPA versions.

https://gist.github.com/peci1/2d7859857fdad73ee8443f5ecd5ee5a3

#!/usr/bin/env bash

# BSD 3-clause license, copyright Martin Pecka @ 2019

# This script outputs a command that will revert all packages from non-PPA versions to their latest PPA version.
# This may be handy i.e. for finding packages for which you installed a newer version from a .deb file, or after
# incompletely removing a PPA.

export LC_ALL=C

command=""

for p in `dpkg-query --showformat='${Package} ' -W`; do
    if [[ $(apt-cache policy $p | grep -Pzo "\*\*\* [^\n]+\s+100") ]]; then
        versions=$(apt-cache policy $p | tr "\n" "\r" | grep -Po '(?<=\r )[ *]{3} [^\r]+ [0-9]+\r\s+[0-9]+' | sed 's/ [0-9]\+\r\s\+\([0-9]\+\)/ \1/g' | tr "\r" "\n")
        installable_versions=$(echo "${versions}" | grep -v " 100$")
        version_to_install=$(echo "${installable_versions}" | head -n1 | grep -Po "\s+\K.*(?= [0-9]+$)")
        if [[ ! -z "${version_to_install}" ]]; then
            echo "${p}=${version_to_install}"
            command="${command} ${p}=${version_to_install}"
        else
            echo "${p}: no PPA version"
        fi
    fi;
done

echo "To revert packages to their latest PPA version, call the following command as root. Please, carefully go through the list of changes apt-get will present to you!"
echo "apt-get install ${command}"