How to Update PHP in Linux to any version

Antonio Silva - Jan 13 - - Dev Community

Over time, PHP releases a new version, making the previous ones obsolete. This means that they no longer receive updates or security patches, making them more vulnerable. It is therefore very important to use the minimum supported version of PHP and to keep all PHP packages up to date.

PHP is currently in its 8.3 version, bringing with it many new features and improvements in performance, syntax, security and stability.

Check Your Current Version of PHP

To find out your current PHP version, use the following command:

php -v
Enter fullscreen mode Exit fullscreen mode

The result will look something like this:

PHP 8.3.1 (cli) (built: Dec 21 2023 20:12:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.1, Copyright (c), by Zend Technologies
    with Xdebug v3.3.0, Copyright (c) 2002-2023, by Derick Rethans
Enter fullscreen mode Exit fullscreen mode

Add ondrej/php PPA

If you try to install PHP version 8.3 you will now get a package not found error.

Run the following command to add the repository with all the PHP versions present:

sudo add-apt-repository ppa:ondrej/php
Enter fullscreen mode Exit fullscreen mode

Install PHP 8

Use the following command to install php:

sudo apt install php8.3
Enter fullscreen mode Exit fullscreen mode

Change php8.3 part of the command above with the appropriate PHP version.

You can also install older versions of PHP.

Purge old PHP versions

If the new installation is working as expected, you can remove the old PHP packages from the system:

sudo apt purge '^php7.4.*'
Enter fullscreen mode Exit fullscreen mode

This assumes you are using PHP 7.4 as the previous version. Change php7.4 part of the command above with the appropriate PHP version.

Running PHP 8 with Other Versions

If you want to keep more than one version and switch between versions, use it on the system.

run the following command:

which php
Enter fullscreen mode Exit fullscreen mode

As a result, we have this directory:

/usr/bin/php
Enter fullscreen mode Exit fullscreen mode

Let's see what's in that directory:

ls -la /usr/bin/php
Enter fullscreen mode Exit fullscreen mode
rwxrwxrwx 1 root root 21 jan  1 21:50 /usr/bin/php -> /etc/alternatives/php
Enter fullscreen mode Exit fullscreen mode

We have a link to another directory, let's check that one too:

lrwxrwxrwx 1 root root 15 jan  1 21:50 /etc/alternatives/php -> /usr/bin/php8.3
Enter fullscreen mode Exit fullscreen mode

Again we have a link to another directory so let's see what's in this new directory:

ls /usr/bin/php*
Enter fullscreen mode Exit fullscreen mode
/usr/bin/php  /usr/bin/php8.3 /usr/bin/php7.4
Enter fullscreen mode Exit fullscreen mode

We have all versions of PHP installed. Let's change it so that the system uses PHP version 7.4. Use the following command:

sudo update-alternatives --config php
Enter fullscreen mode Exit fullscreen mode
There are 2 choices for the alternative php (providing /usr/bin/php).
Selection Path Priority Status

------------------------------------------------------------

* 0 /usr/bin/php8.3 83 auto mode
1 /usr/bin/php7.4 74 manual mode
2 /usr/bin/php8.3 83 manual mode

Press <enter> to keep the current choice[*], or type selection number:
Enter fullscreen mode Exit fullscreen mode

Select the number that corresponds to the version you want to use.

Run the command again:

php -v
Enter fullscreen mode Exit fullscreen mode
PHP 7.4 (cli) (built: Dec 21 2023 20:12:13) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.1, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.1, Copyright (c), by Zend Technologies
    with Xdebug v3.3.0, Copyright (c) 2002-2023, by Derick Rethans
Enter fullscreen mode Exit fullscreen mode

So the system is using PHP version 7.4.

Don't forget to add a reaction if the post has helped.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .