Django Installation on Mac OS X Lion

Like many of Apple fanboys out there even I decided to upgrade Mac OS X Snow Leopard (10.6) to Mac OS X Lion (10.7) as soon as it was made available on App Store. Upgrading from App Store was pretty smooth and was surprised it did not break any apps (May be that’s because it’s just an incremental upgrade with some fancy features) except my Django projects failing to validate because of the default python version change (from 2.6.x to 2.7.1) and can be easily fixed. However I decided to go for fresh install of OS X Lion to test how fresh install (wipe out disk after backup and install ) works with OS X Lion.

Once done with fresh installation of Mac OS X Lion, immediate task was setting up my development environment which includes MySQL server, phpMyAdmin and Django.

Steps I followed to get Django development environment back are as below

  1. Install Xcode via Mac OS X App Store ( Might be required when you compile and install from source)
  2. Install MySQL Server
  3. Install phpMyAdmin
  4. By default jpeg, freetype, littlecms image library support is missing, you need to install at least jpeg support so ImageField in your django project supports jpeg formats.
    1. Install JPEG libraries
      1. Download JPEG library source curl -O
      2. Extract tar -xvzf jpegsrc.v8c.tar.gz
      3. Switch to jpeg directory cd jpeg-8c
      4. Run ./confgure
      5. Run make
      6. Run sudo make install
    2. Install Freetype libraries
      1. Download Freetype library source curl -O
      2. Extract tar -xvzf freetype-2.4.5.tar.gz
      3. Switch to freetype directory cd freetype-2.4.5
      4. Run ./configure
      5. Run make
      6. Run sudo make install
  5. Install Django using sudo easy_install django
  6. Install Python Image Library (PIL) using sudo easy_install PIL
  7. Install mysql-python driver using sudo easy_install MySQL-python (Make sure you got mysql in your PATH, otherwise this will fail with mysql_config not found error)

That’s all you need to go ahead and start creating Django projects. However I prefer using virtualenv for all my Python projects. If you take this route, you can use pip install <package_name> to install necessary packages for example pip install django, pip install MySQL-python, pip install PIL to install Django, MySQL Python driver and Python Image Library.

Update 1 (25/07/2011): If you get error “django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/xxx/xxx/pyprojects/xxx/lib/python2.7/site-packages/, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Users/xxx/xxx/pyprojects/xxx/lib/python2.7/site-packages/
Reason: image not found”, when you validate your project or try to use MySQL-python,

Type export DYLD_LIBRARY_PATH=”$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/” in your terminal and see if problem persists. You can add it to your .profile file to make this persistent so you don’t need to run it every time.

