(map blog thoughts)

Aug 13, 2017 - homebrew

Dyld: Library Not Loaded Error

This post is specifically for an error related to libjpeg.8.dylib after a Homebrew update.

Recently I upgraded Homebrew, and then, upon attempting to write a commit message received an error followed by emacs unwilling to wake up.

The error was:

dyld: Library not loaded: /usr/local/opt/jpeg/lib/libjpeg.8.dylib
dyld: Library not loaded: /usr/local/opt/jpeg/lib/libjpeg.8.dylib
  Referenced from: /usr/local/Cellar/emacs/25.2/Emacs.app/Contents/MacOS/Emacs
  Reason: image not found
error: emacs died of signal 5
error: There was a problem with the editor 'emacs'.
Please supply the message using either -m or -F option.

The issue

After googling around for a solution, it turns out that the Jpeg formula was updated to 9b on August 7th, 2017, and seems to break after that commit.

Most people reporting the error seems to see the error associated with PHP, and one of the solution seems to be to uninstall PHP, and re-install from source.

Of course my problem is a little different (I just need emacs to work), and I needed a way to correct, or at least revert back to a working version of ligjpeg. If you need to do that, here is what you will need to do.

The easy way out

If you have not already run brew cleanup you might be in luck! brew allows you to simply "switch" to another version of a formula if you still have it laying around. First, let us see what we have using brew info

brew info libjpeg
$ brew info libjpeg
jpeg: stable 9b (bottled)
Image manipulation library
/usr/local/Cellar/jpeg/8d (19 files, 714.0KB) *
  Poured from bottle on 2017-08-13 at 17:13:31
/usr/local/Cellar/jpeg/9b (20 files, 725.2KB)
  Poured from bottle on 2017-08-11 at 08:03:40
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/jpeg.rb

Woot! Now, we simply switch over to using the older version, like so:

brew switch libjpeg
$ brew switch libjpeg 8d

You are off to the races! Unfortunately, I had already run brew cleanup, which left me having to take a longer route to a working version of libjpeg.

A (potential) solution

All of the steps for this exercise need to happen in Homebrews installation folder. Let us start there:

Ensure we see what we expect to see
# first `cd` into the location where brew stores all your formulas
$ cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula;
# You should find `jpeg.rb` listed here
$ ls -al jpeg.rb
-rw-r--r--  1 raju  admin  807 Aug 13 17:14 jpeg.rb

Great! So we have a version of jpeg.rb which is the formula for libjpeg. Now that we have the correct directory and know the file in question we need figure out which commit upgraded libjpeg to 9b. The log to the rescue!

brew log
$ brew log jpeg

If you have never done this before, you will get a prompt from brew to fetch all of it’s git history. Simply do what brew tells you to do:

Update brew’s git history
$ git -C "$(brew --repo homebrew/core)" fetch --unshallow

Now, brew log jpeg should reveal the following commits (depending on when you updated brew you may have to go further back to find the necessary commit)

jpeg.rb commit history
commit 2cfb629ee82e4e3bced64d14669192b281b14055
Author: BrewTestBot <brew-test-bot@googlegroups.com>
Date:   Mon Aug 7 06:33:22 2017 +0000

    jpeg: update 9b bottle.

commit 1fee4a326dd7144e192027705fa82d86e4676837
Author: ilovezfs <ilovezfs@icloud.com>
Date:   Sat Aug 5 11:44:29 2017 -0700

    jpeg 9b

commit b23146936a3abb57aabb39017561e7b0c750abbf
Author: FX Coudert <fxcoudert@gmail.com>
Date:   Sun Mar 12 09:15:30 2017 +0100

    jpeg: drop universal

commit 7f16e5aa9451ea391cbb7822a59d2bf702c5b834
Author: BrewTestBot <brew-test-bot@googlegroups.com>
Date:   Tue Sep 6 14:22:41 2016 +0100

    jpeg: update 8d bottle.

Aha! Looks like 7f16e5aa9451ea391cbb7822a59d2bf702c5b834 is what we want! The rest is easy.

Downgrading libjpeg to version 8
# first unlink version 9b
$ brew unlink jpeg
# checkout the version of the formula we care about
$ git checkout 7f16e5aa9451ea391cbb7822a59d2bf702c5b834 jpeg.rb
# use brew to install that specific formula
$ brew install jpeg
# reset everything to it's initial state
$ git reset --hard HEAD
# just to be sure everything is linked correctly
$ brew switch libjpeg 8d

Now, waking up emacs works just fine. Hope this helps someone out there.