How to install PHP 7.0 or PHP 7.1 on Ubuntu

Most of the servers use PHP 5.5.9 or 5.6. Those PHP versions are not going to support soon. Since more and more applications use PHP 7.x to build, then we also need to upgrade it to our server.

Note: I am using Ubuntu 14.04. This way works for Ubuntu 16.04 as well.

Install PHP 7.0

First of all, you need to update modules

sudo apt-get update

Then, you can list all of the available PHP 7.0 packages for review

apt-cache pkgnames | grep php7.0
PHP 7.0 packages
PHP 7.0 packages

Select the packages that you want to install

sudo apt-get install php7.0 libapache2-mod-php7.0 php7.0-cli php7.0-common php7.0-mbstring php7.0-gd php7.0-intl php7.0-xml php7.0-mysql php7.0-mcrypt php7.0-zip

After the installation, check PHP version with command

php -v

The output should resemble

PHP 7.0.15-0ubuntu0.16.04.4 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.15-0ubuntu0.16.04.4, Copyright (c) 1999-2017, by Zend Technologies

Install PHP 7.1

First of all, you also need to check wether PHP 7.1 is available or not

apt-cache pkgnames | grep php7.1

If it is still not yet, so you need to download it.
You add specified PPA to the system apt repository

sudo apt-get install -y python-software-properties
sudo add-apt-repository -y ppa:ondrej/php

Note: If you get this error add-apt-repository command not found, you need to install this module:

sudo apt-get install software-properties-common

After that run update command again

sudo apt-get update -y

Finally, you can install PHP 7.1 same PHP 7.0. Just change 7.0 to 7.1

sudo apt-get install php7.1 libapache2-mod-php7.1 php7.1-cli php7.1-common php7.1-mbstring php7.1-gd php7.1-intl php7.1-xml php7.1-mysql php7.1-mcrypt php7.1-zip

Ok, now you need to disable PHP 5 module

sudo a2dismod php5.6

and active PHP 7.x module

sudo a2enmod php7.x

Restart your server to apply the changes

service apache2 restart

That’s it. Hope this help.

Leave a comment