24 thoughts on “Django Installation on Mac OS X Lion

  1. Pingback: Installation of MySQL server on Apple Mac OS X 10.7 aka Lion | A Non-Programmer Approach to Django

  2. Thanks! Thanks! Thanks!
    Im have a really tough week so far because of Lion, nothing that a fresh install and this tutoria doesn’t resolve.

  3. Pingback: Installing Django on Snow Leopard (and MySQL and PIL)

  4. Hi

    I get the error:
    File “/Library/Python/2.7/site-packages/django/db/backends/mysql/”, line 14, in
    raise ImproperlyConfigured(“Error loading MySQLdb module: %s” % e)
    django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/torsten/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/, 2): no suitable image found. Did find:
    /Users/torsten/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/ mach-o, but wrong architecture

    I tried to export the path but it didn’t help. Do you have any idea ?

  5. My config file in bin not lib and had to put it in my /private/etc/bachrc file like this:

    export PATH=/usr/local/bin:/usr/local/mysql/bin/:$PATH

    Then python-mysql installed ok.

  6. @torsten if you copy and paste that additional export path line, make sure to change the quotation marks to normal ones…also make sure you installed the 64 bit version of mysql server.

  7. Great tutorial! Much easier than anything else I can find (and it worked). The only issue I am having, I still have to add this line (export DYLD_LIBRARY_PATH=”$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/”) to the terminal each time. I have tried to add it to the .profile (nano ~/.profile) but without success. Although this line works, my mysql_config, is in /usr/local/mysql/bin? Any advice?

  8. Update: Just go it working. Added export DYLD_LIBRARY_PATH=”$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/” to bash_profile (nano ~/.bash_profile)!

  9. I did all this, but when i import PIL, it says ” from PIL import Image
    ImportError: No module named PIL”
    and when i try to install PIL via easy_install it says
    “Processing PIL-1.1.7-py2.7-macosx-10.7-intel.egg
    PIL 1.1.7 is already the active version in easy-install.pth”

  10. Getting this error on sudo easy_install PIL

    unable to execute llvm-gcc-4.2: No such file or directory
    error: Setup script exited with error: command ‘llvm-gcc-4.2′ failed with exit status 1

  11. The quotes in the update 1 export command don’t work in properly in the command but once I replaced them with normal quotes it worked fine.

    export DYLD_LIBRARY_PATH=”$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/”

  12. i am using Macbook Pro with Lion installed and i also installed the apple latest development toolkit .
    I tried to install the JPEG but i stuck with one problem.
    When i typed the commond

    I have following output
    checking build system type… x86_64-apple-darwin11.2.0
    checking host system type… x86_64-apple-darwin11.2.0
    checking target system type… x86_64-apple-darwin11.2.0
    checking for a BSD-compatible install… ./install-sh -c
    checking whether build environment is sane… yes
    checking for a thread-safe mkdir -p… ./install-sh -c -d
    checking for gawk… no
    checking for mawk… no
    checking for nawk… no
    checking for awk… awk
    checking whether make sets $(MAKE)… yes
    checking for style of include used by make… GNU
    checking for gcc… gcc
    checking whether the C compiler works… yes
    checking for C compiler default output file name… a.out
    checking for suffix of executables…
    checking whether we are cross compiling… no
    checking for suffix of object files… o
    checking whether we are using the GNU C compiler… yes
    checking whether gcc accepts -g… yes
    checking for gcc option to accept ISO C89… none needed
    checking dependency style of gcc… none
    checking for function prototypes… yes
    checking how to run the C preprocessor…
    after that it runs for long tie and it seems t be that it stuck on
    there is no output or message. Do you have any idea why it is so?
    any suggestion to solve this problem will be highly appreciated.

  13. I have Xcode installed, have sym linked to mysql_config, but when I run `python install` I get a flood of errors like this:

    ‘_mysql_ConnectionObject’ has no member named ‘open’
    ‘_mysql_ConnectionObject’ has no member named ‘connection’

    Then, it prints this error:
    lipo: can’t open input file: /var/tmp//ccEHQ50W.out (No such file or directory)
    Which makes sense, because of the double slash (not sure how to resolve that, however)

    And finally, the standard closing error of:
    error: Setup script exited with error: command ‘llvm-gcc-4.2′ failed with exit status 1

  14. I’d really recommend you look at a couple of things that will make your life a whole lot easier. #1 is homebrew. It makes installing unixy stuff much, much easier on Mac OS X. `brew install foo` is almost always enough to get what you want.

    The other, as mentioned above is virtualenv. If you don’t develop with virtualenv, then you make your life a whole lot harder. I even deploy to a virtualenv on production servers. It makes everything else installable without root requirements.

    Then, once you are doing that, look into fabric as a deployment solution. When you are able to deploy with a single command (`fab deploy –host servername`) it suddenly becomes simple to deploy.

    (Alternatively, look at heroku for a single-command deployment and hosting solution).

    Other random thoughts: you really should use Postgres instead of MySQL (for lots of reasons), and if you aren’t using SCM of some sort, even if it’s svn, then STOP WHAT YOU ARE DOING NOW AND START USING SOMETHING. It really is that important.

  15. +1 for using homebrew. It may seem more complicated initially, but will save you a ton of time going forward developing on a Mac.

  16. Thanks a ton. I ran into some issues with my GCC, but downgrading my Xcode to 4.2 did the trick for me. Something about 4.3 wasn’t working when trying to build executable files.

  17. How did you downgrade to 4.2? I’m guessing that’s what I need to do here – 4.3 is breaking for me too.

  18. +1 re homebrew

    For XCode 4.3, a few tweaks might help:

    1) You need the command-line tools: (no longer part of xcode default install)

    Go to XCode / Preferences, “Downloads” tab, download the command-line tools package.

    2) You need autotools, they’re no longer distributed by apple:

    I like doing this through homebrew.
    * Get homebrew installed (see )
    * brew doctor (checks install, fix any issues identified)
    * brew install automake
    * brew install
    * brew link autoconf

    I would then suggest using virtualenv for deployment.

  19. When I run ./configure :

    checking build system type… i386-apple-darwin11.4.2
    checking host system type… i386-apple-darwin11.4.2
    checking target system type… i386-apple-darwin11.4.2
    checking for a BSD-compatible install… /usr/bin/install -c
    checking whether build environment is sane… yes
    checking for a thread-safe mkdir -p… ./install-sh -c -d
    checking for gawk… no
    checking for mawk… no
    checking for nawk… no
    checking for awk… awk
    checking whether make sets $(MAKE)… no
    checking for style of include used by make… none
    checking for gcc… no
    checking for cc… no
    checking for cl.exe… no
    configure: error: in `/Users/developerbook/jpeg-8c’:
    configure: error: no acceptable C compiler found in $PATH
    See `config.log’ for more details

    What I did wrong?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>