I made the seemingly bad decision to update to macOS Monterey 12.3.1, and am having a hell of a time trying to get PHP working on it again. From what I've seen, Apple has decided that PHP is deprecated - at least from their machines, and they removed every php version and setting that I had set up for all my Laravel and Wordpress projects!
I tried reinstalling PHP using homebrew but I got a lot of errors about certain packages not being able to be downloaded due to authorization denial?
So then I attempted to uninstall and reinstall Homebrew, and I did the following:
Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Add the following to zshrc
eval "$(/opt/homebrew/bin/brew shellenv)"
Test brew version to see if it's working
brew --version
Run brew doctor to make sure everything is configured correctly
brew doctor
I also saw that it was likely that there would be libraries missing and that i should reinstall open ssl so i did that as well:
brew install openssl
Then for PHP I ran:
brew tap shivammathur/php
and then
brew install shivammathur/php/php#7.4
brew install shivammathur/php/php#8.0
and I even restarted the php services just in case
brew services restart shivammathur/php/php#7.4
brew services restart shivammathur/php/php#8.0
Then I ran
php -v
and I got the following error:
zsh: command not found: php
and which php says php not found
what the heck!
I then tried to unlink php and force a link to 7.4:
brew unlink php && brew link --overwrite --force php#7.4
and I got the following brew error:
Error: No such keg: /usr/local/Cellar/php
I tried digging into fixing this keg issue but i haven't been able to figure it out, do you have any suggestions on next steps
Check that Homebrew's bin directory i.e. $(brew --prefix)/bin is in your PATH.
If you want to link PHP 7.4, run
brew link --overwrite --force php#7.4
If you get an error mentioning that it is already linked, you can relink it by unlinking it first and linking it again to be sure. Make sure you unlink the correct version.
brew unlink php#7.4
brew link --overwrite --force php#7.4
I'm trying to set up my linux environment to use PHP for my job. I'll spare you most of the details, but one extension I need is ImageMagick. I'm using Homebrew to help me install stuff. I'm using PHP 7.4. The commands I've run based on what I've read are as follows:
brew install php#7.4
brew install imagemagick
pecl install imagcik
However, when running the last command, I get this error, saying pcre2.h not found:
error image
I've done a lot of googling and haven't been able to find anything that helps with this specific issue. I don't know what I'm doing wrong. Everything is currently done through homebrew, but if it helps my linux distro is OpenSUSE Tumbleweed. Thanks!
Assuming that you did install pcre2 (if you didn't, run the command below and try again).
$ brew install pcre2
If that does not work, the issue is probably that the file exists, but in the wrong spot. A quick Google search shows that creating a symlink can resolve this issue.
$ ln -s <path to package> /home/linuxbrew/.linuxbrew/Cellar/php#7.4/7.4.27/include/php/ext/pcre/pcre2.h
If you're unsure where the package is currently installed. You should be able to find it with this command.
$ brew info pcre2
This is something i've met while trying to install PHP-GMP extension.
What i was trying to do is:
Downloaded gmp with brew using brew install autoconf gmp
Downloaded PHP (i'm using version 7.3.24) from source to extract the GMP extension
Copied Gmp directory to /Applications/MAMP/bin/php/php7.3.24/include/php/ext
Entered that dir and launched phpize
Launched `./configure --with-php-config=/Applications/MAMP/bin/php/php7.3.24/bin/php-config
As soon as i hit enter it starts doing it's stuff until it says:
configure: error: GNU MP Library version 4.2 or greater required.
Once i managed to solve this somehow but i really don't know how i did it. I can only remember of downloading the gmp .tar from the official website https://gmplib.org/#DOWNLOAD but then nothing works.
Let me say that i'm a total noob with those ./configure, make and make install stuff. I don't even know what is going on so all i did was random launching hoping something good could happen:
What i've tried after downloading the official .tar is entering the folder and go with:
./configure
make && make check && make install
I've also tried to do:
export CPPFLAGS="-I/usr/local/Cellar/gmp/include"
export LDFLAGS="-L/usr/local/Cellar/gmp/lib"
as suggested here but nothing changed. I even tried to add --prefix=/usr/local/Cellar to the ./configure command but no luck.
This all story is related to this other question of me where i was trying to go on with the issue when the ./configure was still working. Now it's like i went backwards.
Sidenote: i guess it all changed when i fired brew doctor, and brew cleanup. It must have destroyed something i made that brought me back to this state
Update 23/08/2022:
As i had to go throughout all over this again i think i have some more hint to give you for future reference, here's the list:
Install GMP via homebrew (if you have an old version of homebrew or you migrated datas between Intel Mac and M1 Mac then you need to UNINSTALL homebrew and INSTALL it all over again with all the libraries)
brew install autoconf gmp
Downloading php from source is necessary, so that you can grab the /ext/gmp directory and copy inside your current PHP version folder (for me it is currently Applications/MAMP/bin/php/php8.1.0/include/php/ext/)
You need to enter inside the gmp folder that you just copied inside Applications/MAMP/bin/php/php8.1.0/include/php/ext/gmp
The command i've launched that didn't give any errors about missing GMP was:
./configure
--with-php-config=/Applications/MAMP/bin/php/php8.1.0/bin/php-config CXXFLAGS="-I/opt/homebrew/Cellar/gmp/6.2.1_1/include
CPPFLAGS="-I/opt/homebrew/Cellar/gmp/6.2.1_1/include
LDFLAGS="-L/opt/homebrew/Cellar/gmp/6.2.1_1/lib"
launch make
launch make install
IMPORTANT NOTE: M1 is arm64 architecture and if you've installed brew correctly you should have a folder /opt/homebrew/Cellar/. Instead Intel is x86_64 and the brew folder was /usr/local/Cellar/
Old answer
Ok this was quicker then i thought but i should leave it here as a reference for everyone else which will hit the same issue.
TLDR;
You need to reinstall brew for the new architecture using this arch -x86_64 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" and then install packages with this arch -x86_64 brew install <package>.
Credits to emonz
Long version answer:
This issue all starts with brew, i don't even know why it wasn't giving me hints about this issue until just today. When i launched brew install gmp it printed this error:
which lead me to this answer from emonz. After i finally reinstalled brew i could finally get the ./configure command to work using additional args:
./configure --with-php-config=/Applications/MAMP/bin/php/php7.3.27/bin/php-config CC="gcc -arch x86_64" CXX="g++ -arch x86_64"
So basically it is all related to the new M1 chip i guess. I still don't know what i did in the past to solve this issue w/o upgrading brew to the new arch but somehow i did it. I'll leave the question and this answer more like a guide for future comers and give them hints of what they can try to solve the issue, i wish nobody go past this pain ever again.
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.
PHP was not working for me as I was encountering this libpng issue, so I reinstalled a new version with Homebrew.
However, I'm getting a similar error with libjpeg this time:
$ php -v
dyld: Library not loaded: /usr/local/lib/libjpeg.8.dylib
Referenced from: /usr/local/bin/php
Reason: image not found
Trace/BPT trap: 5
Any suggestions?
As proposed by #jirson-tavera in the homebrew-php repo issue, this can be fixed without overriding your brew installation:
wget -c http://www.ijg.org/files/jpegsrc.v8d.tar.gz
tar xzf jpegsrc.v8d.tar.gz
cd jpeg-8d
./configure
make
cp ./.libs/libjpeg.8.dylib /usr/local/opt/jpeg/lib
Even though the solution in the accepted answer works, it's not the right way to fix the problem. It violates brew's metadata integrity.
Problem
The issue is that Homebrew's jpeg formula has been upgraded to v9 but the existing "bottled" PHP formula is still built and linked against the previous version, v8, which is no longer exists on your system.
You have a few options to fix the issue.
1. Recompile phpxx formula from source (highly recommended)
Uninstall your php formula, and rebuild it from the source instead of using the bottled version. This way, php will use and link against the currently installed version of jpeg. Assuming that you're dealing with php71:
brew reinstall php71 --build-from-source
2. Downgrade jpeg formula the right way (preferred over recompiling it manually)
If you haven't run brew cleanup, you already got the previous jpeg version in your brew's cellar, switch to it:
brew switch jpeg 8d
If you get a jpeg does not have a version "8d" in the Cellar. error, you need to first restore it by reverting the history:
cd $(brew --prefix)/Homebrew/Library/Taps/homebrew/homebrew-core
git checkout b231469
HOMEBREW_NO_AUTO_UPDATE=1 brew install jpeg
git checkout master
brew switch jpeg 8d
You can find out the commit hash by using brew log jpeg and going through the commit messages.
The downside is that there might be other formulas that require the newer version to work properly, e.g. imagemagick. If you face such incompatibility issues, check the first solution above.
3. Downgrade jpeg by manually recompiling (not recommended)
Fetch the source, compile and overwrite brew's version. Refer to Denis' answer for details.
This is not recommended because it violates the integrity of your brew metadata. Brew thinks that it has the 8b version, however, you manually compiled 9b and overwrote the files.
4. Manually symlink the old version (seriously?)
Do not manually symlink the leftover libjpeg.8.dylib. If the file is there, you can just brew switch to it as mentioned in the second solution above.
It's just the worst hack you can do here.
Such issues will be eventually fixed.
I encountered the similar problem today. The cause of the problem was a newer version of libjpeg. And the solution that worked was building php bottle from source.
Check available version of libjpeg:
$brew info libjpeg
jpeg: stable 9b (bottled)
Image manipulation library
http://www.ijg.org
/usr/local/Cellar/jpeg/9b (20 files, 724KB) *
Poured from bottle on 2017-08-07 at 12:06:42
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/jpeg.rb
Install php from source:
brew install --build-from-source php56
Update:
If you have already installed php you have to uninstall it prior to building from source. It also applies to extensions.
I had the same problem and I solved it by downloading the libjpeg tarball manuallu from here and running:
tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b
cp /usr/share/libtool/config.sub .
cp /usr/share/libtool/config.guess .
./configure --enable-shared --enable-static
make
sudo make install
sudo ranlib /usr/local/lib/libjpeg.a #Optionally
I was getting a similar problem trying to upgrade composer:
dyld: Library not loaded: /usr/local/opt/jpeg/lib/libjpeg.8.dylib
Referenced from: /usr/local/bin/php
Reason: image not found
composer: PHP Phar support is required for this formula
Error: An unsatisfied requirement failed this build.
Problem was php couldn't find libjpeg library anymore. One way to fix this is to reinstall php and libjpeg. Here's how I reinstalled them:
# find out which version of php is installed
brew list | grep php
# uninstall php
brew uninstall php70 --ignore-dependencies
# uninstall libjpeg
brew uninstall libjpeg --ignore-dependencies
# install libjpeg again
brew install libjpeg
# install php again
brew install php70
I was then able to upgrade everything again :)
brew update && brew upgrade
🍻
Following command worked for me.
brew switch libjpeg 8d
A dirty solution, that work fine for me.
ln -s /usr/local/lib/libjpeg.9.dylib /usr/local/lib/libjpeg.8.dylib
I didn't try Jirson's answer, but what worked for me was:
brew uninstall libjpeg
brew install libjpeg
I was having the same problem and the only thing that helped was to use the --build-from-source parameter:
brew install php70 --build-from-source
I had also this problem. I solved it in this way:
Uninstall jpeg/8b:
brew uninstall --ignore-dependencies libjpeg
Uninstall jpeg/9b:
brew uninstall --ignore-dependencies jpeg
Re install jpeg/9b
brew install jpeg
I am sharing this as I had the same issue when using PHP5.4. Originally, I had PHP5.6 and it was working fine. Then I downgraded to php5.4 as my app only supports PHP5.4.
Then this error came up. I noticed that I have libjpeg.9.dylib and PHP5.4 is looking for libjpeg.8.dylib.
Doing the following sorted the issue:
brew uninstall php54
brew install --build-from-source php54
As #lifecom notes, this is fixed with homebrew's php70 update, but you might still run into the problem if brew upgrade is trying to update other packages before upgrading php70.
Fix this by manually updating php70 first with brew upgrade php70 and you then should be able to run brew upgrade no problem (or, well, at least without this problem).
In some cases, especially in Catalina, the Homebrew cask for the package may exist but not be properly linked. In that case try:
brew link libjpeg
brew reinstall php --build-from-source
Homebrew php has been updated and no longer an issue with libjpeg