How to detect a Linux distribution in Perl

Last updated on July 8, 2020 by Dan Nanni

Question: I need to write a Perl program which contains Linux distro-dependent code. For that, the Perl program needs to be able to automatically detect what Linux distribution (e.g., Ubuntu, CentOS, Debian, Fedora, etc) it is running on, and what version number it is. How can I identify Linux distribution in Perl?

If you want to detect Linux distribution within a Perl script, you can use a Perl module named Linux::Distribution. This module guesses the underlying Linux operating system by examining /etc/lsb-release, and other distro-specific files under /etc directory. It supports detecting all major Linux distributions, including Fedora, CentOS, Arch Linux, Debian, Ubuntu, SuSe, Red Hat, Gentoo, Slackware, Knoppix, and Mandrake.

To use this module in a Perl program, you need to install it first.

Install Linux::Distribution Perl module on Debian or Ubuntu

Installation on Debian-based system is straightforward with apt-get:

$ sudo apt-get install liblinux-distribution-packages-perl

Install Linux::Distribution on Fedora, CentOS or RHEL

If Linux::Distribution module is not available as a package in your Linux (such as on Red Hat based systems), you can use CPAN to build/install it.

First, make sure that you have CPAN installed on your Linux system:

$ sudo yum -y install perl-CPAN

Then use this command to build and install the module:

$ sudo perl -MCPAN -e 'install Linux::Distribution'

Identify a Linux Distribution in Perl

Once Linux::Distribution module is installed, you can use the following code snippet to identify on which Linux distribution you are running.

use Linux::Distribution qw(distribution_name distribution_version);

my $linux = Linux::Distribution->new;

if ($linux) {
  my $distro = $linux->distribution_name();
  my $version = $linux->distribution_version();
  print "Distro: $distro $versionn";
}
else {
  print "Distro: unknownn";
}

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean