Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»MacOS»How to Install PHP (8.1, 7.4 or 5.6) on MacOS

    How to Install PHP (8.1, 7.4 or 5.6) on MacOS

    By RahulSeptember 26, 20223 Mins Read

    PHP stands for HyperText Preprocessor and is a server-side programming language. Originally it was developed for web development but now is also used as a general-purpose language. It was created by Rasmus Lerdorf in 1994.

    Advertisement

    The PHP versions for macOS is maintained in third party repository that can be add to system with brew tap. It also allows you to install and use multiple PHP versions on a single macOS system.

    This tutorial will help you to install PHP on macOS.

    Prerequisites

    Before starting the installation of PHP using this tutorial you must have the following prerequisites

    • Terminal: You must have Mac Terminal access and little knowledge about working with the terminal application. Ao login to your Mac system and open terminal
    • Homebrew: Homebrew is a popular package manager for the Mac operating systems. It is useful for installing most open-source software like Node. You can install Homebrew with the following command.
      /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 
      

    .

    How to Install PHP on macOS

    The latest version of macOS Sierra ships with PHP 5.6 and OSX 10.11 El Capitan with PHP 5.5, and OSX 10.8 Mountain Lion ships with PHP version 5.3. The latest version of PHP 8.1 is available to install. The steps below help you install PHP 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, or 5.6 on the macOS system.

    1. First, we need to add the shivammathur/php tap to homebrew.
      brew tap shivammathur/php
      
    2. Installing PHP: Use the below command to install PHP 8.1 on a macOS system. To install a different PHP version just replace 8.1 with required version.
      brew install shivammathur/php/[email protected] 
      
    3. Configure PHP 8.1 as default:
      brew link --overwrite --force shivammathur/php/[email protected] 
      
    4. Once the installation is finished, restart the shell and check the active PHP version.
      php -v 
      
      #Output 
      PHP 8.1.6 (cli) (built: May 21 2022 03:46:22) (NTS)
      Copyright (c) The PHP Group
      Zend Engine v4.1.6, Copyright (c) Zend Technologies
          with Zend OPcache v8.1.6, Copyright (c), by Zend Technologies
      

    Configure PHP with Apache

    Edit the Apache configuration file and a text editor:

    vim /usr/local/etc/httpd/httpd.conf 
    

    Add the following snippet to the file:

    1
    2
    3
    4
    5
        LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so
     
        <FilesMatch \.php$>
            SetHandler application/x-httpd-php
        </FilesMatch>

    You can also change the directory index:

    1
        DirectoryIndex index.php index.html

    Save the file and close it.

    Now restart the Apache service to apply changes:

    apachectl restart 
    

    Now create a phpinfo() function file and access in the browser.

    Installing PHP on MacOS
    PHP configuration with Apache

    Upgrade PHP version

    You can upgrade your PHP version to the latest patch release using the brew upgrade command.

    For example, to upgrade PHP 8.1:

    brew upgrade shivammathur/php/[email protected] 
    

    Switch Active PHP Version

    If you have installed multiple PHP versions on your system, you can switch to a new version anytime.

    For example, to switch to PHP 7.4:

    brew link --overwrite --force shivammathur/php/[email protected] 
    

    Make sure you already have installed the PHP 7.4 version on your system.

    Conclusion

    This guide helped you to install PHP on the macOS system. You can install multiple PHP versions on a single system and switch between them anytime.

    macOS PHP 7 PHP 7.1 PHP 7.2 PHP 7.3
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Install Postgres on MacOS

    How To Install PostgreSQL on MacOS

    How to Change Terminal Font Size in macOS

    How to Install LibreOffice on Mac

    How to Install LibreOffice on macOS

    View 12 Comments

    12 Comments

    1. Ed Green on December 28, 2020 12:21 am

      And how to install php 8 without homebrew?

      Reply
    2. Thomas on November 10, 2020 10:54 am

      how do i delete it?

      Reply
    3. Ray Winestone on May 9, 2020 9:45 am

      I have installed php as detailed above for a Mac.

      The commmand php is working but the php info page is not

      Reply
    4. peterdv3 on January 23, 2020 10:07 pm

      I followed the download link for 7.3 and it installed 7.3.8, no problems

      Reply
    5. Alan on September 21, 2019 11:14 am

      I dunno if its me but mine auto reverts back to PHP7.1 despite I have installed 7.3. Anyone got an idea why or if they’re having the same issue

      Reply
      • Daniel on September 24, 2019 7:51 pm

        Same here

        Reply
      • Tony G on February 20, 2020 4:31 pm

        Same here.

        Reply
    6. Allan on September 18, 2019 8:12 pm

      Thanks. Absolutely helpful.

      Reply
    7. Juanmi on June 10, 2019 5:24 pm

      very helpful, thank you so much

      Reply
    8. nacim on December 21, 2018 7:21 pm

      Very good work ! you saved me 🙂 i was looking for how to update my mac from 7.1 to 7.2 , since 3 days , by chance i found your post i solved my problem thanks your tuto in 2 secondes ! , i thank you 🙂

      Reply
      • Tony G on February 20, 2020 4:34 pm

        How did you get PHP to default to 7.2 on your MAC? I just installed php 7.3 but PHP -version shows version 7.1 still. I can’t find how to make it default to version 7.3 – if you know please tell me because this blog entry doesn’t have the answer. – Thanks!

        Reply
    9. Website Design Oakville on November 9, 2018 8:47 am

      Hello there, I found your blog via Google search, and found
      that it’s informative truly. I’ll appreciate if you continue
      this in future. Lots of individuals will be benefited from the given information
      you have provided.
      Cheers!

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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