Installing libmagic in Mac OS X (for Python-magic)

When trying to install python-magic I found out that libmagic was not installed (or not properly available) in Mac OS X 10.7 Lion. There was little information on this problem on the Internet so I thought it might be helpful if I briefly wrote down my experiences.

When installing python-magic:

pip install python-magic

there was an error reported by ctypes (a depency that is installed as well). As ctypes should be part of your python (since 2.5) you can ignore this error or install python-magic from this updated version on GitHub:

After the installation completes however, python-magic gives an error (libmagic not available):

>>> import magic
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[...]/python-magic/", line 128, in <module>
    raise ImportError('failed to find libmagic.  Check your installation')
ImportError: failed to find libmagic.  Check your installation

The easiest way to install libmagic is through Homebrew, a Mac package manager. I used to resort to Fink or Macports in the past, but Brew seems a lot cleaner and more straightforward. Install Homebrew according to the instructions and then install libmagic:

brew install libmagic

Brew installs libmagic in /usr/local/Cellar/ . Python-magic however seems to be looking for a reference to libmagic in /usr/local/lib/

To solve this last problem we can create a symlink in /usr/local/lib to the right file.

cd /usr/local/lib/
ln -s ../Cellar/libmagic/5.04/lib/libmagic.dylib libmagic.dylib

After this, python-magic is able to find libmagic and works properly.

  • Enjoyed this post? Please share it with others:
  • Favicon
  • Digg Favicon
  • Facebook Favicon
  • Google Favicon
  • LinkedIn Favicon
  • Reddit Favicon
  • Technorati Favicon
  • TwitThis Favicon
22 July 2011 • Apple, Geek stuff


Ehtesh Choudhury  on 17 Aug 2011 wrote:

Ideally, in python-magic should take care of that. It assumes you’re using MacPorts, but I guess it hasn’t broken for most people, and for the few left, symlinking solves the problem.

David  on 01 Mar 2012 wrote:

FYI (and for other readers), libmagic is part of the “file” port in macports.





Remember my personal information

Notify me of follow-up comments?

<< Back to main