Find all ubuntu packages not from standard repositories

When upgrading from one ubuntu version to the next one, I like to use the opportunity to do some spring cleaning.

To find from which repositories a package was installed we can use:

dpkg -l | grep "ii" | awk '{print $2}' | xargs -n 1 -IX sh -c "apt policy X 2>/dev/null | tr '\n' ' '" | tee all_packages.txt

Then we can inspect the all_packages.txt file and filter for packages which are not from the ubuntu repositories.

cat all_packages.txt | grep -v "ubuntu.com"

We can now inspect this list and decide which ones to remove and which ones to keep.