skip to Main Content

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?

4

Answers


  1. If you want to check ubuntu version via terminal than you can check it with below command

    lsb_release -a
    
    Login or Signup to reply.
  2. You can call shell commands with shell_exec();

    $version = shell_exec('lsb_release -a');
    echo $version;
    

    Distributor ID: Ubuntu

    Description: Ubuntu 18.04.3 LTS

    Release: 18.04

    Codename: bionic

    From that you can parse the info you need.

    Login or Signup to reply.
  3. It’s aviable with this command

    $ubuntu_version = explode("t",shell_exec('lsb_release -a | grep Release'))[1];
    
    Login or Signup to reply.
  4. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search