Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Ubuntu version via PHP?

Tags:

php

If I try phpinfo() I see that System is:

Linux php56-web-68 4.4.0-142-generic #168-Ubuntu

But I have no idea what Ubuntu version is it? Is it 14.04 or 16.04 or something else?

Is there a way I can get this information just using standard PHP?

like image 822
jilocoiner Avatar asked Sep 17 '25 07:09

jilocoiner


1 Answers

Another alternative is to use cat /etc/lsb-release command and feed it into PHP's shell_exec.

shell_exec should yield similar to this:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS"

After that, just use parse_ini_string to parse the output, in turn returns an array.

Here's a one liner:

echo parse_ini_string(shell_exec('cat /etc/lsb-release'))['DISTRIB_RELEASE'];

Sidenote: Just tested on my ec2 instance, it yields 18.04

A better version than the previous answer above suggested by @jenesaisquoi:

echo shell_exec('lsb_release -sr'); // 18.04
like image 140
Kevin Avatar answered Sep 19 '25 19:09

Kevin