Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»PHP»How to Change Default PHP Version on Ubuntu

    How to Change Default PHP Version on Ubuntu

    By RahulJanuary 27, 20233 Mins Read

    On your system, if you have installed multiple versions of PHP like PHP 8.2, PHP 7.4 and PHP 5.6. Currently, PHP 7.4 is the default version set for Apache and the command line interface (CLI). Maybe you have any other PHP versions installed on your system. Only one PHP version can be set as default at any time.

    Advertisement

    This tutorial will help you to change the default PHP version for CLI and Apache webserver. This can be achieved with a few sets of commands.

  • You may like: How To Install PHP (8.1, 7.4) on Ubuntu 22.04
  • Switch to PHP 8.2

    Before switching to PHP 8.2, you have to deactivate the previously activated Apache module. The following commands will deactivate the already activated Apache module and then activate PHP 8.2 Apache module. Then the next commands will set PHP 8.2 as the default version for the command line interface (CLI).

    • Apache:
      sudo a2dismod php* 
      sudo a2enmod php8.2 
      sudo systemctl restart apache2 
      
    • Command line interface:
      sudo update-alternatives --set php /usr/bin/php8.2 
      sudo update-alternatives --set phar /usr/bin/phar8.2 
      sudo update-alternatives --set phar.phar /usr/bin/phar.phar8.2 
      sudo update-alternatives --set phpize /usr/bin/phpize8.2 
      sudo update-alternatives --set php-config /usr/bin/php-config8.2 
      

    Note: The phpize8.2 and php-config8.2 command is available in php8.2-dev package. This is more useful for compiling PHP modules using pecl.

    If you want a different version (eg: PHP 8.1 or PHP 8.0) to be configured as default, just replace 8.2 with the required version.

    Switch to PHP 7.4

    Similarly, if you need to configure PHP 7.4 as the default version in your system. The following set of commands will change the default PHP to 7.4 for the Apache web server and command line interface.

    • Apache:
      sudo a2dismod php* 
      sudo a2enmod php7.4 
      sudo systemctl restart apache2 
      
    • Command line interface:
      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 to PHP 5.6

    PHP 5.6 is an outdated version. So if you have an application that required PHP 5.6, consider them to upgrade to last PHP version. The following set of commands will change the default PHP to 5.6 for the Apache web server and command line interface.

    • Apache:
      sudo a2dismod php* 
      sudo a2enmod php5.6 
      sudo systemctl restart apache2 
      
    • Command line interface:
      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 change the default PHP versions on an Ubuntu system. This helps you to change the default PHP version for the Apache web server and the command line interface.

    PHP PHP 5.6 PHP 7.1 PHP 8.2 PHP7.2
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    PHP Arrays: A Beginner’s Guide

    Running Laravel Queue Worker as a Systemd Service

    How to Change PHP Session Timeout

    View 36 Comments

    36 Comments

    1. Vikas J. on January 27, 2023 12:32 pm

      I have Switched from 8.1 => 7.4.
      After following the above steps Switched successfully and I can see a Switched version of PHP in the terminal and info.php.

      But My phpmyadmin is not working after doing this.

      Reply
      • Rahul on January 27, 2023 4:40 pm

        All the latest versions of phpMyAdmin support PHP 7.4. Might be that PHP 7.4 have some missing extensions. Please check the Apache error log files to find out the actual error.

        Reply
        • Vikas J. on January 30, 2023 11:42 am

          Error is returning like this as below.

          Error: [Mon Jan 30 17:11:16.156924 2023] [php7:error] [pid 26344] [client 127.0.0.1:55872] PHP Parse error: syntax error, unexpected ‘static’ (T_STATIC) in /usr/share/php/Symfony/Component/DependencyInjection/ContainerBuilder.php on line 272

          Reply
    2. 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
    3. 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
    4. wallyDef on November 17, 2020 1:49 am

      i am so satisfacted. greetings wally

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

      thanks….

      Reply
    6. 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
    7. stanley on July 17, 2020 11:00 am

      thank you

      Reply
    8. 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
    9. Yılmaz Saraç on May 4, 2020 2:30 pm

      thanks, it helped much!

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

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

      Reply
    11. 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
    12. 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
    13. Tanmay K on July 10, 2018 7:05 am

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

      Reply
    14. 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
    15. Ca Stor on March 29, 2018 6:58 pm

      Thank you my friend.:)

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

      Excelente, Buen aporte, de php 7.2 a 7.1,

      Reply
    17. 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
    18. 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
    19. margo on January 25, 2018 3:53 pm

      thanks, you save my time)

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

      thanxx

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

      Good Work! Thanks for the blog

      Reply
    22. 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
    23. f4b1 on August 6, 2017 10:10 pm

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

      Reply
    24. 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
    25. 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

    Advertisement
    Recent Posts
    • How to List Manually Installed Packages in Ubuntu & Debian
    • 10 Bash Tricks Every Developer Should Know
    • How to Validate Email Address in JavaScript
    • Firewalld: Common Firewall Rules and Commands
    • 12 Apk Commands in Alpine Linux Package Management
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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