Upgrading to PHP 7.4

The awaited latest version of the PHP 7.4 was released on 28th Nov, 2019. It comes with a lot of new features such as

  • Typed Properties
  • Arrow Functions
  • Limited Return Type Covariance and Argument Type Contravariance
  • Unpacking Inside Arrays
  • Numeric Literal Separator
  • Weak References
  • Allow Exceptions from __toString()
  • Opcache Preloading
  • Several Deprecations
  • Extensions Removed from the Core

PHP 7.1 approaches EOL & 7.2 will become security only on 1st December.

PHP EOL

Let's see how to upgrade your PHP installation to 7.4.

1. Add the ondrej/php PPA

Ubuntu:

sudo add-apt-repository ppa:ondrej/php # Press enter to confirm.
sudo apt-get update

Debian:

sudo apt install apt-transport-https lsb-release
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg # Download the signing key
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list' # Add Ondrej's repo to sources list.
sudo apt update

2. Install PHP 7.4 and required extensions

To install PHP 7.4 core

sudo apt install php7.4 php7.4-common php7.4-cli

Install required extensions

Simply prefix php7.4- with an extension that you need to install. For example to get the php-curl extension use php7.4-curl

sudo apt install php7.4-curl php7.4-json php7.4-gd php7.4-mbstring php7.4-intl php7.4-bcmath php7.4-bz2 php7.4-readline php7.4-zip

3. PHP 7.4 for web:

To integrate PHP with your web server, If you are using Nginx, or Apache with mod_event, you will need to install php7.4-fpm package. If you are using PHP as an embedded Apache module, you will need the package libapache2-mod-php7.4. For Apache, you can use apachectl -V to see your current MPM, whether it’s prefork or event.

Nginx or Apache with event MPM:

sudo apt install php7.4-fpm

Apache with prefork MPM:

sudo apt install libapache2-mod-php7.4
sudo a2enmod php7.4

4. Uninstall old PHP versions:

sudo apt purge php7.3 libapache2-mod-php7.3 # For removing PHP 7.3
sudo apt purge php7.2 libapache2-mod-php7.2 # For removing PHP 7.2
sudo apt purge php7.1 libapache2-mod-php7.1 # For removing PHP 7.1
sudo apt purge php7.0 libapache2-mod-php7.0 # For removing PHP 7.0

5. Verify PHP 7.4 installation:

To verify the installation of PHP 7.4

###From CLI:

php -v

php -v output

From web server:

Create a file info.php. It must be saved to a very specific directory, which is called the “web root”. In Ubuntu and Debian this is located at /var/www/html/.

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

Type the following in the editor.

<?php
    phpinfo();
?>

When you are finished, save and close the file.

Now visit this script in your browser. http://your_server_IP_address/info.php You should see something similar to this.

phpinfo() output

So now you have PHP 7.4 up and running. See the migration guide for PHP 7.4 at https://www.php.net/migration74

Back to all posts