Install multiple PHP versions in Ubuntu

Standard

PHP (recursive acronym for PHP: Hypertext Preprocessor) is

  • an open source, popular general-purpose scripting language that is widely-used
  • best suited for developing websites and web-based applications
  • server-side scripting language that can be embedded in HTML

Currently, there are 3 PHP version ie PHP 5.67.0 and 7.1 are supported versions of PHP.

PHP 5.35.4 and 5.5 are no longer supported with security updates.

Step 1

1.1. Add PPA called Ondřej Surý PPA which will let you install different versions of PHP – PHP 5.6PHP 7.0, PHP 7.1 and PHP 7.2 on Ubuntu.

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

NOTE: If you have already added this PPA, you can jump up to step 2. To check if Ondřej Surý PPA is added or not, here is a command you can try:

$ ls /etc/apt/sources.list.d/ | grep ondrej

1.2. Next, update the system as follows.

$ sudo apt-get update

Step 2

2.1. To install different supported versions of PHP as follows.

------------------- For Apache Web Server -------------------
$ sudo apt install php5.6   [PHP 5.6]
$ sudo apt install php7.0   [PHP 7.0]
$ sudo apt install php7.1   [PHP 7.1]
------------------- For Nginx Web Server -------------------
$ sudo apt install php5.6-fpm   [PHP 5.6]
$ sudo apt install php7.0-fpm   [PHP 7.0]
$ sudo apt install php7.1-fpm   [PHP 7.1]

 

2.2. To install any PHP modules, simply specify the PHP version and use the auto-completion functionality to view all modules as follows.

------------ press [Tab] key for auto-completion ------------ 
$ sudo apt install php5.6 
$ sudo apt install php7.0 
$ sudo apt install php7.1

2.3. You can install most required PHP modules from the list.

------------ Install PHP Modules ------------
$ sudo apt install php5.6-cli php5.6-xml php5.6-mysql 
$ sudo apt install php7.0-cli php7.0-xml php7.0-mysql 
$ sudo apt install php7.1-cli php7.1-xml php7.1-mysql

Step 3: Set Default PHP Version in Ubuntu

3.1. Finally, verify your default PHP version used on your system like this.

$ php -v

yogal@yogalshrestha:~$ php -v
PHP 7.1.13-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan 5 2018 13:26:45) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.1.13-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

3.2. To set the default PHP version to be used on the system with the update-alternatives command.

------------ Set Default PHP Version 5.6 ------------
$ sudo update-alternatives --set php /usr/bin/php5.6

yogal@yogalshrestha:~$ sudo update-alternatives –set php /usr/bin/php5.6
update-alternatives: using /usr/bin/php5.6 to provide /usr/bin/php (php) in manual mode
yogal@yogalshrestha:~$ php -v
PHP 5.6.32-1+ubuntu16.04.1+deb.sury.org+2 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

------------ Set Default PHP Version 7.1 ------------
$ sudo update-alternatives --set php /usr/bin/php7.1

yogal@yogalshrestha:~$ sudo update-alternatives –set php /usr/bin/php7.1
update-alternatives: using /usr/bin/php7.1 to provide /usr/bin/php (php) in manual mode
yogal@yogalshrestha:~$ php -v
PHP 7.1.13-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan 5 2018 13:26:45) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.1.13-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

3.3. To set the PHP version in Apache web server.

  • Firstly, disable the current version with the a2dismod command
  • Then enable the one you want with the a2enmod command.
$ sudo a2dismod php7.1
$ sudo a2enmod php4.6
$ sudo systemctl restart apache2

3.4. After switching from one version to another, you can find your PHP configuration file, by running the command below.

------------ For PHP 5.6 ------------
$ sudo update-alternatives --set php /usr/bin/php5.6
$ php -i | grep "Loaded Configuration File"
------------ For PHP 7.0 ------------
$ sudo update-alternatives --set php /usr/bin/php7.0
$ php -i | grep "Loaded Configuration File"
------------ For PHP 7,1 ------------
$ sudo update-alternatives --set php /usr/bin/php7.1
$ php -i | grep "Loaded Configuration File"

yogal@yogalshrestha:~$ php -i | grep “Loaded Configuration File”
Loaded Configuration File => /etc/php/7.1/cli/php.ini

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s