Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»PHP»How to Switch between Multiple PHP Version on Ubuntu

    How to Switch between Multiple PHP Version on Ubuntu

    RahulBy RahulDecember 26, 20162 Mins ReadUpdated:July 29, 2020

    On your system, if you have installed multiple versions of PHP (eg PHP 7.4 and PHP 5.6 both). PHP 7.4 is running as default PHP for Apache and CLI. For any requirement, you need to use PHP 5.6. Then you don’t need to remove PHP 7.4. You can simply switch your PHP version to default used for Apache and command line.

    For example, your server has PHP 7.4 and PHP 5.6 both version’s installed. Now following example will help you to switch between both versions. The PHP 5.6 is configured as default PHP version for Apache and CLI. Let’s make PHP 7.4 as default PHP for Apache server and CLI.

    • Read => How to Install PHP 5.6 or PHP 7 on Ubuntu with PPA

    Switch From PHP 5.6 => PHP 7.4

    The PHP 5.6 is set as default version in your system, and you need to switch to PHP 7.4. Run the following commands to switch for Apache and command line.

    Apache:-

    sudo a2dismod php5.6
    sudo a2enmod php7.4
    sudo service apache2 restart
    

    Command Line:-

    sudo update-alternatives --set php /usr/bin/php7.4
    sudo update-alternatives --set phar /usr/bin/phar7.4
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.4
    sudo update-alternatives --set phpize /usr/bin/phpize7.4
    sudo update-alternatives --set php-config /usr/bin/php-config7.4
    

    Note – The phpize7.4 and php-config7.4 command is available in php7.4-dev package. This is more useful for compiling PHP modules using pecl.

    Switch From PHP 7.4 => PHP 5.6

    Default PHP 7.4 is set on your system and you need to switch to PHP 5.6. Now run the following commands to switch for Apache and command line.

    Apache:-

    sudo a2dismod php7.4
    sudo a2enmod php5.6
    sudo service apache2 restart
    

    Command Line:-

    sudo update-alternatives --set php /usr/bin/php5.6
    sudo update-alternatives --set phar /usr/bin/phar5.6
    sudo update-alternatives --set phar.phar /usr/bin/phar.phar5.6 
    sudo update-alternatives --set phpize /usr/bin/phpize5.6
    sudo update-alternatives --set php-config /usr/bin/php-config5.6
    

    Note – The phpize5.6 and php-config5.6 command is available in php5.6-dev package. This is more useful for compiling PHP modules using pecl.

    Conclusion

    This tutorial helps you to switch between multiple PHP versions on Ubuntu system. This helps you to change default PHP version on your Apache server and command line interface.

    PHP PHP 5.6 PHP 7.1 PHP7.2
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleDeploying Node Application using PM2
    Next Article How to Password Protect WordPress Admin (wp-admin) URL

    Related Posts

    How to Install Composer on Ubuntu 22.04

    Updated:June 24, 20223 Mins Read

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:June 19, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

    Updated:June 28, 20225 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    How To Install and Use PHP Composer on Debian 11

    Updated:February 16, 20224 Mins Read

    33 Comments

    1. ameblo.jp on February 2, 2022 6:09 pm

      This is a topic which is close to myy heart… Cheers!

      Exactly where are your contact details though?

      Reply
    2. wallyDef on January 20, 2021 4:03 pm

      i am so satisfacted.my english is poor, sorry :). thx for approving my user greetings wally

      Reply
      • paul on June 8, 2021 1:41 pm

        Would you expect the output of my test info.php to show PHP 5.6?
        #php -i | grep “Loaded Configuration File”
        Loaded Configuration File => /etc/php/5.6/cli/php.ini

        But the info.php still shows
        Configuration File (php.ini) Path /etc/php/7.4/fpm
        Loaded Configuration File /etc/php/7.4/fpm/php.ini

        Reply
    3. wallyDef on November 17, 2020 1:49 am

      i am so satisfacted. greetings wally

      Reply
    4. sammie on November 5, 2020 2:01 pm

      thanks….

      Reply
    5. Danial on August 1, 2020 12:34 pm

      I made a bash function for this process. i hope it be useful:

      #disable current php and enable php with passed version
      function switch_php {
      sudo a2dismod php$(php -v | grep -o -E ‘[0-9.]+’ -m 1 | cut -c 1-3 | head -n 1);
      sudo a2enmod php$1;
      sudo service apache2 restart;
      sudo update-alternatives –set php /usr/bin/php$1;
      sudo update-alternatives –set phar /usr/bin/phar$1;
      sudo update-alternatives –set phar.phar /usr/bin/phar.phar$1;
      sudo update-alternatives –set phpize /usr/bin/phpize$1;
      sudo update-alternatives –set php-config /usr/bin/php-config$1;
      }

      //sample:
      $ switch_php 7.1
      or
      $ switch_php 5.6

      Reply
    6. stanley on July 17, 2020 11:00 am

      thank you

      Reply
    7. Jesus on May 11, 2020 8:06 pm

      Thanks for the tutorial, I was struggling trying to use PHP 7.3 in Ubuntu 20 and this helped me to configure things as needed.

      BTW, do you know of any way besides pecl install –force to build an extension for different PHP version?

      Reply
    8. Yılmaz Saraç on May 4, 2020 2:30 pm

      thanks, it helped much!

      Reply
    9. scorp13 on March 29, 2019 7:25 pm

      Not touching the tutorial itself, but it’s time to abandon the 5.6 version

      Reply
    10. daniel on March 6, 2019 8:06 pm

      How can I check which modules have each php versions without having to switch them and run “php -m” ?
      Thank you

      Reply
      • Rahul on March 7, 2019 7:41 am

        Use the command with PHP version like:

        php7.2 -m
        php5.6 -m

        For enable/disable modules read: https://tecadmin.net/enable-disable-php-modules-ubuntu/

        Reply
        • sammie on November 5, 2020 2:01 pm

          thanks…

          Reply
        • sammie on November 5, 2020 2:03 pm

          I hope this

          Reply
          • sammie on November 5, 2020 2:03 pm

            Also I hope

            Reply
    11. AZAHAR UDDIN AHMED on August 15, 2018 9:55 am

      I’m using Centos 7 and I want to switch default version from ” PHP 5.4.16 (cli) ” to ” PHP 7.0.27 ” in shall so for that what would be the command?

      Reply
    12. Tanmay K on July 10, 2018 7:05 am

      Thanks, Rahul. This is exactly what I was looking for.

      Reply
    13. Shiv on May 28, 2018 8:39 pm

      Great post Rahul,

      Even I was on the same issue few months back. Similar article I found on a tech blog which saved my day.

      Great explanation and detailing. Keep up the good work.

      Reply
    14. Ca Stor on March 29, 2018 6:58 pm

      Thank you my friend.:)

      Reply
    15. Cristian Felipe on March 1, 2018 5:38 pm

      Excelente, Buen aporte, de php 7.2 a 7.1,

      Reply
    16. Ravi on February 20, 2018 10:26 am

      I’m hosting 5 sites using nginx virtual sites .. I want to use PHP 7.1 for just one site only how can I do this ?

      Reply
    17. Heliotrop on February 3, 2018 7:12 am

      How abot switching from php7.1 to php7.2 ?
      phpize and php-config throw error messages (no alternatives)

      Reply
      • Rahul K. on February 3, 2018 10:24 am

        Hi, What error are you getting?

        Reply
      • Eugene on February 17, 2018 2:32 pm

        just install php7.2 and check php version

        Reply
    18. margo on January 25, 2018 3:53 pm

      thanks, you save my time)

      Reply
    19. shamsh on November 30, 2017 7:38 am

      thanxx

      Reply
    20. Nikhil on November 10, 2017 4:51 pm

      Good Work! Thanks for the blog

      Reply
    21. valentin on October 10, 2017 12:29 pm

      Ne fonctionne pas du tout sur un ubuntu 16.04
      Pour se la raconter dans la signature y a du monde, mais pour filer des informations pertinentes la y a plus personne

      Reply
    22. f4b1 on August 6, 2017 10:10 pm

      Nice, thank you very much, I could make the change easily with your tutorial

      Reply
    23. Eduardo on May 17, 2017 6:42 am

      Also Consider:

      From PHP 5.6 => PHP 7.1

      update-alternatives –set phar /usr/bin/phar7.1 && update-alternatives –set phar.phar /usr/bin/phar.phar7.1 && systemctl restart apache2

      From PHP 7.1 => PHP 5.6

      update-alternatives –set phar /usr/bin/phar5.6 && update-alternatives –set phar.phar /usr/bin/phar.phar5.6 && systemctl restart apache2

      Reply
      • Rahul K. on May 18, 2017 3:44 am

        Thanks Eduardo,
        Article updated

        Reply
    24. RunCloud on January 11, 2017 9:37 am

      Great post you have here, Rahul. Did you know that if you manage your server with RunCloud, you can just change the PHP version just a click. We developed RunCloud which will configure your server to the best configuration so you can focus only on things that matters. Check out http://runcloud.io to get your 15 days free trial.

      Reply
      • bvl on July 23, 2018 4:11 pm

        what is your selling point over something like ISPConfig3 which is provided free of charge?

        Reply

    Leave A Reply Cancel Reply

    Recent Posts
    • How to run “npm start” through docker
    • Filesystem Hierarchy Structure (FHS) in Linux
    • How to accept user input in Python
    • What is difference between var, let and const in JavaScript?
    • What is CPU? – Definition, Types and Parts
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.