Check PHP Version and Install on Ubuntu 24.04

This article details the steps to check and install a specific PHP version on Ubuntu 24.04, highlighting its significance for web development compatibility. It covers checking installed PHP versions, using the phpinfo() function, identifying PHP modules, and installing specific PHP versions via third-party repositories, ensuring optimal server performance and security.

This article explains how to check and install a specific PHP version on Ubuntu 24.04.

PHP is a server-side scripting language primarily designed for web development. It supports many of the popular content management systems today.

Some applications or frameworks, such as WordPress or Magento, may require a specific version of PHP to function properly. For instance, older applications built with PHP 5 might face issues when run on PHP 7 or later versions due to changes in syntax and features.

If you wish to install a specific version of PHP on Ubuntu, the following steps guide you through the process.

Find out what version of PHP is installed

Installing PHP on Ubuntu Linux is easy. Run the apt-get install php commands to get it installed.

Run the commands below to check the PHP version on the Ubuntu Linux command console.

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

Version 7.4.3 is available in the Ubuntu repository, but this does not mean the latest PHP version can be installed.

You can also find more information on the installed PHP version 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 <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian PHP Maintainers <team+pkg-php@tracker.debian.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 the PHP version, create a file called phpinfo.php in the web server’s root directory. Typically at the location below:

sudo nano /var/www/html/phpinfo.php

Copy and paste the line below in the file, 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:

Find out what PHP modules are installed

Now that you know how to determine the version of PHP installed on your system, the following commands will show you how to list all the modules currently loaded with PHP.

dpkg --get-selections | grep -i php

After executing the commands above, you should see output that resembles the lines shown 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

The length of the list above may vary based on your environment.

To list modules compiled with PHP, run the commands below:

php -m

Install a specific version of PHP

By default, the Ubuntu repository provides the latest stable version of PHP. Therefore, when you install PHP from the Ubuntu repository, it will install that specific available version.

You might need to add a third-party repository to your system if you want to install a specific version that is not available in Ubuntu Linux.

Use the commands below to add a repository in Ubuntu Linux, which will allow you to install specific versions of software.

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 a 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.

Example:

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!

Conclusion:

  • Understanding the PHP version running on your system is crucial for compatibility and security.
  • PHP installation on Ubuntu is straightforward and can be tailored to specific versions using third-party repositories.
  • The phpinfo() function is a helpful tool for verifying PHP configuration and installed modules.
  • Regularly checking for updates and managing PHP versions ensures optimal performance for your web applications.
  • Knowing how to list installed PHP modules enhances your ability to troubleshoot and manage your server environment effectively.
Richard Avatar

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *


Exit mobile version