How to Update to PHP 7.4 on Ubuntu?

PHP 7.4 comes with several performance improvements and new features. If you’re running an older version of PHP on your Ubuntu system, upgrading to PHP 7.4 can provide a significant boost to your web applications. In this guide, we’ll walk you through the process of updating to PHP 7.4 on Ubuntu.

Note: These instructions are for Ubuntu 18.04 and 20.04. Some steps may differ for other Ubuntu versions.

root@geek:~# php -v
PHP 7.2.24-0ubuntu0.18.04.17 (cli) (built: Feb 23 2023 13:29:25) ( 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-0ubuntu0.18.04.17, Copyright (c) 1999-2018, by Zend Technologies

Step 1: Add the PHP Repository

To ensure that you’re installing PHP 7.4 from a trusted source, add the ondrej/php repository to your system. This repository is maintained by Ondřej Surý, a well-known Debian and Ubuntu PHP package maintainer.

First, update the package list and install the necessary packages:

sudo apt update
sudo apt install -y software-properties-common

Next, add the ondrej/php repository:

sudo add-apt-repository ppa:ondrej/php

Update the package list again to include the packages from the newly added repository:

sudo apt update

Step 2: Install PHP 7.4

Install PHP 7.4 and its associated modules by running the following command:

sudo apt install -y php7.4
root@geek:~# sudo apt install -y php7.4
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libapache2-mod-php7.4 libargon2-1 libpcre2-8-0 php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache
  php7.4-readline
The following NEW packages will be installed:
  libapache2-mod-php7.4 libargon2-1 php7.4 php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-readline
The following packages will be upgraded:
  libpcre2-8-0 php-common
2 upgraded, 8 newly installed, 0 to remove and 40 not upgraded.
Need to get 3,911 kB of archives.
After this operation, 17.3 MB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php-common all 2:93+ubuntu18.04.1+deb.sury.org+2 [16.3 kB]
Get:2 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-common amd64 1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1 [641 kB]
Get:3 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-json amd64 1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1 [18.7 kB]
Get:4 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-opcache amd64 1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1 [196 kB]
Get:5 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-readline amd64 1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1 [12.3 kB]
Get:6 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 libargon2-1 amd64 0~20190702-0.1+ubuntu18.04.1+deb.sury.org+1 [20.4 kB]
Get:7 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 libpcre2-8-0 amd64 10.40-1+ubuntu18.04.1+deb.sury.org+1 [206 kB]
Get:8 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4-cli amd64 1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1 [1,404 kB]
Get:9 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 libapache2-mod-php7.4 amd64 1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1 [1,346 kB]
Get:10 http://ppa.launchpad.net/ondrej/php/ubuntu bionic/main amd64 php7.4 all 1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1 [51.2 kB]
Fetched 3,911 kB in 3s (1,449 kB/s)
(Reading database ... 121438 files and directories currently installed.)
Preparing to unpack .../0-php-common_2%3a93+ubuntu18.04.1+deb.sury.org+2_all.deb ...
Unpacking php-common (2:93+ubuntu18.04.1+deb.sury.org+2) over (1:60ubuntu1) ...
Selecting previously unselected package php7.4-common.
Preparing to unpack .../1-php7.4-common_1%3a7.4.33-5+ubuntu18.04.1+deb.sury.org+1_amd64.deb ...
Unpacking php7.4-common (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
Selecting previously unselected package php7.4-json.
Preparing to unpack .../2-php7.4-json_1%3a7.4.33-5+ubuntu18.04.1+deb.sury.org+1_amd64.deb ...
Unpacking php7.4-json (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
Selecting previously unselected package php7.4-opcache.
Preparing to unpack .../3-php7.4-opcache_1%3a7.4.33-5+ubuntu18.04.1+deb.sury.org+1_amd64.deb ...
Unpacking php7.4-opcache (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
Selecting previously unselected package php7.4-readline.
Preparing to unpack .../4-php7.4-readline_1%3a7.4.33-5+ubuntu18.04.1+deb.sury.org+1_amd64.deb ...
Unpacking php7.4-readline (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
Selecting previously unselected package libargon2-1:amd64.
Preparing to unpack .../5-libargon2-1_0~20190702-0.1+ubuntu18.04.1+deb.sury.org+1_amd64.deb ...
Unpacking libargon2-1:amd64 (0~20190702-0.1+ubuntu18.04.1+deb.sury.org+1) ...
Preparing to unpack .../6-libpcre2-8-0_10.40-1+ubuntu18.04.1+deb.sury.org+1_amd64.deb ...
Unpacking libpcre2-8-0:amd64 (10.40-1+ubuntu18.04.1+deb.sury.org+1) over (10.31-2) ...
Selecting previously unselected package php7.4-cli.
Preparing to unpack .../7-php7.4-cli_1%3a7.4.33-5+ubuntu18.04.1+deb.sury.org+1_amd64.deb ...
Unpacking php7.4-cli (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
Selecting previously unselected package libapache2-mod-php7.4.
Preparing to unpack .../8-libapache2-mod-php7.4_1%3a7.4.33-5+ubuntu18.04.1+deb.sury.org+1_amd64.deb ...
Unpacking libapache2-mod-php7.4 (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
Selecting previously unselected package php7.4.
Preparing to unpack .../9-php7.4_1%3a7.4.33-5+ubuntu18.04.1+deb.sury.org+1_all.deb ...
Unpacking php7.4 (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
Setting up php-common (2:93+ubuntu18.04.1+deb.sury.org+2) ...
Setting up libargon2-1:amd64 (0~20190702-0.1+ubuntu18.04.1+deb.sury.org+1) ...
Setting up libpcre2-8-0:amd64 (10.40-1+ubuntu18.04.1+deb.sury.org+1) ...
Setting up php7.4-common (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...

Creating config file /etc/php/7.4/mods-available/calendar.ini with new version

Creating config file /etc/php/7.4/mods-available/ctype.ini with new version

Creating config file /etc/php/7.4/mods-available/exif.ini with new version

Creating config file /etc/php/7.4/mods-available/fileinfo.ini with new version

Creating config file /etc/php/7.4/mods-available/ffi.ini with new version

Creating config file /etc/php/7.4/mods-available/ftp.ini with new version

Creating config file /etc/php/7.4/mods-available/gettext.ini with new version

Creating config file /etc/php/7.4/mods-available/iconv.ini with new version

Creating config file /etc/php/7.4/mods-available/pdo.ini with new version

Creating config file /etc/php/7.4/mods-available/phar.ini with new version

Creating config file /etc/php/7.4/mods-available/posix.ini with new version

Creating config file /etc/php/7.4/mods-available/shmop.ini with new version

Creating config file /etc/php/7.4/mods-available/sockets.ini with new version

Creating config file /etc/php/7.4/mods-available/sysvmsg.ini with new version

Creating config file /etc/php/7.4/mods-available/sysvsem.ini with new version

Creating config file /etc/php/7.4/mods-available/sysvshm.ini with new version

Creating config file /etc/php/7.4/mods-available/tokenizer.ini with new version
Setting up php7.4-opcache (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...

Creating config file /etc/php/7.4/mods-available/opcache.ini with new version
Setting up php7.4-readline (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...

Creating config file /etc/php/7.4/mods-available/readline.ini with new version
Setting up php7.4-json (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...

Creating config file /etc/php/7.4/mods-available/json.ini with new version
Setting up php7.4-cli (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in auto mode
update-alternatives: using /usr/bin/phar7.4 to provide /usr/bin/phar (phar) in auto mode
update-alternatives: using /usr/bin/phar.phar7.4 to provide /usr/bin/phar.phar (phar.phar) in auto mode

Creating config file /etc/php/7.4/cli/php.ini with new version
Setting up libapache2-mod-php7.4 (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...

Creating config file /etc/php/7.4/apache2/php.ini with new version
libapache2-mod-php7.4: php7.2 module already enabled, not enabling PHP 7.4
Setting up php7.4 (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.6) ...
Processing triggers for php7.4-cli (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...
Processing triggers for libapache2-mod-php7.4 (1:7.4.33-5+ubuntu18.04.1+deb.sury.org+1) ...

If you need any additional PHP extensions, you can install them using the following format:

sudo apt install -y php7.4-[extension-name]

For example, to install the MySQL, GD, and XML extensions, run:

sudo apt install -y php7.4-mysql php7.4-gd php7.4-xml

Step 3: Verify PHP 7.4 Installation

After installing PHP 7.4, verify the installation by checking the PHP version:

php -v

The output should display PHP 7.4.x, where ‘x’ is the latest minor version.

root@geek:~# php -v
PHP 7.4.33 (cli) (built: Feb 14 2023 18:30:50) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.33, Copyright (c), by Zend Technologies

Step 4: Configure Your Web Server

If you’re using Apache, you may need to update the configuration to use PHP 7.4. First, disable the old PHP version and enable PHP 7.4:

sudo a2dismod php[old_version]
sudo a2enmod php7.4

Replace [old_version] with the previous PHP version you had installed (e.g., 7.2 or 7.3).

See also  How to Install and Configure sudo on Ubuntu

Next, restart the Apache service to apply the changes:

sudo systemctl restart apache2

If you’re using Nginx, you’ll need to update the PHP-FPM configuration. Open the Nginx configuration file for your website (usually located in /etc/nginx/sites-available/) and update the fastcgi_pass directive to point to the PHP 7.4 FPM socket:

fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;

Finally, restart the Nginx and PHP-FPM services to apply the changes:

sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm

Conclusion

You have now successfully updated to PHP 7.4 on your Ubuntu system. Be sure to test your web applications thoroughly to ensure compatibility with the new PHP version.

See also  How to Uninstall DirectAdmin on Ubuntu

If you have any questions, comments, or suggestions for improvement, please feel free to share your thoughts in the comments section below. Your feedback is invaluable to us, and it helps us create better and more informative content for our users.

Additional Considerations

After upgrading to PHP 7.4, it’s crucial to review the changes and deprecated features in this version. You can refer to the PHP 7.4 migration guide here for more information.

It’s also a good idea to check the error logs for any issues or warnings related to the PHP upgrade. For Apache, the error log is typically located at /var/log/apache2/error.log, and for Nginx, the error log can be found at /var/log/nginx/error.log. The PHP-FPM error log is usually located at /var/log/php7.4-fpm.log.

See also  Static Website Configuration for Nginx Web Server on CentOS 6 / CentOS 7

In case you encounter any issues after the upgrade, you might need to adjust your PHP scripts or configurations to comply with PHP 7.4 requirements. Keep in mind that some PHP extensions may also require updates or may no longer be supported in PHP 7.4.

Remember to keep your PHP version up-to-date by regularly checking for updates and applying them as needed. This ensures that your system remains secure and benefits from the latest performance improvements and features.

If you need to switch between multiple PHP versions, consider using a tool like phpbrew to manage different PHP installations more easily.

Feel free to leave any questions or suggestions in the comments section below. We’re here to help you make the most of your PHP installation and ensure a seamless experience when upgrading to newer versions.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *