Install PHP 7.4 FPM on Ubuntu 18.04

It is always awesome when new versions of PHP come out so we can test the performance improvements! According to these benchmarks PHP 7.4 took around 500 ms of execution time for the same task that PHP 7.2 took over 650 ms to complete!

Remember to test the new PHP 7.4 in a staging environment first ideally on a separate server. If you need help testing this feel free to get in touch.

This tutorial will show you how to install PHP 7.4 on Ubuntu 18.04 and update the CLI path to use the new PHP version.

Install PHP 7.4 php-fpm on Ubuntu 18.04

Start with adding the repository kindly provided by sury.org

sudo apt-get install apt-transport-https -y
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg
echo "deb https://packages.sury.org/php/ stretch main" | sudo tee /etc/apt/sources.list.d/php.list

Update your package list

sudo apt-get update

Install the PHP 7.4 packages for nginx and php-fpm functionality

sudo apt-get install php7.4-cli php7.4-curl php7.4-mysql php7.4-fpm php7.4-gd php7.4-xml php7.4-mbstring php7.4-zip php7.4-soap php7.4-dev -y

Now we can update the PHP command line binary shorthand php to use the PHP 7.4 CLI binary.

Update CLI to PHP 7.4

Test your current PHP CLI version

php --version

Output here is showing PHP 7.2!

PHP 7.2.24-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Oct 24 2019 18:28:51) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.24-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Update the CLI shortcut to use the 7.4 binary with this command

sudo update-alternatives --set php /usr/bin/php7.4

Now make sure it worked

php --version

Output is now showing PHP 7.4 🙂

PHP 7.4.1 (cli) (built: Dec 18 2019 14:44:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.1, Copyright (c), by Zend Technologies

Remember to configure PHP-FPM for low memory consumption with this guide.