In order to have a version of PHP 7.4 greater than 7.4.3, you will need to use Ondřej Surý's PPA. This will give you 7.4.27 if you are running Ubuntu 18.04, 20.04, 21.04, or 21.10. Fortunately, it's not too difficult to do.
WARNING: You need to understand that by following these steps, the web server will be OFFLINE during the PHP version change. You will be uninstalling 7.4.3 and installing 7.4.27, which does take a couple of minutes. If you are doing this on a production machine that does not have any spare for maintenance, your visitors will need to understand the error(s) they see are temporary.
With that out of the way, let's get started.
If you are updating an existing workstation/server:
Open Terminal or connect to the server where you need 7.4.27
Stop Apache:
sudo service apache2 stop
Note: This technically is not required, but it's just "cleaner".
Uninstall all PHP packages on your machine:
sudo apt purge `dpkg -l | grep php| awk '{print $2}' |tr "\n" " "`
Source
NOTE: Read the packages that are going to be uninstalled and be sure that the list is correct before continuing. If you do not agree with the list, then STOP.
Once the packages are removed, clean apt
:
sudo apt clean
Now that PHP has been removed from the system, let's continue.
Installing Ondřej Surý's version of PHP:
Open Terminal or connect to the server where you need 7.4.27
Ensure Apache is not running:
sudo service apache2 stop
Note: This technically is not required, but it's just "cleaner". Of course, if Apache isn't installed, you don't need to do this.
Add the appropriate PPA:
sudo add-apt-repository ppa:ondrej/php
Update apt
:
sudo apt update
Install PHP 7.4.27:
sudo apt install php7.4
Note: While the package states 7.4
, this will give you 7.4.27-1+ubuntu20.04.1+deb.sury.org+1
on 20.04 (as of this writing).
Of course, feel free to add any other PHP packages that you may need, but keep in mind that you must specifically specify the major and minor versions for every additional package. For example:
sudo apt install php7.4 php7.4-json php7.4-xml php7.4-mbstring ...
Once complete, confirm the installed version:
$ php -v
PHP 7.4.27 (cli) (built: Nov 25 2021 23:16:22) ( NTS )
Copyright (c) The PHP Group ...
Start/Restart Apache (if that's the web server you're using):
sudo service apache2 start
Now you're using a fully up-to-date version of PHP 7.4.x, which will receive security updates until November 28, 2022. Hopefully by then there will be a plan for you to upgrade to PHP 8.0 or 8.1.