PHP is a popular scripting language used in the backend during web development to take the client request, process the request and respond to it, or execute MySQL queries.
Many popular CMS (content management systems), such as WordPress, Joomla, Drupal, Laravel, Magento, Symfony, and many more, are based on PHP.
As the popularity of PHP grows, each PHP version undergoes multiple changes, such as performance improvements, functionality updates, and security enhancements.
With this release, knowing the PHP version installed on your system is crucial, especially if you plan to develop a new web app based on PHP; ensure you check the PHP version on your system and strategize your development accordingly.
In this article, I’ll show you how to check the PHP (or PHP-FPM) version on Ubuntu and other Linux distros using the command line.
How to Check PHP Version in Linux (via 4 Methods)
There are multiple ways to find out the PHP version installed on your system: I’ll show you four methods— the first three work for any Linux distribution such as Debian, Ubuntu, Red Hat, Fedora, Arch, Manjaro, etc., while the fourth method only works for Ubuntu-based distributions such as Debian, Pop!_OS, Linux Mint, etc.
Method 1: Check PHP Version via PHP Command
The simplest way to find out the PHP version is by using the “-h” or “–help” flag with the “php” command in your command line.
$ php -v
Output:
This method will work for the majority of Linux distributions with PHP installed through a package manager.
Method 2: Check PHP Version via PHP Script
When PHP is installed using a different method, such as source code, the previous method might not work as it relies on passing the help flag to the PHP binary file that resides in the bin folder.
If you’re using the absolute path of the PHP binary file to run your PHP script, you can create a new PHP file named “phpversion.php” and include the following content:
<?php
phpinfo();
?>
Then, you have two options: either run the PHP server and access the PHP file in your web browser to check the PHP version, or pass the PHP file to the absolute path of the PHP binary file and use the grep command to find the PHP version.
$ /path/to/php phpversion.php | grep "PHP Version"
Output:
This method would work for all Linux distributions, as well as Windows and macOS.
Method 3: Check PHP Version via System Path
If you’ve relocated the PHP directory and can’t locate it, neither of the previous methods will work. In such cases, you can use a few command-line tools to find the PHP directory.
For example, in the given command, “locate” will search for a file or directory named PHP, “grep” will filter the results for files ending with the name PHP, and the final command is “xargs” and “ls“, which are used to follow symbolic links, taking stdin data from the previous piped command.
$ locate php | grep -P "/php$" | xargs ls -L
When you run the above command, it will return all PHP files, sometimes including the PHP version in the output, as shown.
If the PHP version isn’t displayed, you can look for a PHP file in a bin directory or use tools like the file command to identify a binary file. Then, use the absolute path to the PHP binary file along with the help option to determine the PHP version.
$ /usr/bin/php -v
Output:
This method is quite effective but might be a bit trickier for beginners, so if you’re using a Debian or Ubuntu-based distribution, you can follow the next method to verify the PHP version.
Method 4: Check PHP Version via APT Command
When you install a program on a Debian or Ubuntu-based distribution, it adds an entry in the APT list that you can use to check the PHP version installed on your system.
$ apt list --installed | grep php
Output:
This method would only work for Debian or Ubuntu-based distributions; if you’re uncertain about the Linux distribution you’re running, use the command “cat /etc/os-release” to identify it.
Final Word
In this article, you’ve learned various methods to check the PHP version running on your Linux system. If you have any questions, feel free to ask them in the comment section.
Till then, peace!
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.