15

Is there a way (from a script) to identify the default system package manager?

To clarify, what I want to do is run a given command and, on Debian or any of its derivatives it'll return something like "apt", on openSUSE it'll return "zypp", on Fedora et al it'll return "yum", on Arch Linux it'll return "pacman" etc.

I know I can do this with something like the following, I just wondered if there was a more robust method that won't break as soon as there is an executable with the same name.

which apt >/dev/null 2>&1
if [ $? -eq 0 ]
then
    echo "apt"
fi
# etc...
9

Start with the accepted answer to this question: How can I get distribution name and version number in a simple shell script?. Then, decide which package manager you want to use based on the detected distribution.

  • Is there a readily available list of package managers for different distributions? There are a lot of debian clones in particular. – DrAl Aug 23 '12 at 6:58
  • Not sure about a readily available list, but all Debian clones will use apt-get. – Jim Paris Aug 23 '12 at 16:52
  • Yes, I realise that; my problem isn't so much identifying the distribution (although your link will certainly make this easier), it's figuring out how to link a distribution name like (to pick one at random) "SolusOS" with apt without maintaining a list of every distribution that exists. – DrAl Aug 24 '12 at 6:49
  • Do it the autotools way: Check if it works. BTW, that a distribution uses apt means that it is a Debian relative (and they are a rather compact bunch), finding yum doesn't mean that the packaging conventions (split into library/development, one big brick of Perl or individual packages off CPAN, ...) are the same. Don't even try to install a Fedora package on CentOS (there are at least 3 or 4 years of version skew, just won't work). – vonbrand Jan 18 '13 at 3:28
  • Detecting the distro will only work for the popular ones. A whitelist can't be updated for all the less know distributions. Directly testing the presence of the tools is the only way to make it work. If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck. – liberforce Oct 18 '18 at 9:53
8

Instead of identify binary programs, you should start from identify distributions,

Just give you few lines that works in bash scripting:

declare -A osInfo;
osInfo[/etc/redhat-release]=yum
osInfo[/etc/arch-release]=pacman
osInfo[/etc/gentoo-release]=emerge
osInfo[/etc/SuSE-release]=zypp
osInfo[/etc/debian_version]=apt-get

for f in ${!osInfo[@]}
do
    if [[ -f $f ]];then
        echo Package manager: ${osInfo[$f]}
    fi
done

Althrough these parts can't be trusted, but generally people won't do that.

2

I opted to go this route after reviewing the others. This came up for me when running many docker containers and needing curl / jq and not being able to rely on what was available from job to job.

script:
 - packagesNeeded='curl jq'
 - if [ -x "$(command -v apk)" ];       then sudo apk add --no-cache $packagesNeeded
 - elif [ -x "$(command -v apt-get)" ]; then sudo apt-get install $packagesNeeded
 - elif [ -x "$(command -v dnf)" ];     then sudo dnf install $packagesNeeded
 - elif [ -x "$(command -v zypper)" ];  then sudo zypper install $packagesNeeded
 - else echo "FAILED TO INSTALL PACKAGE: Package manager not found. You must manually install: $packagesNeeded">&2; fi

In pure bash:

packagesNeeded='curl jq'
if [ -x "$(command -v apk)" ];       then sudo apk add --no-cache $packagesNeeded
elif [ -x "$(command -v apt-get)" ]; then sudo apt-get install $packagesNeeded
elif [ -x "$(command -v dnf)" ];     then sudo dnf install $packagesNeeded
elif [ -x "$(command -v zypper)" ];  then sudo zypper install $packagesNeeded
else echo "FAILED TO INSTALL PACKAGE: Package manager not found. You must manually install: $packagesNeeded">&2; fi
  • I'll note in GitLab CI I had some issue stacking the 'elif' for some reason which is why I just if statements. – Mark May 5 '20 at 21:23

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.