This post will show you how to install PHP 7.2 on Debian 8 (jessie) or Debian 9 (stretch).

Adding the repository to apt

First off, install the required packages.

sudo apt install ca-certificates apt-transport-https lsb-release -y

Then we need to add the sury.org GPG key.

sudo wget -O /etc/apt/trusted.gpg.d/php.gpg https://packages.sury.org/php/apt.gpg

Now we can add the repository.

echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/php.list

Update apt cache.

sudo apt update -y

Installing PHP 7.2

Now when we have the repository added, we can use apt to install php7.2

Install php7.2 using apt.

sudo apt install php7.2

If you would like to, you can also install additional PHP packages which may be required.

sudo apt install php7.2-cli php7.2-opcache php7.2-common php7.2-curl php7.2-json php7.2-mbstring php7.2-mysql php7.2-readline php7.2-xml php7.2-gd -y

Verify the installation

Verify the installation by running php in your terminal.

php -v

This should print the PHP version if installed correctly.