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
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
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
Install PHP 8
Use the following command to install php:
sudo apt install php8.3
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.*'
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
As a result, we have this directory:
/usr/bin/php
Let's see what's in that directory:
ls -la /usr/bin/php
rwxrwxrwx 1 root root 21 jan 1 21:50 /usr/bin/php -> /etc/alternatives/php
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
Again we have a link to another directory so let's see what's in this new directory:
ls /usr/bin/php*
/usr/bin/php /usr/bin/php8.3 /usr/bin/php7.4
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
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:
Select the number that corresponds to the version you want to use.
Run the command again:
php -v
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
So the system is using PHP version 7.4
.
Don't forget to add a reaction if the post has helped.