 Learn about Linux in practice  1. Introduction
In Linux, package managers handle software installation, updates, and dependency management. Each distribution typically includes a default package manager:
- Debian and its derivatives often use apt
- RHEL and similar distributions commonly use dnf or yum
- Arch uses pacman
- SUSE-based systems use zypper
In this tutorial, we’ll discuss how to identify the package manager used by a given Linux distribution. In scripting, automation, or multi-distro environments, selecting the correct package manager can improve efficiency and reduce the chance of errors.
2. Identifying Package Manager by Distribution Name
One method to identify the package manager of a system involves checking the distribution name. Since each major Linux distribution comes with a default package manager, identifying the distribution often indicates which package manager is in use.
One way to check the distribution is the /etc/os-release script:
$ source /etc/os-release && echo $NAME
Ubuntu
Alternatively, the lsb_release command should show the name of the distribution:
$ lsb_release -i | cut -f2
Ubuntu
In this case, we extract the second whitespace-separated field.
With this information, it could be easier to determine the default package manager via a list similar to the one we saw earlier.
In this case, the output shows that the distro is Ubuntu. As already outlined, apt is the default package manager for Ubuntu. To verify that apt is installed, we can use the which command:
$ which apt
/usr/bin/apt
If the output shows a file path like /usr/bin/apt or similar, apt is installed in the current system and distribution.
If not, it likely isn’t installed. Of course, a system can use different package managers.
3. Identifying Package Managers With a Bash Script
For a quick and automated way to identify the package manager, we can use a Bash script that checks for common package managers.
3.1. Basic Script for a Single Package Manager
Let’s create the script:
$ cat pkgmgrid.sh
if command -v apt > /dev/null 2>&1; then
echo "apt package manager"
elif command -v dnf > /dev/null 2>&1; then
echo "dnf package manager"
elif command -v yum > /dev/null 2>&1; then
echo "yum package manager"
elif command -v zypper > /dev/null 2>&1; then
echo "zypper package manager"
elif command -v pacman > /dev/null 2>&1; then
echo "pacman package manager"
else
echo "No known package manager found"
fi
Now, we make the script executable and run it:
$ chmod +x pkgmgrid.sh
$ ./pkgmgrid.sh
apt package manager
Yet, this script detects only one package manager, even if multiple are installed. It uses the chain of if–elif statements, which means that once it meets a condition, it skips the other options. For example, if Ubuntu has both apt and snap installed, the script identifies only the first one it matches and ignores the others.
3.2. Advanced Script for Multiple Package Managers
However, some distributions support more than one package manager. For instance, Debian uses both the high-level apt and low-level dpkg. Similarly, Fedora uses dnf and rpm. Additionally, some systems support universal package managers such as snap and flatpak for cross-distribution compatibility.
For systems with more than one package manager, we can use a more flexible script. This script uses the for loop to iterate through a list of some common package managers, checking each one without stopping at the first match. This way, it detects more than one installed package manager. It also detects the Linux distribution and identifies the default package manager based on the detected distro.
Let’s create the script:
$ cat package-manager-id.sh
#!/bin/bash
# Detect installed package managers
package_managers=("apt" "yum" "dnf" "zypper" "pacman" "snap" "flatpak")
echo "Installed package managers on this Linux system:"
for manager in "${package_managers[@]}"; do
if command -v "$manager" > /dev/null 2>&1; then
echo "-$manager"
fi
done
# Detect distro
if [[ -f /etc/os-release ]]; then
distro=$(source /etc/os-release && echo $NAME)
else
distro="*Unknown*"
fi
echo "Detected distro: $distro"
# Default package manager, if any
echo -n "Default distro package manager: "
if [[ "$distro" == "Ubuntu" || "$distro" == "Debian" || "$distro" == "Linux Mint" ]]; then
echo "apt"
elif [[ "$distro" == "Fedora" || "$distro" == "Red Hat"* ]]; then
echo "dnf"
elif [[ "$distro" == "CentOS" ]]; then
echo "yum"
elif [[ "$distro" == "Arch Linux" || "$distro" == "Manjaro" ]]; then
echo "pacman"
elif [[ "$distro" == "openSUSE" ]]; then
echo "zypper"
fi
Now, we make the script executable and run it:
$ chmod +x package-manager-id.sh
$ ./package-manager-id.sh
Installed package managers on this Linux system:
-apt
-snap
Detected distro: Ubuntu
Default package manager: apt
The output confirms that the system has both the apt and snap package managers installed, with apt expected to serve as the default package manager.
4. Conclusion
In this article, we’ve covered how to identify the package manager of a Linux distribution. We explored methods using system files, commands, and Bash scripts to detect both installed and default package managers. Knowing which package manager the system uses helps make installing, updating, and running scripts easier and more reliable across different Linux systems. The post How to Identify the Package Manager of a Linux System first appeared on Baeldung on Linux.
Content mobilized by FeedBlitz RSS Services, the premium FeedBurner alternative. |