How to install and use PHP on macOS via brew - php

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!

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.

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!

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

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 php#7.4
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 php#8.1
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.

Related

Running PHP-server here with Atom on mac

Since updating my mac to Montery my php-server 0.8.0 in atom no longer works and I'm given this error
Listening on http://localhost:8000
Document root is /Applications/MAMP/htdocs
PHP Server could not launch
Have you defined the right path to PHP in your settings? Using PHP
Can anybody help me set this back up? Here are my settings.
v
macOS 12 (”Monterey”) is the first version that no longer ships with PHP included. That said, you will need to install PHP yourself. The PHP documentation lists several ways to install it, the most common way is to use Homebrew:
Enter the following in a terminal window to install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Now you can install the latest PHP version
brew install php
The last couple of PHP 7.x versions are still maintained by the Homebrew team, for even older releases there's a ”tap” (third party repository.)

homebrew - upgrade php broke php 5.6 dependency

I have multiple versions of PHP installed on my localhost using homebrew:
the standard core php package (v7.3.12)
php 5.6 from exolnet/homebrew-deprecated
Now when I upgraded php to 7.3.12 recently it upgraded a dependency that php#5.6 requires and now I am getting this error:
dyld: Library not loaded: /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib
Referenced from: /usr/local/Cellar/php#5.6/5.6.40/bin/./php
Reason: image not found
[1] 83775 abort ./php
And when I navigate to /usr/local/opt/openssl/lib/ I see that I now have the upgraded libcrypto.1.1.dylib installed.
I've tried brew upgrade and reinstalling php#5.6 but no joy.
Does anyone have a suggestion? I really need both versions of php working on my localhost to continue development.
From my experience, trying to manage multiple PHP versions via homebrew is an absolute PITA, and I don't think I've ever got to the point where I can easily have both versions available side by side (until now!). Extensions are also supported, the details of which are provided later on in this answer.
That said, I have found a guide that gives some instructions on how to get this working and it works an absolute treat:
The linked guide references this method in the context of running PHP within Apache, so if that's your use case that's a nice little bonus for you too.
Now, because just posting links as answers on StackOverflow just isn't the done thing, I've detailed the main steps you need to follow in order below. Homebrew uses different paths on Apple Silicon hardware vs Intel because of course it does, so if I've missed any path changes please let me know so I can update my answer.
For anyone following this guide that doesn't yet have the prerequisites installed, ensure you have the Xcode command line tools and Homebrew installed and up to date.
If you want to check Homebrew for any issues, you can run
brew doctor
It's also never a bad idea to ensure that you have OpenSSL installed, especially if starting a fresh on macOS Monterey:
brew install openssl
So, let's get started.
Reset your environment
First, update everything then upgrade:
brew update
brew upgrade
brew cleanup
The reason for this is: "This will actually 'migrate' the core PHP packages (which are the only ones supported), but there's a bunch of symlinks utilized that could cause problems down the road, so after upgrading, we'll remove all PHP packages, to provide a fresh start"
Once you've done that, check what's actually installed:
brew list | grep php
Then remove whatever you find, for example:
brew uninstall --force php56 php56-apcu php56-opcache php56-xdebug
brew uninstall --force php70 php70-apcu php70-opcache php70-xdebug
brew uninstall --force php71 php71-apcu php71-opcache php71-xdebug
brew uninstall --force php72 php72-apcu php72-opcache php72-xdebug
brew cleanup
Double check there's nothing left:
brew list | grep php
and clean up any old configuration files:
Intel:
rm -Rf /usr/local/etc/php/*
Apple Silicon:
rm -Rf /opt/homebrew/etc/php/*
If have the exolnet/deprecated tap installed, you'll need to remove it first using
brew untap exolnet/deprecated
If you don't, you can get some weird conflicts
Add the new Tap and Install
We're then going to tap a different repository. This repository has many versions of PHP pre-built which may or may not work for you, however installation should be much faster as we don't have to compile from source.
brew tap shivammathur/php
You can then install any PHP versions you require:
brew install shivammathur/php/php#7.0
brew install shivammathur/php/php#7.1
brew install shivammathur/php/php#7.2
brew install shivammathur/php/php#7.3
brew install shivammathur/php/php#7.4
brew install shivammathur/php/php#8.0
At the time of writing, this repository has versions 5.6 through 8.1 available for installation
The php.ini files are located in the following directories:
Intel:
/usr/local/etc/php/7.0/php.ini
/usr/local/etc/php/7.1/php.ini
/usr/local/etc/php/7.2/php.ini
/usr/local/etc/php/7.3/php.ini
/usr/local/etc/php/7.4/php.ini
/usr/local/etc/php/8.0/php.ini
Apple Silicon:
/opt/homebrew/etc/php/7.0/php.ini
/opt/homebrew/etc/php/7.1/php.ini
/opt/homebrew/etc/php/7.2/php.ini
/opt/homebrew/etc/php/7.3/php.ini
/opt/homebrew/etc/php/7.4/php.ini
/opt/homebrew/etc/php/8.0/php.ini
Link your desired version
Once you've got here, close and reopen any terminal windows you have open to avoid strange path issues.
Now, these versions are installed, but not linked. To switch to PHP 7.3 for example, run the following command:
brew unlink php && brew link --overwrite --force php#7.3
And then check we have the correct version:
php -v
If we want to switch to PHP 7.4:
brew unlink php && brew link --overwrite --force php#7.4
And then check we have the correct version:
php -v
Easier Switching
If you want a faster way of switching PHP versions, check out the sPHP script from rhukster
You can install this by running:
Intel:
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/adc8c149876bff14a33e3ac351588fdbe8172c07/sphp.sh > /usr/local/bin/sphp
chmod +x /usr/local/bin/sphp
Apple Silicon:
curl -L https://gist.githubusercontent.com/rhukster/f4c04f1bf59e0b74e335ee5d186a98e2/raw/adc8c149876bff14a33e3ac351588fdbe8172c07/sphp.sh > /opt/homebrew/bin/sphp
chmod +x /opt/homebrew/bin/sphp
and execute it using:
sphp 8.0
Of course, with anything like this YMMV, but this should hopefully be a good starting point and actually get you where you need to be and remove some headaches.
What about extensions?
Well I'm glad you asked. For the later versions, pecl does work. However, if you want to install xdebug for 5.6 for example, pecl throws its toys out of its pram.
There is another repository you can tap, specifically for extensions:
brew tap shivammathur/extensions
Once you've done this, it's as simple as running:
brew install xdebug#5.6
brew install xdebug#8.1
The list of available extensions is detailed in the link above. I won't include them here because it's a dynamic list and it'll soon be out of date, but most of them support 5.6 through to 8.1
Wrap up
Having followed this process myself, this is the easiest approach I'm yet to find for managing multiple PHP versions on macOS. Feel free to run brew doctor and brew cleanup at any points during the process, it can't hurt after all and might in fact help with debugging any issues you happen across. That said, my environment was an absolute mess and this tidied it up in about ten minutes. The longest step in this process for me was updating Homebrew itself.

How to load memcached on PHP in Mac OS X Catalina?

I have tried virtually everything I could for the last 3 hours. I just don't seem to get it to load on PHP. I was able to install it via brew install memcached and successfully make run it as a background service via memcached -d. But that's not really what I am looking for.
I am looking at loading it as one of my PHP modules. Running php -m on the terminal does not list memcached on it at all. Neither looking at phpinfo() or print_r(get_loaded_extensions()); on runtime gave me any luck. I am trying to officially access it in my project via something like extension_loaded('memcached'), of course, this returns a falsy value atm.
I have tried solutions like below:
How to install memcached module for php#7.1 on MacOS High Sierra?
https://donatstudios.com/OS-X-Mavericks-Memcached-PHP-Extension-Installation
Can I install the memcached PHP extension with PECL?
Most of the solutions are similar to the above linked. Unfortunately, this isn't working for me because of an issue similar to this:
Unable to use PHPIZE after update to MacOS Mojave
I also get below error when executing a make command:
make: *** No targets specified and no makefile found. Stop.
And the below error when running ./configure
checking for session includes... configure: error: Cannot find
php_session.h
Meanwhile, I also cannot attempt an install via pecl install memcached due to the error below:
configuration download directory "/tmp/pear/install" is not writeable.
Change download_dir config variable to a writeable dir to avoid this
warning
Basically everything just doesn't work. And honestly, I didn't even know how to start writing this question, so I'm just kinda throwing everything in here.
It doesn't help that there are no recent discussions about installing memcached on PHP since Catalina. A lot of resources are just 5 years old and that's not a good sign, a number of them aren't even valid solutions anymore.
I am losing my mind. Why this thing is so complicated to configure is beyond my comprehension. If someone can point me in the right direction that would be great!
So I was able to find a solution after 2 working days. What you should do is move away from using built-in Apache/PHP from macOS and use the ones from Homebrew.
Here are the descriptive step-by-step procedure I did:
1) Unload the built-in Apache.
2) Install a new Apache from Homebrew via brew install httpd and then run the service
3) Install PHP via brew install php
4) Configure httpd.conf from /usr/local/etc/httpd/, including loading Homebrew PHP module, mod_rewrite, setting up DirectoryIndex, ServerName, default Listen port, etc.
4.1) If you are using virtual hosts, set this up on /usr/local/etc/httpd/extra/
5) Configure ~/.bash_profile to use the new PHP version (test via php -v or which php)
6) Install PEAR
7) Install memcached via PEAR
I have compiled a list of links that you can use:
Apache & PHP Installation
https://tecadmin.net/install-apache-macos-homebrew/
https://getgrav.org/blog/macos-catalina-apache-multiple-php-versions
https://gist.github.com/DragonBe/0faebe58deced34744953e3bf6afbec7
Follow brew info php to configure Homebrew PHP to Homebrew Apache
Configure bash profile to use homebrew php by default
PHP --version shown incorrectly on osX
How to use the php that brew installed?
Install PEAR and configure
https://jasonmccreary.me/articles/install-pear-pecl-mac
Install memcached via PEAR
pecl install memcached then follow instructions
Or
How to install memcached module for php#7.1 on MacOS High Sierra?
https://donatstudios.com/OS-X-Mavericks-Memcached-PHP-Extension-Installation
Can I install the memcached PHP extension with PECL?
why don't you try vagrant box. You will get a virtual environment of your choice, install a ubuntu environment in minutes and you can get a LAMP or MEAN / MERN vagrant box ready made.
Steps :
1) Download and install virtualbox.
2) Download and install Vagrant
3) Go to terminal
Check if vagrant is installed or not.
vagrant -v
In order to get a ubuntu box spinning up, run these commands.
vagrant init ubuntu/trusty64
It will download you a ubuntu box in minutes.
vagrant up
vagrant ssh
Have a look at vagrant you will enjoy it.
Also there are whole lot of ready to use boxes
Vagrant boxes
Documentation : [Vagrant Document]

Yosemite / El Capitan php-gd + mcrypt installation

How to install/enable php-gd and mcrypt in Yosemite 10.10 - PHP 5.5.14 build?
note that following steps install newest PHP 5.5.x
To check if you have gd and mcrypt installed use $ php -m to display compiled in modules, continue if you are missing desired modules and please see accepted answer.
GD
So, Apple installed GD (and you're using their build of PHP, which is fine), but did not include support for PNG.
You are definitely missing "full" gd (with png support) module if:
imagecreatefrompng() function is missing.
"Call to undefined function imagecreatefrompng()"
Mcrypt
install brew using accepted answer and omit part with gd installation (unless you need it), and use following steps:
do search - $ brew search mcrypt
pick the version of PHP you want
install - $ brew install php55-mcrypt
confirm using $ php -m | grep mcrypt
Want to switch between PHP versions?
Check out brew-php-switcher and follow instructions.
You are right, Yosemite's built-in PHP comes without PNG and FreeType support.
Update 2015/10 for El Captian: With OS X 10.11 El Capitan PNG support is back, but FreeType is still missing.
Solution
Use the Homebrew package manager to painlessly build and install a complete PHP and replace it in Apache's config. The whole process only takes about ten minutes if you follow the steps below.
A quick (but complete) walk through
(Note 1: I use Homebrew here, a package management system for OS X. If you are acquainted with MacPorts – another package manager – you can acchieve the same results with that system. It is also possible to use my Homebrew solution in parallel to an existing MacPorts installation on your machine.)
(Note 2: If you want to read all the details about the installation process, have a look at the Homebrew basic installation and the Homebrew PHP installation information. But you really won't need that if you follow these steps.)
Now let's go...
First install Xcode from App Store. If you already have it, check App Store again, to make sure you've got the latest version!
Now you need to install the Xcode Command Line Tools. To do so, open a Terminal and enter:
xcode-select --install
The next command will install the Homebrew package manager system:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
The script explains what it will do and then pauses before it does it.
The next command to enter is a kind of system status test:
brew doctor
This checks the basics of the Homebrew installation.
I got a warning "You have MacPorts or Fink installed: /opt/local/bin/port ..." which I ignored successfully. ;)
Now setup the homebrew/dupes tap which has dependencies we need:
brew tap homebrew/dupes
Setup the homebrew/versions tap which also has dependencies we need:
brew tap homebrew/versions
Then run the following command:
brew tap homebrew/homebrew-php
Now you are ready to finally build PHP. For a list of available configuration options you may run one of these:
brew options php55
brew options php56
But I was fine just using the defaults.
To do so enter ONE of these two, depending on your needs:
brew install php55
brew install php56
(This takes a while, please be patient!)
while installing php56 (5.6.x) on Yosemite (10.10.5) there is a bug please see this issue on github. use brew install php56 --without-ldap instead.
If you get an error of type "Cannot find OpenSSL's " you have not installed the Xcode Command Line Tools as I told you in the beginning. ;) Go ahead, install them and re-run that last command.
PHP is built now and the script will end with some details about how to use it:
Open httpd.conf (should be located at /private/etc/apache2/httpd.conf) and enable PHP by adding ONE of these two lines, depending on which PHP version you just installed:
LoadModule php5_module /usr/local/opt/php55/libexec/apache2/libphp5.so
LoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5.so
Don't forget to comment out any existing LoadModule php5_module... line that might be present from Yosemite's own PHP version!
Restart Apache
sudo apachectl restart
Your new php.ini file can be found in: /usr/local/etc/php/5.5/php.ini
Enjoy!
The answer from #Jpsy is good, but there's another option, from the guys from liip, here. This is a PHP package that comes pre-built for Yosemite (older versions works too) but it is just one line of code:
curl -s http://php-osx.liip.ch/install.sh | bash -s 5.5
After that, everything is ready to work as expected. The configuration that cames with that installation is well suited for Symfony 2 development, but it should work just fine with other use cases.
Finally, if you need to use the updated PHP CLI, too, but you don't want to use the PHP version that comes with the OS, then you could also add to your .bash_profile or similar this line of code:
export PATH=/usr/local/php5/bin:$PATH
I don't have enough rep to make a comment, but if you're using OS X Server for Yosemite (Version 4 from the App Store) the file to edit is:
/library/server/web/config/apache2/httpd_server_app.conf
mine looks like this now and confirmed it is working after using php 5.6 from homebrew.
#LoadModule php5_module libexec/apache2/libphp5.so
LoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5.so
Standard Apache coming with Yosemite the file to update after
sudo brew install php55
is in /etc/apache2/httpd.conf
Note that location of php.ini is also changed. The standard one coming with Yosemite is in /etc/php.ini, the one with homebrewed version is in /usr/local/etc/php/5.5/php.ini

How do I upgrade PHP in Mac OS X?

I feel this is an awfully obtuse question to ask, but strangely, this problem is poorly documented.
I would like to upgrade PHP, but there are several problems:
There is no built-in package manager. MacPorts doesn't recognize php as an installed package because it didn't install PHP itself.
Running locate php indicates there are probably many dependencies.
I don't know HOW php was installed, as it was included with the OS, so I don't know whether I should install from source or download binaries. I also don't know the proper way to uninstall the previous version without breaking dependencies.
I am running on Leopard. I have a feeling Apple doesn't want you to upgrade. Would buying Snow Leopard and upgrade solve this problem (and future ones like it)?
You may want to check out Marc Liyanage's PHP package. It comes in a nice Mac OS X installer package that you can double-click. He keeps it pretty up to date.
http://php-osx.liip.ch/
Also, although upgrading to Snow Leopard won't help you do PHP updates in the future, it will probably give you a newer version of PHP. I'm running OS X 10.6.2 and it has PHP 5.3.0.
I use this: https://github.com/Homebrew/homebrew-php
The command is:
$ xcode-select --install
$ brew tap homebrew/dupes
$ brew tap homebrew/versions
$ brew tap homebrew/homebrew-php
$ brew options php56
$ brew install php56
Then config in your .bash_profile or .bashrc
# Homebrew PHP CLI
export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"
I think one simple way to do it, is:
1 - Check you where is your current PHP:
$ which php
$ /usr/local/bin/php
You see? Usually, our commands that we run is a link in /usr/local/bin so...
2 - Unlink this current link of PHP
unlink /usr/local/bin/php
If you prefere, before unlink it, check the path and then remove php files (do ls -al /usr/local/bin | grep php and then rm -rf into desired path)
3 - Install PHP 7.1
curl -s http://php-osx.liip.ch/install.sh | bash -s 7.1
4 - Create new link (using php 7.1 bin that you have installed)
ln /usr/local/php5-7.1.9-20170914-100859/bin/php /usr/local/bin/php
Like I said, its a simple way I think.
There is no built-in package manager. MacPorts doesn't recognize php as an installed package because it didn't install PHP itself.
You could still install it with MacPorts. sudo port install php52 (or whichever version you want) will install PHP.
It won't overwrite the Apple-supplied version. It'll install it under /opt/local. You can add /opt/local to the beginning of your $PATH, and use the MacPorts version in your Apache config.
Option #1
As recommended here, this site provides a convenient, up-to-date one liner.
This doesn't overwrite the base version of PHP on your system, but instead installs it cleanly in /usr/local/php5.
Option #2
My preferred method is to just install via Homebrew.
Before I go on, I have the latest version (v5.0.15) of OS X Server (yes, horrible, I know...however, the web server seems to work A-OK). I searched high and low for days trying to update (or at least get Apache to point to) a new version of PHP. My mcrypt did not work, along with other extensions and I installed and reinstalled PHP countless times from http://php-osx.liip.ch/ and other tutorials until I finally noticed a tid-bit of information written in a comment in one of the many different .conf files OS X Server keeps which was that OS X Server loads it's own custom .conf file before it loads the Apache httpd.conf (located at /etc/apache2/httpd.conf). The server file is located:
/Library/Server/Web/Config/apache2/httpd_server_app.conf
When you open this file, you have to comment out this line like so:
#LoadModule php5_module libexec/apache2/libphp5.so
Then add in the correct path (which should already be installed if you have installed via the http://php-osx.liip.ch/ link):
LoadModule php5_module /usr/local/php5/libphp5.so
After this modification, my PHP finally loaded the correct PHP installation. That being said, if things go wonky, it may be because OS X is made to work off the native installation of PHP at the time of OS X installation. To revert, just undo the change above.
Anyway, hopefully this is helpful for anyone else spending countless hours on this.
Upgrading to Snow Leopard won't solve the your primary problem of keeping PHP up to date. Apple doesn't always keep the third party software that it bundles up to date with OS updates. And relying on Apple to get you the bug fix / security update you need is asking for trouble.
Additionally, I would recommend installing through MacPorts (and doing the config necessary to use it instead of Apple's PHP) rather than try to upgrade the Apple supplied PHP in place. Anything you do to /usr/bin risks being overwritten by some future Apple update.
Saving on keystrokes, this worked on MacOS Sierra:
$ brew install homebrew/php/php71
$ /usr/local/opt/php71/bin/php -v
PHP 7.1.4 (cli) (built: Apr 14 2017 15:02:16) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
Check your current php version in terminal with the following command,
$ php -v
You see current php version in terminal, and next command run in terminal if you want to upgrade your php version with php concat with version liked as,
$ brew install homebrew/php/php71
Please restart terminal if you finished php version upgrade installed and run the command.
$ php -v
Now you see the current php version in terminal....thank
Use this Command:
curl -s http://php-osx.liip.ch/install.sh | bash -s 7.0
You can use curl to update php version.
curl -s http://php-osx.liip.ch/install.sh | bash -s 7.3
Last Step:
export PATH=/usr/local/php5/bin:$PATH
Check the upgraded version
php -v
best way to upgrade is
compile it from source
see this tutorial that may be helful for you
http://www.computersnyou.com/2012/09/how-to-upgrade-php-in-mac-osx-compiling.html
to upgrade php7 to latest stable version brew upgrade php7
or for php5.X to latest stable version
brew upgrade php56
use brew list to check installed version

Categories