alvas alvas - 1 year ago 59
Linux Question

How to get the boost_major_version number automatically?

Previously on Ubuntu, we can do the following to get the


echo "$boost_cv_lib_version" | sed 's/_//;s/_.*//'

For whatever reason, Ubuntu changes when upgrading from version 14.04 to 16.04 and now I have to do the following to find my Boost version:

dpkg -s libboost-dev | grep 'Version'

And the current version of boost installed using
sudo apt install libboost-all-dev


Given this, I would require the major version, i.e.

Other than manually looking at it knowing it's 158? I've tried some regexes but I couldn't get it correct to strip the string:

I've tried:

alvas@ubi:~/repp$ dpkg -s libboost-dev | grep 'Version'
alvas@ubi:~/repp$ dpkg -s libboost-dev | grep 'Version' | cut -d' ' -f2 | grep -oP '([0-9].*\.[0-9].*)\.'

But I also understand that the
option might not be available on linux platforms.

I could do this by piping into Python to do the dirty string works but that's a little too much... I'm sure there's a better way:

alvas@ubi:~/repp$ dpkg -s libboost-dev | grep 'Version' | python -c "import re,sys; print re.findall(r'([0-9].*\.[0-9].*)\.[0-9].*\.', sys.stdin.readline())[0].replace('.', '')"

So the question is:

  • How do I get the
    on Ubuntu 16.04?

  • If
    dpkg -s libboost-dev | grep 'Version'
    is the only way to do it, how do I strip

Answer Source

You can find the version info by querying the dpkg database with dpkg-query directly:

dpkg-query -W --showformat='${Version}\n' libboost-dev

--showformat indicates we just want the value of the Version parameter, so you will get just the version number in the output.

Now, to answer your second question, AFAIK dpkg does not differentiate between the major and minor (and micro) version numbers, you need to parse that yourself. Here is a sed way for this case:

dpkg-query -W --showformat='${Version}\n' libboost-dev | sed -E 's/^([^.]+)\.([^.]+)\..*/\1\2/'