How to Install and Configure PHP 7.2 on Ubuntu 18.04

Updated on November 16, 2018
How to Install and Configure PHP 7.2 on Ubuntu 18.04 header image

PHP and related packages are the most commonly used components when deploying a web server. In this article, we will learn how to setup PHP 7.2 on Ubuntu 18.04 LTS.


  • An up-to-date Ubuntu 18.04 server instance.
  • A sudo user.

Update Ubuntu 18.04

First, update the list of packages:

sudo apt-get update -y

Next, install the updates:

sudo apt-get upgrade -y

Create your sudo user

Ubuntu ships with sudo installed, so the first step will be to simply add a new user:

adduser <username>

You will be asked to set information about this user:

Enter the new value, or press ENTER for the default
Full Name []: Test User
Room Number []: 01
Work Phone []: 5555555
Home Phone []: 5555555
Other []:

You can fill these fields in, or press Enter to leave them default. After this press Y and then Enter to verify that the information is correct.

Next, add the new user to the sudo group:

usermod -aG sudo <username>

You can now log out, and log back in as your new user. To test that the user was added correctly, use the following command once you have logged back in as the new user:

ls -la /root

You will receive the following notice:

ls: cannot open directory '/root': Permission denied

When you append the previous command with sudo, you will be asked for your password and receive a list of the /root directory:

sudo ls -la /root

You can now move onto updating Ubuntu.

Install a webserver

You can use Apache or Nginx as your webserver.

To install and start Apache:

sudo apt-get install apache2 -y
sudo systemctl start apache2.service

To install and start Nginx:

sudo apt-get install nginx -y
sudo systemctl start nginx.service

Install PHP 7.2

PHP 7.2 is included in the default Ubuntu repository for 18.04. You can list each of the available PHP 7.2 packages with the following command:

apt-cache pkgnames | grep php7.2

Next, install the packages that your application requires:

sudo apt-get install php -y
sudo apt-get install php-{bcmath,bz2,intl,gd,mbstring,mysql,zip,fpm} -y

Finally, restart your webserver to allow PHP to run.

For Apache, use the following:

systemctl restart apache2.service

Alternatively, use the following for Nginx:

systemctl restart nginx.service

Confirm the PHP version:

php -v

The output will resemble the following:

PHP 7.2.10-0ubuntu0.18.04.1 (cli) (built: Sep 13 2018 13:45:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
	with Zend OPcache v7.2.10-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

The main config file of PHP 7.2 will be saved as /etc/php/7.2/fpm/php.ini. You can use the vi text editor to modify relevant settings in that file:

sudo vi /etc/php/7.2/fpm/php.ini

Note: Remember to restart Apache or Nginx if you make any changes to that file or any other PHP config files.

You have successfully set up PHP 7.2 on Ubuntu 18.04 to work with either Nginx or Apache. You are now ready to customize your configurations and deploy your apps.