Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a script in PHP that needs to compare two package version numbers to determine which is newer.

PHP has a handy function called version_compare(), but it gets really confused with some of the more creative version numbers. For example, version_compare() thinks that 0.9.8g-16ubuntu3.1 is newer than 0.9.8o-1ubuntu4.4.

Anyone know how I can get this to work?

I'm happy to run any command line scripts using exec().

share|improve this question
up vote 5 down vote accepted

take a look to the dpkg --compare-versions option, it will allow you to ask dpkg to do the comparison :)

dpkg --compare-versions 0.9.8g-16ubuntu3.1 lt 0.9.8o-1ubuntu4.4

will return 0, that means 0.9.8g-16ubuntu3.1 is lt (lesser than, < ) to 0.9.8o-1ubuntu4.4

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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