Skip to main content

Working with multiple versions of PHP 7.3, 7.2, 7.1, 7.0 & 5.6 on Ubuntu

 

Sometimes PHP developers need to work on multiple versions of PHP for different projects and one might think of using a docker installation, which is a better idea of what we are going to do below, but Docker can be complicated for many reasons and one might not be ready to get started with Docker. In the below article, I will be explaining how one can work with multiple PHP version on Ubuntu. I am using ubuntu 18.10 and the below article for me without any issues.

Install PHP (5.6, 7.0, 7.1, 7.2 and 7.3) on Ubuntu Using PPA

1.  Adding OndÅ™ej PPA to install different versions of PHP – PHP 5.6PHP 7.0, PHP 7.1, PHP 7.2 and PHP 7.3 on the system.

sudo apt-get install software-properties-common

## sudo apt install python-software-properties

sudo add-apt-repository ppa:ondrej/php

2.  Update the system

Make sure you have the sudo privileges to do so.

sudo apt-get update



3. Add different versions of PHP as required

I will be adding fpm version as well, you may ignore them if you do not need them

sudo apt install php5.6 php5.6-fpm

sudo apt install php7.0 php7.0-fpm

sudo apt install php7.1 php7.1-fpm

sudo apt install php7.2 php7.2-fpm

sudo apt install php7.3 php7.3-fpm

4. You might need to add the most common modules one normally uses with PHP

I work a lot with Laravel, WordPress, Symfony, EspoCRM & Zend Framework so I shall add the below modules for all the PHP versions

For PHP 5.6

sudo apt install php-gmagick php5.6-xml php-ssh2 php5.6-curl php5.6-gd php5.6-mbstring php5.6-mysql php-tokenizer php-xdebug php-apcu php5.6-bcmath php5.6-bz2 php-memcache php-zip

For PHP 7.0

sudo apt install php-gmagick php7.0-xml php-ssh2 php7.0-curl php7.0-gd php7.0-mbstring php7.0-mysql php-xdebug php-apcu php7.0-bcmath php7.0-bz2 php-memcache php7.0-zip

For PHP 7.1

sudo apt install php-gmagick php7.1-xml php-ssh2 php7.1-curl php7.1-gd php7.1-mbstring php7.1-mysql php-xdebug php-apcu php7.1-bcmath php7.1-bz2 php-memcache php7.1-zip

For PHP 7.2

sudo apt install php-gmagick php7.2-xml php-ssh2 php7.2-curl php7.2-gd php7.2-mbstring php7.2-mysql php-xdebug php-apcu php7.2-bcmath php7.2-bz2 php-memcache php7.2-zip

For PHP 7.3

sudo apt install php-gmagick php7.3-xml php-ssh2 php7.3-curl php7.3-gd php7.3-mbstring php7.3-mysql php-xdebug php-apcu php7.3-bcmath php7.3-bz2 php-memcache php7.3-zip

Well the above should be more than enough to get you started with your PHP projects.

5. Check the installed versions of PHP

Execute the following command to check all the versions of PHP installed on the system:

ls -al /usr/bin | grep php

-------------------------- output -------------------------------

lrwxrwxrwx 1 root root 21 Mar 26 2020 php - /etc/alternatives/php*

-rwxr-xr-x 1 root root 4512600 Jan 16 12:12 php5.6*

-rwxr-xr-x 1 root root 4508520 Dec 10 11:23php7.0*

-rwxr-xr-x 1 root root 4665176 Jan 20 20:56 php7.1*

-rwxr-xr-x 1 root root 5014552 Feb 14 22:26 php7.2*

-rwxr-xr-x 1 root root 4769528 Feb 18 01:33 php7.3*

To check your current version of PHP execute the following command:

php -v


It should output something as below:

PHP 7.3.2-3+ubuntu18.10.1+deb.sury.org+1 (cli) (built: Feb 18 2019 15:44:30) ( NTS ).

Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.2, Copyright (c) 1998-2018 Zend Technologies

with Zend OPcache v7.3.2-3+ubuntu18.10.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

with Xdebug v2.7.0RC2, Copyright (c) 2002-2019, by Derick Rethans

Compiling extensions or working with PHP Dev tools?

If you are planning to compile shared PECL extensions install the PHP dev tools for respective version

sudo apt install php5.6-dev

sudo apt install php7.0-dev

