How to list all the packages which are installed from PPAs?

The following command returns the package name and its ppa (if installed from a ppa):

apt-cache policy $(dpkg --get-selections | grep -v deinstall$ | awk '{ print $1 }') | perl -e '@a = <>; $a=join("", @a); $a =~ s/\n(\S)/\n\n$1/g;  @packages = split("\n\n", $a); foreach $p (@packages) {print "$1: $2\n" if $p =~ /^(.*?):.*?500 http:\/\/ppa\.launchpad\.net\/(.*?)\s/s}'

Details:

  • dpkg --get-selections gives only the installed packages after grep -v deinstall$
  • awk '{ print $1 }' returns only the package name
  • perl -e '@a = <>; $a=join("", @a)' concatenates all the lines returned by apt-cache policy
  • $a =~ s/\n(\S)/\n\n$1/g; adds a newline between each package section
  • @packages = split("\n\n", $a); is a perl array containing all the packages infos, one package per item.
  • foreach $p (@packages) {print "$1: $2\n" if $p =~ /^(.*?):.*?500 http:\/\/ppa\.launchpad\.net\/(.*?)\s/s} is a loop where the package and the ppa are printed if a ppa with prio 500 is found in the policy.

  • aptitude command below shows list of installed packages for active PPA's in sources.list.

    aptitude search '?narrow(?installed, ~Oppa)'
    

    ~Oppa means Origin contains 'ppa'

    Reference: aptitude - Search term reference

  • In case PPA repository was removed, packages become obsolete. Alternatively use this filter instead ~Oppa | -o reference:

    How to get a list of all packages installed from PPAs even for PPA repositories that may have been removed


The source of an installed package can be checked using apt-cache, for example

$ apt-cache policy oracle-java7-installer

oracle-java7-installer:
  Installed: 7u51-0~webupd8~7
  Candidate: 7u51-0~webupd8~7
  Version table:
 *** 7u51-0~webupd8~7 0
        500 http://ppa.launchpad.net/webupd8team/java/ubuntu/ precise/main i386 Packages
        100 /var/lib/dpkg/status

The output of apt-cache policy <package_name> contains the source.

One can use the following script to obtain the list of packages installed from PPAs.

#!/bin/bash
echo "List of packages which are not installed from Ubuntu repository"
for i in `dpkg -l | grep "^ii" | awk '{print $2}'`
do
    j=`apt-cache policy "$i" | grep "ppa.launchpad.net"` 
    if [ $? -eq 0 ]; then
        echo "$i"
        #echo "$i $j"
    fi
done