Add PPA repository
Ubuntu's default PPA repositories will probably not include the latest PHP version so you'll want to add
sudo add-apt-repository ppa:ondrej/php sudo apt-get update
Install PHP packages
Head over to the PHP website to check the current stable version. At the time of writing, this is 8.1.1.
Assuming we'll use PHP with Nginx and MySQL, we'll want to install two packages.
sudo apt install php8.1-fpm php8.1-mysql
These two packages will download other core PHP packages as their dependencies.
This should output something like:
PHP 8.1.1 (cli) (built: Dec 31 2021 07:26:20) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.1, Copyright (c) Zend Technologies with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies