First, make sure you have the following package installed so you can add repositories:
apt-get install software-properties-common
Next, add the PHP repository from Ondřej:
add-apt-repository ppa:ondrej/php
And finally, update your package list:
apt-get update
After you’ve added the repository, you can install PHP 8.1 with the following command:
apt-get install php8.1