OS X Installation

We highly recommend using the Homebrew package manager.Ideally you could start from a clean /usr/local to avoid conflicts.In the following, we assume that you’re using Anaconda Python and Homebrew.

CUDA: Install via the NVIDIA package that includes both CUDA and the bundled driver. CUDA 7 is strongly suggested. Older CUDA require libstdc++ while clang++ is the default compiler and libc++ the default standard library on OS X 10.9+. This disagreement makes it necessary to change the compilation settings for each of the dependencies. This is prone to error.

Library Path: We find that everything compiles successfully if $LD_LIBRARY_PATH is not set at all, and $DYLD_FALLBACK_LIBRARY_PATH is set to provide CUDA, Python, and other relevant libraries (e.g. /usr/local/cuda/lib:$HOME/anaconda/lib:/usr/local/lib:/usr/lib).In other ENV settings, things may not work as expected.

General dependencies

  1. brew install -vd snappy leveldb gflags glog szip lmdb
  2. # need the homebrew science source for OpenCV and hdf5
  3. brew tap homebrew/science
  4. brew install hdf5 opencv

If using Anaconda Python, a modification to the OpenCV formula might be neededDo brew edit opencv and change the lines that look like the two lines below to exactly the two lines below.

  1. -DPYTHON_LIBRARY=#{py_prefix}/lib/libpython2.7.dylib
  2. -DPYTHON_INCLUDE_DIR=#{py_prefix}/include/python2.7

If using Anaconda Python, HDF5 is bundled and the hdf5 formula can be skipped.

Remaining dependencies, with / without Python

  1. # with Python pycaffe needs dependencies built from source
  2. brew install --build-from-source --with-python -vd protobuf
  3. brew install --build-from-source -vd boost boost-python
  4. # without Python the usual installation suffices
  5. brew install protobuf boost

BLAS: already installed as the Accelerate / vecLib Framework. OpenBLAS and MKL are alternatives for faster CPU computation.

Python (optional): Anaconda is the preferred Python.If you decide against it, please use Homebrew.Check that Caffe and dependencies are linking against the same, desired Python.

Continue with compilation.

libstdc++ installation

This route is not for the faint of heart.For OS X 10.10 and 10.9 you should install CUDA 7 and follow the instructions above.If that is not an option, take a deep breath and carry on.

In OS X 10.9+, clang++ is the default C++ compiler and uses libc++ as the standard library.However, NVIDIA CUDA (even version 6.0) currently links only with libstdc++.This makes it necessary to change the compilation settings for each of the dependencies.

We do this by modifying the Homebrew formulae before installing any packages.Make sure that Homebrew doesn’t install any software dependencies in the background; all packages must be linked to libstdc++.

The prerequisite Homebrew formulae are

  1. boost snappy leveldb protobuf gflags glog szip lmdb homebrew/science/opencv

For each of these formulas, brew edit FORMULA, and add the ENV definitions as shown:

  1. def install
  2. # ADD THE FOLLOWING:
  3. ENV.append "CXXFLAGS", "-stdlib=libstdc++"
  4. ENV.append "CFLAGS", "-stdlib=libstdc++"
  5. ENV.append "LDFLAGS", "-stdlib=libstdc++ -lstdc++"
  6. # The following is necessary because libtool likes to strip LDFLAGS:
  7. ENV["CXX"] = "/usr/bin/clang++ -stdlib=libstdc++"
  8. ...

To edit the formulae in turn, run

  1. for x in snappy leveldb protobuf gflags glog szip boost boost-python lmdb homebrew/science/opencv; do brew edit $x; done

After this, run

  1. for x in snappy leveldb gflags glog szip lmdb homebrew/science/opencv; do brew uninstall $x; brew install --build-from-source -vd $x; done
  2. brew uninstall protobuf; brew install --build-from-source --with-python -vd protobuf
  3. brew install --build-from-source -vd boost boost-python

If this is not done exactly right then linking errors will trouble you.

Homebrew versioning that Homebrew maintains itself as a separate git repository and making the above brew edit FORMULA changes will change files in your local copy of homebrew’s master branch. By default, this will prevent you from updating Homebrew using brew update, as you will get an error message like the following:

  1. $ brew update
  2. error: Your local changes to the following files would be overwritten by merge:
  3. Library/Formula/lmdb.rb
  4. Please, commit your changes or stash them before you can merge.
  5. Aborting
  6. Error: Failure while executing: git pull -q origin refs/heads/master:refs/remotes/origin/master

One solution is to commit your changes to a separate Homebrew branch, run brew update, and rebase your changes onto the updated master. You’ll have to do this both for the main Homebrew repository in /usr/local/ and the Homebrew science repository that contains OpenCV in /usr/local/Library/Taps/homebrew/homebrew-science, as follows:

  1. cd /usr/local
  2. git checkout -b caffe
  3. git add .
  4. git commit -m "Update Caffe dependencies to use libstdc++"
  5. cd /usr/local/Library/Taps/homebrew/homebrew-science
  6. git checkout -b caffe
  7. git add .
  8. git commit -m "Update Caffe dependencies"

Then, whenever you want to update homebrew, switch back to the master branches, do the update, rebase the caffe branches onto master and fix any conflicts:

  1. # Switch batch to homebrew master branches
  2. cd /usr/local
  3. git checkout master
  4. cd /usr/local/Library/Taps/homebrew/homebrew-science
  5. git checkout master
  6. # Update homebrew; hopefully this works without errors!
  7. brew update
  8. # Switch back to the caffe branches with the formulae that you modified earlier
  9. cd /usr/local
  10. git rebase master caffe
  11. # Fix any merge conflicts and commit to caffe branch
  12. cd /usr/local/Library/Taps/homebrew/homebrew-science
  13. git rebase master caffe
  14. # Fix any merge conflicts and commit to caffe branch
  15. # Done!

At this point, you should be running the latest Homebrew packages and your Caffe-related modifications will remain in place.