How to Install PHP 7.2 on Ubuntu 16.04?

PHP 7.2 is the latest stable version of PHP and has many new features, improvements, and bug fixes. You should definitely use it if you want a better, faster website/application.

Update Ubuntu

Of course, as always, first update Ubuntu:

sudo apt-get update && apt-get upgrade

Add the PHP repository

You can use a third-party repository to install the latest version of PHP. We’ll use the repository by Ondřej Surý.

First, make sure you have the following package installed so you can add repositories:

sudo apt-get install software-properties-common

Next, add the PHP repository from Ondřej:

sudo add-apt-repository ppa:ondrej/php

And finally, update your package list:

sudo apt-get update

Install PHP 7.2

After you’ve added the repository, you can install PHP 7.2 with the following command:

sudo apt-get install php7.2

This command will install additional packages:

  • libapache2-mod-php7.2
  • libargon2-0
  • libsodium23
  • libssl1.1
  • php7.2-cli
  • php7.2-common
  • php7.2-json
  • php7.2-opcache
  • php7.2-readline

And that’s it. To check if PHP 7.2 is installed on your server, run the following command:

php -v

Install PHP 7.2 modules

You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with the following command:

sudo apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml

And that’s all. You can now start using PHP on your Ubuntu server.