It turns out that there is no standard “apt” command which lists where a package was installed from. You may need this information if you have added additional APT repositories to your Debian/Ubuntu installation. I see a lot of questions at the forums (1, 2, 3, 4) and the proper solution tends to be “parse apt-cache output yourself”. Here is my solution which is very similar to this one:
#!/bin/bash
set -u
errors=0
for PKGNAME in $(dpkg -l|grep ^i|awk '{print $2}'); do
INFO="$(apt-cache policy "$PKGNAME")"
IVER="$(echo "$INFO" | grep Installed: | awk '{print $2}')"
IPRIO="$(echo "$INFO" | fgrep "*** $IVER" | awk '{print $3}')"
REPO="$(echo "$INFO" | fgrep -A1 "*** $IVER" | tail -n+2 | head -n1 | awk '{print $2 " " $3}')"
echo "$PKGNAME repo=$REPO"
if [ "$REPO" == '' ]; then
errors=$(( $errors + 1 ))
echo "ERROR: Unable to find the repo for package \"$PKGNAME\"" >&2
fi
done
if [ "$errors" -ne 0 ]; then
echo "ERROR: $errors errors encountered" >&2
exit 1
else
exit 0
fi