Upgrade/Installation guide for PHP 7.4 on Ubuntu and Debian

Published On28 Nov 2019

PHP 7.4 was released today just in time for the thanks giving!

I have written posts about upgrading PHP to 7.3 last year, and for other versions in the previous years. It's time to upgrade to PHP 7.4!

SPOOKY WARNING

PHP 7.4 deprecates a bunch of features that your existing applications might be using. Although unlikely, there is a non-zero chance of the upgrade breaking your existing sites. Please make sure to make a proper backup of your server before you proceed.

1. Add ondrej/php PPA

Thanks to Ondrej, we have the PHP 7.4 builds ready at his PPA. These packages are not official builds from Ubuntu, but they are used by practically every modern Ubuntu stack, and I can't recommend them highly enough. If your wallet allows it, I suggest that you pay forward for his efforts with a donation.

Ubuntu
sudo add-apt-repository ppa:ondrej/php # Press enter when prompted.
sudo apt-get update
Debian
sudo apt install apt-transport-https lsb-release ca-certificates curl -y
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg 
sudo sh -c 'echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update

2. Get a list of current PHP packages

If you are upgrading from a previous PHP version, we will not take a note of the current PHP packages you have in your server. This list will help us to figure out the packages we should install for PHP 7.4. Note that PHP no longer bundles some less used extensions such as wddx, which you will need to compile yourself if you need it. Take a look at what's new and changing in PHP 7.4 for information on what's being removed from core.

dpkg -l | grep php | tee packages.txt

This will list all packages, filter them for those who have php in their name, and write them to packages.txt file at your current directory in addition to show them on screen.

This list will come handy to refer after we install PHP 7.4 to install the PHP 7.4 counterparts of the packages.

A screenshot of the existing package in an Ubuntu server

3. Install PHP 7.4

*Breathes heavily...

PHP 7.4 core

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

This is to install the basic PHP 7.4 binaries. You will have the bare minimum PHP installation after this. Note that we did not install fpm or any other server integration package just yet

PHP 7.4 extensions

It's now time for the extensions. If you are upgrading from a previous installation, you can now take a look at the contents of your packages.txt file (cat packages.txt).

In Ondrej's PPA/Debian repo, PHP packages follow the php7.4-PACKAGE pattern. For example, mbstring is at php7.4-mbstring, and curl is at php7.4-mbstring.

To install:

sudo apt install php7.4-curl php7.4-mbstring php7.4-bz2 php7.4-readline php7.4-intl

Extend the list of packages above to match to the contents of your packages.txt file, or to all packages that are desired.

Here is my recommended list of extensions:

sudo apt install php7.4-bcmath php7.4-bz2 php7.4-curl php7.4-intl php7.4-mbstring php7.4-mysql php7.4-readline php7.4-xml php7.4-zip`

PHP already includes extensions such as curl, hash, ctype in core.

Web server integration

If you are planning to use PHP in a web server, you need to integrate the web server with PHP. There are two packages that you can use:

For Nginx web server, or Apache server using mod_event MPM, you need php7.4-fpm package. If you are using PHP as an embedded Apache module (often a bad call), you will need libapache2-mod-php7.4 package. Apache users can run apachectl -V to get more information about the server integration. If it says prefork, you need libapache2-mod-php7.4 package. php7.4-fpm otherwise.

Nginx or Apache with event MPM
sudo apt install php7.4-fpm
sudo a2enconf php7.4-fpm # For Apache only
Apache with prefork MPM
sudo apt install libapache2-mod-php7.4

3. Test PHP 7.4 installation.

Run php -v to make sure PHP 7.4 (CLI) is properly installed. You can run php -m to get a list of extensions enabled in your PHP 7.4 setup.

PHP 7.4 'php -v' command.

4. Remove old PHP versions

After a successful PHP 7.4 installation, you can remove the old version if desired. Unless there is a strong reason to keep the old version running and hogging your system resources, I recommend that you remove the old versions.

apt purge php7.3 php7.3-common # Change 7.3 with all versions you want to purge.

... aaand that's it! You should now have a running PHP 7.4 with common packages/replacement packages of old version. Keep an eye on the error logs for potential backwards incompatible bugs in your code. I have compiled a list of things changed, deprecated, added, and removed in PHP 7.4.

Recent Articles on PHP.Watch

All ArticlesFeed 
How to fix `mysql_native_password` not loaded errors on MySQL 8.4

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to fix the SQLSTATE[HY000] [1524] Plugin 'mysql_native_password' is not loaded errors caused in MySQL 8.4 no longer enabling the mysql_native_password plugin by default.
How to fix PHP Curl HTTPS Certificate Authority issues on Windows

How to fix PHP Curl HTTPS Certificate Authority issues on Windows

On Windows, HTTPS requests made with the Curl extension can fail because Curl has no root certificate list to validate the server certificates. This article discusses the secure and effective solutions, and highlights bad advice that can leave PHP applications insecure.
AEGIS Encryption with PHP Sodium Extension

AEGIS Encryption with PHP Sodium Extension

The Sodium extension in PHP 8.4 now supports AEGIS-128L and AEGIS256 Authenticated Encryption ciphers. They are significantly faster than AES-GCM and CHACHA20-POLY1305. This article benchmarks them and explains how to securely encrypt and decrypt data using AEGIS-128L and AEGIS256 on PHP.
Subscribe to PHP.Watch newsletter for monthly updates

You will receive an email on last Wednesday of every month and on major PHP releases with new articles related to PHP, upcoming changes, new features and what's changing in the language. No marketing emails, no selling of your contacts, no click-tracking, and one-click instant unsubscribe from any email you receive.

Support PHP.Watch — If you find the articles, version information, Codex, and other PHP.Watch contributions useful, consider supporting through GitHub Sponsors. Your sponsorship helps dedicate more time to creating valuable content and improving the PHP community. Together, we can keep the momentum going — thank you for your support!

Thanks to the highest tier sponsor: @TomasVotruba for your generous support to keep PHP.Watch moving 💜