How to Install Different PHP (5.6, 7.0 and 7.1) Versions in Ubuntu

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

32 Responses

  1. Ahmad Farel says:

    Work perfectly on ubuntu16.04, thanks

  2. Sawan Choubisa says:

    Thanks for the useful suggestion. appreciated

  3. JJ says:

    Thanks for informative article.

    Apparently Apache 2.4 can run more than one version of PHP i.e. I have that runs PHP 5.6 and I have that runs PHP 7.1.

    Any idea how to do this?

  4. Anmol Rastogi says:

    Thankyou so much !
    this helped a lot :)

  5. Anderson Filho says:

    Great tutorial. Very useful. Thanks a lot.

  6. Marcel says:

    This was the first tutorial with multiple php versions that worked for me. First off all thank you!

    To have a more complete tutorial you should may add the install commands for the mbstring extension of each php version.

    $ sudo apt-get install php7.0-mbstring

    Best Marcel

  7. LeonidMew says:

    I have install php5.6 php7.0 php7.1 packages, also installed -cgi, -cli and many more modules for all versions, but only update-alternatives example changes cli version while changing apache php version gives error:

    [email protected]:/etc/apache2$ sudo a2enmod php7.1
    ERROR: Module php7.1 does not exist!

    I have do the same for all versions, but somehow not all libapache2-mod-php was installed.

    So I do following to fix error: (libapache2-mod-php5.6 was only one already installed)

    $ sudo apt-get install libapache2-mod-php5.6 libapache2-mod-php7.0 libapache2-mod-php7.1

    After that a2dismod and a2enmod work as expected.

    Also, I have analyze install log and realized what update-alternatives example should be expanded to some thing like this:

    $ sudo update-alternatives --set php /usr/bin/php7.1
    $ sudo update-alternatives --set phar /usr/bin/phar7.1
    $ sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.1
    $ sudo update-alternatives --set phpdbg /usr/bin/phpdbg7.1
    $ sudo update-alternatives --set php-cgi /usr/bin/php-cgi7.1
    $ sudo update-alternatives --set php-cgi-bin /usr/lib/cgi-bin/php7.1

    replace numbers with version you need..

  8. Fabio says:

    Awesome article, Aaron! Thanks a lot!

  9. John says:

    On my server I’m still with the issue of having the wrong CLI version as it still show an old version not installed but apache uses the correct version. I need to find out how to have the correct CLI because some console applications need the correct version. The steps above will not help as it only changes the version used by apache. Any help on this? Thanks

  10. Jaro says:

    This command doesn’t work (shows php cli ini):

    php –ini | grep “Loaded Configuration File”

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.