How to install Php 7 on Ubuntu

How to Ubuntu

In this tutorial we will learn to install Php 7 on Ubuntu.

Step #1: Update

Before we go ahead and install Php 7 on our Ubuntu computer let us update the packages using the following command.

So, open Terminal and run the following command.

$ sudo apt update

Note! In the above command we are using sudo so, it will prompt you to enter your password.

If you see any package that needs upgrade then run the following command.

$ sudo apt upgrade

Step #2: Install Php

Run the following command in the terminal to install Php.

$ sudo apt install php

Or you can use the following command to get version 7.2.

$ sudo apt install php7.2

After installing php you will be able to check the installed version by typing php -v command.

$ php -v
PHP 7.2.5-0ubuntu0.18.04.1 (cli) (built: May  9 2018 17:21: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.5-0ubuntu0.18.04.1, Copyright (c) 1999-2018, by Zend Technologies

At the time of writing this tutorial I had Php v7.2.5 on my machine.


List Php modules

Run the following command in the terminal to list all the Php modules.

$ apt-cache search --names-only ^php

The given command will list Php modules similar to the one given below.

$ apt-cache search --names-only ^php
libapache2-mod-php7.2 - server-side, HTML-embedded scripting language (Apache 2 module)
php - server-side, HTML-embedded scripting language (default)
php-all-dev - package depending on all supported PHP development packages
php-cgi - server-side, HTML-embedded scripting language (CGI binary) (default)
php-cli - command-line interpreter for the PHP scripting language (default)
php-common - Common files for PHP packages
php-curl - CURL module for PHP [default]
php-dev - Files for PHP module development (default)
php-gd - GD module for PHP [default]
php-gmp - GMP module for PHP [default]
php-ldap - LDAP module for PHP [default]
php-mysql - MySQL module for PHP [default]

So, depending on your needs you can install the Php modules from the above list using the following command.

$ sudo apt install php_module_name

Where, php_module_name can be lets say php-mysql.

Alright, this brings us to the end of this tutorial. If you find this tutorial helpful then please share it with everyone.

Thanks for reading and have fun coding :-)