This article describes how to install various versions of PHP on a Mac computer using HomeBrew. Mainly to solve the low version of the installation, as well as the use of multiple versions of the problem. M1 Mac (Apple Silicon) is also supported.
PHP is now up to version 8.0, but versions 7.x and even 5.6 are still widely used in corporate production environments. For backend developers, it is best to install the same version on their own computer as in the company’s production environment to ensure that the code fits during development.
Installing a lower version of PHP is somewhat difficult for those of you who don’t know how to compile and install it. Even for some middle and senior engineers, it is not easy. This is mainly due to the versioning of some underlying packages that different versions of PHP rely on for compilation and installation, as well as the mismatching of some packages on the Mac OS itself. Most people spend an hour or two, or even half a day, installing PHP. The main purpose of this article is to get the development environment right and fast. It doesn’t discuss native build and install, or how to do it better.
If you’re an old engineer,
Brew tap Shivammathur/PHP brew search PHP brew install shivammathur/ PHP /[email protected] //Copy the code
If you’re new to this,
First, the Mac OS itself comes with PHP, and versions of PHP vary from Mac OS to Mac OS. If your own is enough, use your own. It is still recommended that you install a new one for easy management later.
The first step is to install HomeBrew, a tool that makes installing other software faster and cleaner later. Brew.sh /index\_zh-c… . You can search for installation. You can also use the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Copy the code
HomeBrew acts as a “software manager” for technical software, including common technical tools such as Mysql, NodeJS, Redis, etc. ** It is recommended to learn how to use this tool and install other software. Get twice the result with half the effort.
After the installation is complete. The relevant information is displayed. ==> Installation successful!
You can go very likely in this one step not go down, have no relation, search the source of tsinghua that changes home or big source in science.
Second, search for available PHP versions. If you have the version you need, just install it.
brew search php
Copy the code
If the installation fails, or you get an error with the keyword ICu4c, give up and try the following. The ICU4c error is resolvable, but cumbersome.
Third, if you don’t have the version you need, add another repository.
To explain a bit, you can think of the software manager as having a main warehouse, which contains only the latest and most commonly used items. And if you want some old goods, you need to transfer goods from other places. So additional sub-warehouses need to be added.
Add old PHP repository:
brew tap shivammathur/php
Copy the code
After the installation is complete, search again
brew search php
Copy the code
As you can see on the far right, there are many versions.
Choose the version you prefer.
The brew install shivammathur/PHP/[email protected]Copy the code
Installing multiple Versions
Just select another version and install it. For example, if I have installed 8.0, I will install 7.3.
The brew install shivammathur/PHP/[email protected]Copy the code
The installation process looks like this
Prompt after installation: Add related environment variables as prompted. Similar to the following code.
[email protected] is keg-only, which means it was not symlinked into /usr/local, Because this is an alternate version of another formula. If you need to have [email protected] first in your PATH run: Echo 'export PATH="/usr/local/opt/[email protected]/bin:$PATH"' >> ~/. ZSHRC echo 'export PATH="/usr/local/opt/[email protected]/sbin:$PATH"' >> ~/. ZSHRC For compilers to find [email protected] you may need to set: Export LDFLAGS=" -l /usr/local/opt/[email protected]/lib" export CPPFLAGS="-I/usr/local/opt/[email protected]/include" To have launchd start Exolnet /deprecated/[email protected] now and restart at login: Brew services start exolnet/deprecated/[email protected] Or, if you don't want/need a background service you can just run: php-fpmCopy the code
PHP -v has a look at the version. If the version hasn’t changed, restart your terminal.
How to Switch Versions
brew install brew-php-switcher
Copy the code
Once installed, I am now at 7.1 and I will switch to 7.3
The brew - PHP - switcher 7.3Copy the code
Add environment variables as prompted after switching. Restart the terminal.
Other:
Where is the PHpini file?
php --ini
Copy the code
or
➜ PHP - I | grep 'the php.ini' Configuration File (the php.ini) Path = > / usr/local/etc/PHP / 7.0 the Loaded Configuration File = > / usr/local/etc/PHP / 7.0 / PHP iniCopy the code
How do I restart PHP?
Brew Services restart [email protected] //start stop
Error message:
Solution: Install the xcode-select tool as prompted. Note that this is not installing Xcode, so don’t be afraid.
Open terminal and run xcode-select –install
Conclusion: research technology when the research technology, the pursuit of efficiency when the pursuit of efficiency.
The end of the article, for reference only, welcome to discuss.
O programmers understand technology | see life
PHP, Go back-end programmer learning path. The focus is on sharing back-end technologies, aiming to explain real-world programming. Climb the slope of enlightenment together.