110

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?

sepehr
  • 17,110
  • 7
  • 81
  • 119
Snowcrash
  • 80,579
  • 89
  • 266
  • 376

15 Answers15

252

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
sepehr
  • 17,110
  • 7
  • 81
  • 119
Denis Pitzalis
  • 2,800
  • 1
  • 15
  • 12
170

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.

Community
  • 1
  • 1
sepehr
  • 17,110
  • 7
  • 81
  • 119
  • 4
    This should be the accepted answer almost positively. – Mike Legacy Sep 25 '17 at 18:39
  • First method worked for me - however, I also had to run `xcode-select --install` before I could reinstall PHP 7.1 from source. – jrdn Oct 18 '17 at 14:02
  • Method 2 does not find the commit anymore, maybe because of force pushes. A workaround: instead of `git checkout b231469` I did `curl https://raw.githubusercontent.com/Homebrew/homebrew-core/b23146936a3abb57aabb39017561e7b0c750abbf/Formula/jpeg.rb > Formula/jpeg.rb`, then instead of `git checkout master` I wrote `git checkout Formula/jpeg.rb`. – tuomassalo Jan 31 '18 at 13:44
  • @tuomassalo Thanks for sharing your workaround. [The commit](https://github.com/Homebrew/homebrew-core/commit/b231469) still exists in my local clone. If it was due to a forced push, you were not able to fetch it using `curl` either. To me, it seems like that your local clone is somehow not up to date with the remote . – sepehr Feb 01 '18 at 09:03
  • @sepehr You are correct: my local copy didn't have all the history. Now that I try again, cloning the repo manually and checking out `b23...` worked fine. – tuomassalo Feb 01 '18 at 13:55
  • 2
    if `git checkout b231469` gets error, then try this first `git -C "$(brew --repo homebrew/core)" fetch --unshallow` – Yoshi Jul 30 '18 at 05:27
  • It's `brew reinstall php@7.3 --build-from-source` for the PHP 7.3 – Galichev Anton Oct 18 '19 at 13:26
12

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.

sepehr
  • 17,110
  • 7
  • 81
  • 119
pwc
  • 446
  • 3
  • 10
  • same here just now - what exactly happened? brew updated libjpeg and its now not in the right version for php5.6? – Toby Aug 07 '17 at 12:10
  • btw. your answer outputs: Warning: josegonzalez/php/php56 5.6.30_6 is already installed probably should remove it first? seems scary :P – Toby Aug 07 '17 at 12:13
  • Yes, you have to uninstall `php` and the extensions. Then install `php` with `--build-from-source`. I've looked into git history of the `libjpeg` formula and the version of `libjpeg` was update to `9b` on August 5th. – pwc Aug 07 '17 at 12:19
  • nice so this somehow broke it... well, compiling is done. it seems it works again now - thanks :) – Toby Aug 07 '17 at 12:23
  • This worked for PHP 7.1 as well, just uninstall and then run `brew install --build-from-source php71`. Issue arose for me tonight after running `brew upgrade`. – Dwight Aug 07 '17 at 13:36
7

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

GabLeRoux
  • 16,715
  • 16
  • 63
  • 81
6

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
sepehr
  • 17,110
  • 7
  • 81
  • 119
Jirson Tavera
  • 1,303
  • 14
  • 18
5

Following command worked for me.

brew switch libjpeg 8d
Pankaj Agrawal
  • 1,469
  • 2
  • 17
  • 27
  • This worked for me. I looked in my directory using `ls /usr/local/Cellar/jpeg/` and saw two subdirectories `8d` and `9c` Since my error was `dyld: Library not loaded: /usr/local/opt/jpeg/lib/libjpeg.8.dylib` It made sense it was trying to load the wrong one. – TrophyGeek Apr 12 '18 at 23:05
5

A dirty solution, that work fine for me.

ln -s /usr/local/lib/libjpeg.9.dylib /usr/local/lib/libjpeg.8.dylib
Shobi
  • 10,374
  • 6
  • 46
  • 82
Jorge Lobo
  • 59
  • 1
  • 1
  • There is a reason software is versioned. This will segfault your server as soon as it tries making a library call that doesn’t exist or has changed signatures. – miken32 Jan 09 '22 at 19:47
4

I didn't try Jirson's answer, but what worked for me was:

brew uninstall libjpeg
brew install libjpeg
jds
  • 7,910
  • 11
  • 63
  • 101
  • What do you do with all the packages that are dependent upon libjpeg? Error: Refusing to uninstall /usr/local/Cellar/jpeg/9b because it is required by [SNIP], which are currently installed. – Richard A Quadling Aug 15 '17 at 10:17
  • 1
    @RichardAQuadling You can use the `--ignore-dependencies` option when uninstalling. – sepehr Aug 15 '17 at 15:02
  • This didn't seem to resolve the issue for me. Latest jpeg version is currently jpeg-9b. libjpeg.8.dylib is still missing after following @gwg's instructions. – Gino Sep 01 '17 at 13:57
3

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
martin
  • 93,354
  • 25
  • 191
  • 226
2

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

Uninstall jpeg/8b and 9b

Re install jpeg/9b

brew install jpeg

Re-install jpeg/9b

1

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
sepehr
  • 17,110
  • 7
  • 81
  • 119
0

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).

0

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
alexw
  • 8,468
  • 6
  • 54
  • 86
0

I solved mine with brew reinstall jpeg-xl

blongho
  • 1,151
  • 9
  • 12
-1

Homebrew php has been updated and no longer an issue with libjpeg

lifecom
  • 139
  • 1
  • 5