How do I find the most recent version of a package in the repositories in a shell script? If I use apt-cache-policy, I get the installed version as "Candidate", not the most recent one from the repositories.

apt-cache policy nvidia-current shows:

  Installed: 280.13-0ppa~natty1
  Candidate: 280.13-0ppa~natty1
  Version table:
 *** 280.13-0ppa~natty1 0
        100 /var/lib/dpkg/status
     270.41.06-0ubuntu1 0
        500 http://archive.ubuntu.com/ubuntu/ natty/restricted amd64 Packages

It looks like the installed version is marked with ***, so that one must be ignored. Perhaps there is an awk script that could be used?

  • Do you mean that you want to find the most recent version in the official repository, even if you have a ppa with a more recent version in your sources? – andrewsomething Aug 9 '11 at 16:48
  • @andrewsomething: I've removed the PPA without using ppa-purge. I forgot to mention that no new packages may be installed. (background: a PPA contained a nvidia-current package which was flawed and broke GL on the system. Such broken packages needs to be up/downgraded to a version which does not include that flaw. The PPA has already been removed as it contains conflicting packages. See also github.com/Bumblebee-Project/Bumblebee/blob/develop/cleanup#L89) – Lekensteyn Aug 9 '11 at 18:50

I suggest the following awk script, to which feed apt-cache policy output:

#!/usr/bin/awk -f

/^     [^ ]/ {
  version = $1
/^ \*\*\* [^ ]/ {
  version = $2
/^        [^ ]/ {
  server = $2
  if (server !~ /^\//) {
    print version
  • It works, many thanks. Is it guaranteed that the output of apt-cache policy is sorted by version? – Lekensteyn Aug 10 '11 at 7:56
  • I guess the recommended version is at the top of the output. – Lekensteyn Aug 10 '11 at 8:31
  • @Lekensteyn: I also assumed so, never seen otherwise – enzotib Aug 10 '11 at 8:41

The below command seems to work:

LANG=C apt-cache policy nvidia-current | grep '^     [^ ]' |\
    sort | awk '{print $1}' | head -1

LANG=C ensures that the output is consistent across different locales. grep matches a set of spaces followed by a non-space character (e.g. the version). awk displays the version which is the first non-whitespace block. Next, the output is sorted and the most recent version should be available on the top which is taken by head.

  • Note: I find this dirty and I'm not sure if the sorting suits all versions. Cleaner alternatives are appreciated. – Lekensteyn Aug 9 '11 at 16:09
  • 1
    I see two problems: you deliberately exclude the installed version, but this could be the most recent you are looking for. Second, default collate sort order is not necessarily the correct version string order. Lastly, you don't check if the version came from some repo or is a local package installation. – enzotib Aug 9 '11 at 20:04
  • Thank you for your feedback, what would be the correct sorting function? There is dpkg --compare-versions, but it cannot be used for sorting, only recursively checking which version is considered more recent. As for the local packages, this won't be an issue in my case as nvidia-current shouldn't be installed from a .deb file directly but it's certainly worth nothing. – Lekensteyn Aug 10 '11 at 7:51

You might want to look at rmadison

#! /bin/bash

DEFAULT_DIST="$(ubuntu-distro-info --stable)"
ARCH="$(dpkg --print-architecture)"

if [ -z "$1" ]; then
  echo "Usage: $0 <PACKAGE> <DIST>"

if [ -z "$TARGET_DIST" ]; then
  echo "Target dist not specified. Assuming $DEFAULT_DIST."

VERSION="$(rmadison $PACKAGE -a $ARCH | grep $TARGET_DIST | cut -d "|" -f 2)"


Or the one-liner:

rmadison nvidia-current -a amd64 | grep natty | cut -d "|" -f 2
  • Unfortunately, I cannot request users to install another package. By the way, rmadison does a HTTP request to determine this data. Is there a way to query the cache to allow work offline? – Lekensteyn Aug 9 '11 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.