sudo apt install php7.1-dev

sudo apt install php7.2-dev

sudo apt install php7.3-dev

6. Changing PHP version

One may use the command `update-alternatives` to update the default PHP version

For changing PHP version to 5.6 execute the following command

sudo update-alternatives --set phpize /usr/bin/phpize5.6

sudo update-alternatives --set php /usr/bin/php5.6

sudo update-alternatives --set php-config /usr/bin/php-config5.6

For changing to PHP 7.0:

sudo update-alternatives --set phpize /usr/bin/phpize7.0

sudo update-alternatives --set php /usr/bin/php7.0

sudo update-alternatives --set php-config /usr/bin/php-config7.0

For changing to PHP 7.1:

sudo update-alternatives --set phpize /usr/bin/phpize7.1

sudo update-alternatives --set php /usr/bin/php7.1

sudo update-alternatives --set php-config /usr/bin/php-config7.1

For changing to PHP 7.2:

sudo update-alternatives --set phpize /usr/bin/phpize7.1

sudo update-alternatives --set php /usr/bin/php7.1

sudo update-alternatives --set php-config /usr/bin/php-config7.1

For changing to PHP 7.3:

sudo update-alternatives --set phpize /usr/bin/phpize7.3

sudo update-alternatives --set php /usr/bin/php7.3

sudo update-alternatives --set php-config /usr/bin/php-config7.3


For PHP professionals working on multiple frameworks, using multiple versions of PHP becomes inevitable. Hope the above content help others as it is was helpful for me.

Comments

Popular posts from this blog

Apt-fast for Increasing Download Speed while Installing and Updating Packages Ubuntu/Debian [Beginners Guide]

Have you ever experienced slow download speed when downloading or updating packages in Ubuntu even when your network connection is running fine? You encounter this issue especially when updating and installing packages for the first time after installing a new Ubuntu/Debian OS. However, there is a way to get around this by speeding up the download speed using apt-fast command in Linux. Apt-fast is a shell script wrapper for “apt-get” and “aptitude” that uses the power of either  axel  or  aria 2  download managers to accelerate the download process. It improves download performance by downloading packages simultaneously in parallel with multiple packages per connection. In this article, we will walk through some steps to install apt-fast in order to accelerate the update and downloading process in Ubuntu. We will use Ubuntu 18.04 LTS for describing the procedure mentioned in this article.   Step 1: Installing prerequisites We need to install...

What is a CDN ? How Does a content delivery network Work?

I ntroduction. Latest Web sites and applications often need to provide   amount of static content to end users. This content includes images, style sheets, JavaScript, and video. The increase in the number of static assets and the increase grows the bandwidth usage increases page load time decreased, depending on the size of the user's search experience, and reduce the usable capacity of the server. Dramatically improving performance, reducing the page load time   reduce the bandwidth and infrastructure costs, you can implement a content delivery network,  And  CDN cache these assets at a set of servers that are geographically distributed. What is a CDN? Content delivery network is a group of servers distributed geographically optimized to provide static content to end users. While this static content can be almost any type of data, CDNs are most commonly used to deliver web pages and related files, streaming video and audio, and large softwar...

How To Install PHP 7.4 and PHP 7.3 ON Ubuntu 18.04/19.04/16.04

How to Install PHP 7.4 / PHP 7.3 on Ubuntu?. This guide will help you Install PHP 7.4 / PHP 7.3 on Ubuntu 18 / Ubuntu 16 /  Ubuntu 19 . PHP is an open-source server-side scripting language which has been widely adopted for the creation of dynamic web pages. PHP is secure, fast, simple, efficient, flexible and a loosely typed scripting language. The PHP release 7.4.0 has been made available for the general public and for use in Production environments. How to install PHP 7.4 on Ubuntu 18.04 / Ubuntu 19.04 / Ubuntu 16.04 Step 1: Add PHP 7.4 PPA Repository We’ll add ppa:ondrej/php PPA repository which has the latest build packages of PHP. sudo apt-get update sudo apt -y install software-properties-common sudo add-apt-repository ppa:ondrej/php sudo apt-get update Step 2: Install PHP 7.4 on Ubuntu 18/ Ubuntu 19/ Ubuntu 16 Install PHP 7.4 on Ubuntu 18.04/19.04/16.04 using the command: sudo apt -y install php7.4 Check version inst...