How to Install PHP 7 on MacOS

Download and Install PHP on macOS

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

Open a terminal and run below commands

For PHP 7.2

curl -s | bash -s 7.2

For PHP 7.1

curl -s | bash -s 7.1

For PHP 5.6 – Running with OSX 10.11 El Capitan or lower versions.

curl -s | bash -s 5.6

Verify PHP Installation

The PHP versions for macOS are maintained by php-osx and doesn’t overwrite the current php binaries installed on your system. The installs everything in /usr/local/php5. The new php binary is therefore in /usr/local/php5/bin/php.

export PATH=/usr/local/php5/bin:$PATH

To verify correct version of PHP is installed on your system, Execute the following command.

php -v

PHP 7.2.2 (cli) (built: Feb 1 2018 13:23:34) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.2, Copyright (c) 1999-2018, by Zend Technologies
with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans



Update: Để lần sau mỗi lần run không phải chạy lại lệnh EXPORT thì làm như sau

touch ~/.bash_profile; open ~/.bash_profile

Sau khi chạy lệnh thì thêm đoạn mã export ở trên vào dưới biến môi trường sẵn có là được.

Lệnh cần thêm vào file vừa mở ra (nếu ai quên)

export PATH=/usr/local/php5/bin:$PATH



Leave a Reply

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