DEV Community

Cover image for How to Update PHP in Linux to any version
Antonio Silva
Antonio Silva

Posted on

How to Update PHP in Linux to any version

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.

Top comments (8)

Collapse
 
dogers profile image
Dogers

In Debian/Ubuntu, anyway :)

Collapse
 
xtreme2020 profile image
José Francisco

Super important info before any upgrade, check your code compatibility, test in your staging site or local environment the version you are going to upgrade, to avoid down time, also if you are using NGINX with PHP-FPM, there is an extra step to do and that is change your site config file to PHP-FPM version you upgrade and restart the service...

Collapse
 
xxzeroxx profile image
Antonio Silva

Thank you for the information

Collapse
 
dev-alamin profile image
Al Amin

Helpful post. Liked it.

Collapse
 
xxzeroxx profile image
Antonio Silva

I'm glad you found it helpful.

Collapse
 
detzam profile image
webstuff

GOOD but how you make the change so that apache or nginx see you changed php?
phpinfo() sometimes will show you the old version

Collapse
 
xxzeroxx profile image
Antonio Silva

When you change the PHP version, Apache or Nginx may not immediately recognize the update. This usually happens if the web server is still using an older version of PHP, even though the new one is installed. Here's how to ensure the change is correctly applied:


Steps for Apache

Check the installed PHP versions:

php -v
Enter fullscreen mode Exit fullscreen mode

Disable the old PHP module:

sudo a2dismod phpX.Y
Enter fullscreen mode Exit fullscreen mode

Replace X.Y with the version you want to disable (e.g., php7.4).

Enable the new PHP module:

sudo a2enmod phpZ.W
Enter fullscreen mode Exit fullscreen mode

Replace Z.W with the version you want to enable (e.g., php8.2).

Restart Apache:

sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Verify using phpinfo(): Create or update your phpinfo.php file in the web server's document root:

phpinfo();
Enter fullscreen mode Exit fullscreen mode

Steps for Nginx

Modify the PHP-FPM socket:

Edit the Nginx configuration file (commonly /etc/nginx/sites-available/default) and update the fastcgi_pass directive to point to the new PHP-FPM version socket. For example:

fastcgi_pass unix:/run/php/php8.2-fpm.sock;
Enter fullscreen mode Exit fullscreen mode

Restart PHP-FPM service:

sudo systemctl restart phpX.Y-fpm
Enter fullscreen mode Exit fullscreen mode

Restart Nginx:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Verify using phpinfo(): Create or update your phpinfo.php file in the web server's document root:


Troubleshooting Tips

  • Clear browser cache: Sometimes, the browser cache may cause you to see old information. Clear your cache or test in an incognito window.
  • Check CLI vs. server versions: The PHP version in the CLI may differ from the one used by the web server. Use the following to confirm the web server version:
phpinfo();
Enter fullscreen mode Exit fullscreen mode
  • Reinstall PHP extensions: Ensure all necessary extensions are installed for the new PHP version.
Collapse
 
simshard profile image
Simon K

Thanks for the clear info