How to install php 7.4 on Ubuntu 21.04

To Ensure system updated.

apt update

If any update is available update the system and reboot.

apt upgrade -y && reboot

Add PHP PPA Repository

apt -y install software-properties-common

add-apt-repository ppa:ondrej/php

Output:

root@vps:~# add-apt-repository ppa:ondrej/php
PPA publishes dbgsym, you may need to include 'main/debug' component
Repository: 'deb http://ppa.launchpad.net/ondrej/php/ubuntu/ hirsute main'
Description:
Co-installable PHP versions: PHP 5.6, PHP 7.x and most requested extensions are included. Only Supported Versions of PHP (http://php.net/supported-versions.php) for Supported Ubuntu Releases (https://wiki.ubuntu.com/Releases) are provided. Don't ask for end-of-life PHP versions or Ubuntu release, they won't be provided.

Debian oldstable and stable packages are provided as well: https://deb.sury.org/#debian-dpa

You can get more information about the packages at https://deb.sury.org

IMPORTANT: The <foo>-backports is now required on older Ubuntu releases.

BUGS&FEATURES: This PPA now has a issue tracker:
https://deb.sury.org/#bug-reporting

CAVEATS:
1. If you are using php-gearman, you need to add ppa:ondrej/pkg-gearman
2. If you are using apache2, you are advised to add ppa:ondrej/apache2
3. If you are using nginx, you are advised to add ppa:ondrej/nginx-mainline
   or ppa:ondrej/nginx

PLEASE READ: If you like my work and want to give me a little motivation, please consider donating regularly: https://donate.sury.org/

WARNING: add-apt-repository is broken with non-UTF-8 locales, see
https://github.com/oerdnj/deb.sury.org/issues/56 for workaround:

# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
More info: https://launchpad.net/~ondrej/+archive/ubuntu/php
Adding repository.
Press [ENTER] to continue or Ctrl-c to cancel.

Install PHP 7.4 on Ubuntu

Ensure the added package is up to date.

apt update

If any update is available update the system and reboot.

apt upgrade -y && reboot

Install PHP 7.4 on Ubuntu

apt -y install php7.4

Output:

root@vps:~# apt -y install php7.4
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php7.4 libapr1
  libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libjansson4 liblua5.3-0
  libsodium23 php-common php7.4-cli php7.4-common php7.4-json php7.4-opcache
  php7.4-readline ssl-cert
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser ufw php-pear
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapache2-mod-php7.4 libapr1
  libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libjansson4 liblua5.3-0
  libsodium23 php-common php7.4 php7.4-cli php7.4-common php7.4-json php7.4-opcache
  php7.4-readline ssl-cert
0 upgraded, 20 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,091 kB of archives.
After this operation, 26.7 MB of additional disk space will be used.

Once you have installed PHP, check the PHP version.

php -v

Output:

 root@vps:~# php -v
PHP 7.4.16 (cli) (built: Mar 23 2021 16:15:03) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.16, Copyright (c), by Zend Technologies
root@vps:~#

To Install php Extensions.

NOTE: Replace the required extensions name in the command

apt install php7.4-<extension>

To list installed php modules/extensions.

php -m

This concludes our topic of installing PHP 7.4 on the Ubuntu 20.04 system.