Hướng dẫn install php 7.3 mac

Dạo gần đây mình cần phải chuyển đổi liên tục giữa các phiên bản PHP trên máy của mình để làm việc.

Nếu các bạn đã từng gặp vấn đề như mình thì ít nhiều các bạn cũng quen với câu lệnh này:

Yeah, mình cũng làm như vậy. Nhưng mỗi lần chuyển đi chuyển lại, mình thấy lười quá.

Trong cái khó ló cái khôn. Mình dạo hết các diễn đàn tây ta các kiểu, cuối cùng mình tìm được một các khá hay.

Có thể cùng lúc chạy nhiều phiên bản PHP như thế này:

1
2
3
4
5
6
7
8
9
10
11
$ php -v
PHP 8.1.1 (cli) (built: Dec 15 2021 09:38:20) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.1, Copyright (c) Zend Technologies
with Zend OPcache v8.1.1, Copyright (c), by Zend Technologies

$ php74 -v
PHP 7.4.27 (cli) (built: Dec 17 2021 00:11:47) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies

Hướng dẫn install php 7.3 mac

Các bạn có thể thấy, cùng lúc mình có thể dùng lệnh php để chạy cho bản PHP 8.1.1 và php74 để chạy cho bản PHP 7.4.27

Ngoài ra mình hoàn toàn có thể cài thêm PHP 7.1 và dùng lệnh php71

Bây giờ mình sẽ hướng dẫn các bạn cách thực hiện.

Cài đặt các công cụ hỗ trợ

Cài đặt công cụ XCode Command Line

1
xcode-select --install

Cài đặt Homebrew

Homebrew là một trình quản lý gói cho macOS. Nó giống như trình quản lý gói apt trên Ubuntu.

1
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Kiểm tra lại xem đã cài đặt thành công Homebrew hay chưa bằng lệnh sau:

1
2
$ brew --version
Homebrew 3.3.10

Ngoài ra bạn có thể chạy lệnh brew doctor để xem mọi thứ đã ổn hay chưa.

Cài đặt đa phiên bản PHP

Thời điểm hiện tại, Homebrew chỉ duy trì và hỗ trợ các phiên bản PHP 7.2, 7.3 và 7.4

Do đó, để cài đặt được các phiên bản PHP cũ hơn PHP 5.6, 7.0 & 7.1 thì chúng ta cần phải chạy lệnh sau:

1
brew tap exolnet/homebrew-deprecated

Bây giờ bạn có thể cài đặt tất cả các phiên bản PHP có sẵn bằng các lệnh

Điều này sẽ mất một ít thời gian, cho nên bạn có thể đi pha một cốc cafe ☕️ rồi quay lại!

Chuyển đổi giữa các phiên bản PHP

Sau khi cài đặt, bạn có thể chuyển đổi giữa các phiên bản PHP bằng cách ‘link’ và ‘unlink’ trong brew:

Year, bây giờ bạn có thể chuyển đổi qua lại giữa các phiên bản PHP rồi đúng không. Tiếp theo mình sẽ chỉ tiếp cho các bạn một thủ thuật nhỏ để không cần phải chuyển đổi qua lại giữa các phiên bản nữa, mà có thể dùng trực tiếp như mình đã nêu ở phần đầu luôn.

Để thực hiện các bạn làm theo các bước sau:

Cấu hình đa phiên bản PHP

Điều chỉnh tên file php

Cài đặt và cấu hình ZSH

Nếu chưa cài đặt zsh thì trước tiên bạn cần cài đặt bằng lệnh sau:

1
brew install zsh

Tiếp theo, bạn cần cài đặt thêm oh-my-zsh như sau:

1
sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Cuối cùng bạn hãy mở file ~/.zshrc và thêm vào nội dung sau:

Brew's PHP 7.1, 7.2 and 7.3 all have INTL enabled by default.

Nội dung chính

  • Installing PHP through Brew
  • Possible issues
  • Installing additional extensions
  • How do I enable Intl extension on Mac?
  • How do I enable Intl extension in PHP?
  • How do I install PHP extensions?
  • Can you install PHP on Mac?


Most probably, you're just using your Mac OS' bundles version of PHP.

Run

ls -l $(which php)

to find out where the current PHP binary is located and whether it is symlinked to a Brew installation or not. In my case, for example:

lrwxr-xr-x 1 27 May 23 16:30 /usr/local/bin/php -> ../Cellar/php/7.3.5/bin/php

Meaning that my php is linked to Brew's 7.3.5 version.

If you are NOT using Brew's PHP, you'll see something like

