This post shows students and new users steps to find out the version of PHP running on their system or install a specific PHP version on Ubuntu Linux. For users who want to know what version of PHP is running on their server, the steps below will show them how.
For those who also want to know how to install a specific version on PHP on Ubuntu Linux, the steps below will also show them how to do that. The steps will also include options to find out what modules are installed with PHP on Ubuntu Linux as well.
For those who don’t know PHP is, here’s an overview:
PHP is one of the most used server-side programming languages. Chances are many of the websites you visit today mostly likely are running PHP. And if you’re thinking of running a website, you find yourself using PHP server-side scripting language as well.
To get started with find out what version of PHP you’re running, or installing a specific PHP version on Ubuntu Linux, continue below.
How to find out what version of PHP you are running in Ubuntu Linux
Installing PHP on Ubuntu Linux is easy. Simply run the apt-get install php commands to get it installed.
On Ubuntu Linux command console, run the commands below to check PHP version.
php -v # OR php --version
Once you run the command above, it should output similar lines as below:
PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
As you can see, the version number 7.4.3 is the latest version currently available in Ubuntu repository, however, it doesn’t mean that it is the latest version of PHP available to be installed.
You may also be able to find more info of the PHP version installed by running the commands below:
apt info php
When you run the commands above, it should list similar lines as below:
Package: php Version: 2:7.4+75 Priority: optional Section: php Source: php-defaults (75) Origin: Ubuntu Maintainer: Ubuntu Developers <email@example.com> Original-Maintainer: Debian PHP Maintainers <firstname.lastname@example.org> Bugs: https://bugs.launchpad.net/ubuntu/+filebug
The most reliable way of finding out what version of PHP is used for that specific website is to use the phpinfo() function, which prints various information about the PHP server, including its version.
To use this step to find PHP version, create a file called phpinfo.php in the web server root directory. Typically at the location below:
sudo nano /var/www/html/phpinfo.php
In the file, copy and paste the line below, then save the file and exit.
<?php phpinfo( ); ?>
Next, open your browser, go to localhost/phpinfo.php or your domain name (example.com/phpinfo.php) and the version of the PHP server will be displayed on your screen:
How to find out what PHP modules are currently installed in Ubuntu Linux
Now that you know how to find out what version of PHP installed on your system, the commands below show you how to list all the modules that are currently loaded with PHP.
dpkg --get-selections | grep -i php
Once you run the commands above, it should output similar lines as below.
libapache2-mod-php8.1 install php-common install php-fpm install php7.4-cli install php7.4-common install php7.4-fpm install php7.4-json install php7.4-opcache install php7.4-readline install php8.1 install php8.1-cli install php8.1-common install php8.1-opcache install php8.1-readline install ftp
Depending on your environment, the list above may be longer or shorted.
To list modules compiled with PHP, run the commands below:
How to install specific version of PHP on Ubuntu Linux
By default, Ubuntu repository contains the latest stable version of PHP. When you install PHP from Ubuntu repository, it will only install that specific version available at that time.
If you need to install a specific version not available in Ubuntu Linux, you may have to add a third party repository to your system.
Run the commands below to add a repository to Ubuntu Linux that will allow you to install specific versions.
First, install the necessary commands below:
sudo apt update sudo apt install software-properties-common
Next, run the commands below to add the repository.
sudo add-apt-repository ppa:ondrej/php sudo apt-get update
After that, you can search for and install specific version of PHP.
apt-cache showpkg php
Install a specific version by running the command format below:
sudo apt-get install php8.x=<complete version name>
Replace the x with the PHP minor version number and <complete version number> with the version number available for Ubuntu.
sudo apt install php8.1=8.1.0-1+ubuntu20.04.1+deb.sury.org+1
Now the new version should reflect as shown below:
PHP 8.1.0 (cli) (built: Nov 25 2021 20:22:03) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.0, Copyright (c) Zend Technologies with Zend OPcache v8.1.0, Copyright (c), by Zend Technologies
That should do it!
This post showed you how to check and install specific Ngnix version on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.