Maxim Krušina
Maxim Krušina

Reputation: 799

How to install and use PHP on macOS via brew

it seems like a little bit stupid question, but Google for one hour and cannot find it:

How to install php on macOS via brew and use in in shell.

Install is simple (if you have brew already installed):

brew install php

But after installation, when i use php -v i still see default php on mac os and not the brew's one. Shoud I Add brew php binary into PATH, bash profile, or what?

Thank you!

Upvotes: 3

Views: 5212

Answers (4)

hassan
hassan

Reputation: 166

Step 1: Install Homebrew

The first step is to install Homebrew and this is a tool (package manager for macOS) that will allow us to install easily PHP and basically any other package/tools.

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

Step 2: Install PHP

To install PHP we can use the command below. The first command will automatically install the latest version of PHP which is at the current moment of writing this post version 8.

brew install php

If other versions may be preferred we can specify the PHP version and the code will look like the following.

brew install [email protected]

Step 3: The php.ini Config

The default PHP configuration should be sufficient to get started but if there are any other configurations, we can change them through the php.ini located on this path. Do note the PHP version number which in this case is version 8.

/usr/local/etc/php/8.1/php.ini

Step 4: Check if PHP is running

To check if PHP is already running we can make use of the brew services command. First, we can list the services that we have installed.

brew services list

If PHP is not started we can then run the command below to start the service in the background.

brew services start [email protected]

Step 5: Checking PHP Version

Lastly to check the PHP version do run:

php -v

By now you will have PHP running on the background process and every time you logged in to the system it will start by default. Thanks for reading and have a good try.

Upvotes: 1

Casey McMullen
Casey McMullen

Reputation: 71

On April 1, 2018 Homebrew discontinued the Homebrew/php tap and went with a core install approach, which means many of the extensions now must be installed with PECL. I have written a two part blog series to help with installing Apache and PHP w/ PECL on later versions of MacOS. You can find it at the link below, I hope it helps!

https://medium.com/@crmcmullen/how-to-install-php-on-macos-10-13-high-sierra-and-10-14-mojave-using-homebrew-and-pecl-ef2276db3d62

Upvotes: 0

Maxim Krušina
Maxim Krušina

Reputation: 799

So in my case, the trouble was with some access permissions on specific folders on my Mac OS, so just sharing what I learned: if it's not working, carefully examile log / error feed, there can be a hint for you!

Upvotes: 0

Mark Setchell
Mark Setchell

Reputation: 207445

homebrew creates symbolic links in /usr/local/bin to pretty much every binary it installs. So the answer to your question is to add that to your path in your login profile. That will probably be $HOME/.profile and you can add a line like:

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

Then it will be set each time you login or open a new Terminal.

Upvotes: 6

Related Questions