-rwxr-xr-x 1 11169664 Mar 21 07:09 /usr/bin/php

Installing PHP through Brew

Find out whether you've already installed PHP:

brew list | grep php

If there is any output, and your version of PHP is present, go to step 2, or use step 1 to update PHP to the latest version.

1. Install Homebrew's PHP

brew install 

(or 7.2, 7.1). If Brew complains about not being able to find a formula, you might have messed with taps. Instead of , you could try to supply the full path to the current php formula:

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/master/Formula/php.rb

Still not working, check whether you are running a recent version of Homebrew brew --version.

Homebrew 2.1.3-31-geaf2370
Homebrew/homebrew-core (git revision fd1ef; last commit 2019-05-25)
Homebrew/homebrew-cask (git revision 16d50; last commit 2019-05-26)

2. Link Homebrew's PHP

Now, to have php 'in your path', there are two options.

a) Either homebrew's version must be linked from its install location (/usr/local/bin/Cellar/php....) to a directory in your path (e.g., /usr/local/bin). To do this, run:

brew link --force 

If you are not able to link, this is typically caused by set permissions or System Integrity Protection. In the first case, try sudo chown "$USER":admin /usr/local/bin/php.

b) Or, add the /usr/local/opt/ directory (opt-prefix) to your $PATH variable. E.g., for Bash:

echo 'export PATH="/usr/local/opt//bin:/usr/local/opt//sbin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

3. Validate installation

ls -l $(which php)

should show that php is linked to a Homebrew PHP installation in /usr/local/bin/Cellar.

php -v

should show the recently installed version of PHP. Try to restart your terminal if that's not the case.

php -i | grep -i intl

should show some information about the current install of intl.

If you're using webservers and/or PHP FPM, this is the time to restart those services. (Or restart your system, if you don't know how to do that and cannot figure out).

Possible issues

If you still get warnings about missing extensions (Unable to load dynamic library, etc.), then your php.ini is messed up.

Find the current location of php's ini

$ php -i | grep \.ini

Configuration File (php.ini) Path => /usr/local/etc/php/7.3
Loaded Configuration File => /usr/local/etc/php/7.3/php.ini
Scan this dir for additional .ini files => /usr/local/etc/php/7.3/conf.d
Additional .ini files parsed => /usr/local/etc/php/7.3/conf.d/ext-opcache.ini
....

Edit /usr/local/etc/php/7.3/php.ini and find the offending extension load (e.g., extension="myext.so"). Comment out those that cannot be found.

Homebrew permissions

Some argue that it's a good idea to chown /usr/local.

sudo chown -R "$USER":admin /usr/local

This will make installing things here, by hand and through Homebrew, a lot easier, but also a bit less secure too, since non-root processes are now allowed to write here too.

Your web-application is using a different version of PHP.

Make sure that it doesn't... The configuration of this depends on the used webserver. A first step would be to output the current PHP configuration in your web-application with <?php phpinfo();.

This explains steps for Apache.

Another way to get a webserver + PHP stack running quicly is using Laravel Valet.

Installing additional extensions

To install additional PHP extensions, use PEAR.

pear -V should output the current PEAR and PHP version.

PEAR Version: 1.10.9
PHP Version: 7.3.5
Zend Engine Version: 3.3.5

Now, to install an extension, for example, PHP's yaml extension:

pear install yaml

How do I enable Intl extension on Mac?

Re: Install php_extension intl into Mac OS X Server.

(1) Use "port" to install php5-intl % sudo port install php5-intl..

(2) Copy intl.so into active php extensions directory % sudo cp /opt/local/lib/php/extensions/no-debug-non-zts-20090626/intl.so /usr/lib/php/extensions/no-debug-non-zts-20090626..

How do I enable Intl extension in PHP?

The intl extension is enabled by default on PHP versions above 7.2. If you're noticing any errors on your site related to this extension, please Contact Support for further assistance. If your site is still running PHP 5.6, you must enable this extension manually by making a small addition to your phprc file.

How do I install PHP extensions?

How To Compile And Install PHP Extensions From Source.

Install the PHP development package. On Ubuntu/debian, you can use apt-get, it's a piece of cake. ... .

Download & unzip the PHP5 source code. ... .

Prepare the extension (phpize) ... .

Configure & Make the extension. ... .

Move the extension. ... .

Edit your PHP. ... .

Restart your php..

Can you install PHP on Mac?

PHP is bundled with macOS since macOS X (10.0. 0) prior to macOS Monterey (12.0. 0). Compiling is similar to the Unix installation guide.