Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Installing Caffe on Mac 10.11.5 and later in the 10.11 series, and 10.12.1 and later in the 10.12 series
#!/bin/sh
# Install brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# Apple hides old versions of stuff at https://developer.apple.com/download/more/
# Install the latest XCode (8.0).
# We used to install the XCode Command Line Tools 7.3 here, but that would just upset the most recent versions of brew.
# So we're going to install all our brew dependencies first, and then downgrade the tools. You can switch back after
# you have installed caffe.
# Install CUDA toolkit 8.0 release candidate
# Register and download from https://developer.nvidia.com/cuda-release-candidate-download
# or this path from https://developer.nvidia.com/compute/cuda/8.0/rc/local_installers/cuda_8.0.29_mac-dmg
# Select both the driver and the toolkit, no documentation necessary
# Install the experimental NVIDIA Mac drivers
# Download from http://www.nvidia.com/download/driverResults.aspx/103826/en-us
# Install cuDNN v5 for 8.0 RC or use the latest when it's available
# Register and download from https://developer.nvidia.com/rdp/cudnn-download
# or this path: https://developer.nvidia.com/rdp/assets/cudnn-8.0-osx-x64-v5.0-ga-tgz
# extract to the NVIDIA CUDA folder and perform necessary linking
# into your /usr/local/cuda/lib and /usr/local/cuda/include folders
# You will need to use sudo because the CUDA folder is owned by root
sudo tar -xvf ~/Downloads/cudnn-8.0-osx-x64-v5.0-ga.tar /Developer/NVIDIA/CUDA-8.0/
sudo ln -s /Developer/NVIDIA/CUDA-8.0/lib/libcudnn.dylib /usr/local/cuda/lib/libcudnn.dylib
sudo ln -s /Developer/NVIDIA/CUDA-8.0/lib/libcudnn.5.dylib /usr/local/cuda/lib/libcudnn.5.dylib
sudo ln -s /Developer/NVIDIA/CUDA-8.0/lib/libcudnn_static.a /usr/local/cuda/lib/libcudnn_static.a
sudo ln -s /Developer/NVIDIA/CUDA-8.0/include/cudnn.h /usr/local/cuda/include/cudnn.h
# Install the brew dependencies
# Do not install python through brew. Only misery lies there
# We'll use the versions repository to get the right version of boost and boost-python
# We'll also explicitly upgrade libpng because it's out of date
# Do not install numpy via brew. Your system python already has it.
brew install -vd snappy leveldb gflags glog szip lmdb
brew install hdf5 opencv
brew upgrade libpng
brew tap homebrew/science
brew tap homebrew/versions
brew install --build-from-source --with-python -vd protobuf
brew install --build-from-source -vd boost159 boost-python159
# Clone the caffe repo
cd ~/Documents
git clone https://github.com/BVLC/caffe.git
# Setup Makefile.config
# You can download mine directly from here, but I'll explain all the selections
# For XCode 7.3:
# https://www.dropbox.com/s/vuy6ha0p7cc5px3/Makefile.config?dl=1
# For XCode 8.0 and later (Sierra):
# https://dl.dropboxusercontent.com/u/2891540/caffe_10.12/Makefile.config
# First, we'll enable cuDNN
# USE_CUDNN := 1
# In order to use the built-in Accelerate.framework, you have to reference it.
# Astonishingly, nobody has written this anywhere on the internet.
# BLAS := atlas
# If you use El Capitan (10.11), we'll use the 10.11 sdk path for vecLib:
# BLAS_INCLUDE := /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers
# Otherwise (10.12), let's use the 10.12 sdk path:
# BLAS_INCLUDE := /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers
# BLAS_LIB := /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A
# Configure to use system python and system numpy
# PYTHON_INCLUDE := /System/Library/Frameworks/Python.framework/Headers \
# /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/include
# PYTHON_LIB := /System/Library/Frameworks/Python.framework/Versions/2.7/lib
# Configure to enable Python layers. Some projects online need this
# WITH_PYTHON_LAYER := 1
curl https://dl.dropboxusercontent.com/u/2891540/Makefile.config -o Makefile.config
# Download the XCode Command Line Tools for 7.3, since NVIDIA does not yet support Xcode 8.0's tools
# http://adcdownload.apple.com/Developer_Tools/Command_Line_Tools_OS_X_10.11_for_Xcode_7.3/Command_Line_Tools_OS_X_10.11_for_Xcode_7.3.dmg
# Now, choose those tools instead
sudo xcode-select --switch /Library/Developer/CommandLineTools
# Go ahead and build.
cd caffe
make -j8 all
# To get python going, first we need the dependencies
# On a super-clean Mac install, you'll need to easy_install pip.
sudo -H easy_install pip
# Now, we'll install the requirements system-wide. You may also muck about with a virtualenv.
# Astonishingly, --user is not better known.
pip install --user -r python/requirements.txt
# Go ahead and run pytest now. Horrible @rpath warnings which can be ignored.
make -j8 pytest
# Now, install the package
# Make the distribution folder
make distribute
# Install the caffe package into your local site-packages
cp -r distribute/python/caffe ~/Library/Python/2.7/lib/python/site-packages/
# Finally, we have to update references to where the libcaffe libraries are located.
# You can see how the paths to libraries are referenced relatively
# otool -L ~/Library/Python/2.7/lib/python/site-packages/caffe/_caffe.so
# Generally, on a System Integrity Protection -enabled (SIP-enabled) Mac this is no good.
# So we're just going to change the paths to be direct
cp distribute/lib/libcaffe.so.1.0.0-rc3 ~/Library/Python/2.7/lib/python/site-packages/caffe/libcaffe.so.1.0.0-rc3
install_name_tool -change @rpath/libcaffe.so.1.0.0-rc3 ~/Library/Python/2.7/lib/python/site-packages/caffe/libcaffe.so.1.0.0-rc3 ~/Library/Python/2.7/lib/python/site-packages/caffe/_caffe.so
# Verify that everything works
# start python and try to import caffe
python -c 'import caffe'
# If you got this far without errors, congratulations, you installed Caffe on a modern Mac OS X
@shixudongleo

This comment has been minimized.

Show comment Hide comment
@shixudongleo

shixudongleo Aug 12, 2016

typo on line # 23 includ -> include

sudo ln -s /Developer/NVIDIA/CUDA-8.0/include/cudnn.h /usr/local/cuda/include/cudnn.h

typo on line # 23 includ -> include

sudo ln -s /Developer/NVIDIA/CUDA-8.0/include/cudnn.h /usr/local/cuda/include/cudnn.h
@doctorpangloss

This comment has been minimized.

Show comment Hide comment
@doctorpangloss

doctorpangloss Aug 19, 2016

@shixudongleo thanks for the note, I fixed it. Based on your fork, it looks like you got everything installed! That's great to see.

Owner

doctorpangloss commented Aug 19, 2016

@shixudongleo thanks for the note, I fixed it. Based on your fork, it looks like you got everything installed! That's great to see.

@Vikramank

This comment has been minimized.

Show comment Hide comment
@Vikramank

Vikramank Aug 30, 2016

Hi,
thanks for the script. Till line 67, everything went fine. Since I am using anaconda python, what would be the change in script. ( I tried but it threw me an error)

Hi,
thanks for the script. Till line 67, everything went fine. Since I am using anaconda python, what would be the change in script. ( I tried but it threw me an error)

@TimZaman

This comment has been minimized.

Show comment Hide comment
@TimZaman

TimZaman Sep 18, 2016

Instead of ln'ing /usr/local/cuda/.. you could just set the flag in Makefile.config to:
CUDA_DIR := /Developer/NVIDIA/CUDA-8.0/ right?

Instead of ln'ing /usr/local/cuda/.. you could just set the flag in Makefile.config to:
CUDA_DIR := /Developer/NVIDIA/CUDA-8.0/ right?

@doctorpangloss

This comment has been minimized.

Show comment Hide comment
@doctorpangloss

doctorpangloss Sep 20, 2016

@TimZaman That could work! Definitely try it. It's an annoying step for sure and pollutes brew.

Owner

doctorpangloss commented Sep 20, 2016

@TimZaman That could work! Definitely try it. It's an annoying step for sure and pollutes brew.

@jrimestad

This comment has been minimized.

Show comment Hide comment
@jrimestad

jrimestad Nov 1, 2016

This was very helpfull albeit I never got it compiling using CUDA 7.5 and Xcode 7.3 because of vecLib compile errors. Had no problem compiling without CUDA though 😄
In the end I still had to rename _caffe.dylib to _caffe.so in the python package to use caffe in python, which seems weird given this PR BVLC/caffe@a14f0ef

This was very helpfull albeit I never got it compiling using CUDA 7.5 and Xcode 7.3 because of vecLib compile errors. Had no problem compiling without CUDA though 😄
In the end I still had to rename _caffe.dylib to _caffe.so in the python package to use caffe in python, which seems weird given this PR BVLC/caffe@a14f0ef

@doctorpangloss

This comment has been minimized.

Show comment Hide comment
@doctorpangloss

doctorpangloss Nov 9, 2016

@jrimestad re: CUDA 7.5 and Xcode 7.3, definitely upgrade to El Capitan / Sierra if you can and use CUDA 8.0. If you're using a Hackintosh, I can confirm that Sierra with a Titan X (and probably all Maxwell chips) work fine. Allegedly the new drivers that ship with the CUDA installation package support Pascal, but it's untested on my end.

Re: renaming _caffe.dylib, yep, just pretty much ignore what other people say, they basically never follow their own steps on clean machines, so files are found in all sorts of places and they think stuff works when it really does not. Also, I believe this does not use CMAKE, while that PR does.

It's unusual that you should get vecLib compile errors. On both my Sierra and El Capitan stations I have vecLib in the right places. I corrected the installation instructions for Sierra though (it was essentially untested until now).

Owner

doctorpangloss commented Nov 9, 2016

@jrimestad re: CUDA 7.5 and Xcode 7.3, definitely upgrade to El Capitan / Sierra if you can and use CUDA 8.0. If you're using a Hackintosh, I can confirm that Sierra with a Titan X (and probably all Maxwell chips) work fine. Allegedly the new drivers that ship with the CUDA installation package support Pascal, but it's untested on my end.

Re: renaming _caffe.dylib, yep, just pretty much ignore what other people say, they basically never follow their own steps on clean machines, so files are found in all sorts of places and they think stuff works when it really does not. Also, I believe this does not use CMAKE, while that PR does.

It's unusual that you should get vecLib compile errors. On both my Sierra and El Capitan stations I have vecLib in the right places. I corrected the installation instructions for Sierra though (it was essentially untested until now).

@nvanthuy

This comment has been minimized.

Show comment Hide comment
@nvanthuy

nvanthuy Nov 19, 2016

Hi, when I ran make -j8 all, I got following errors"
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc3] Error

Please help. Thank you.

nvanthuy commented Nov 19, 2016

Hi, when I ran make -j8 all, I got following errors"
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc3] Error

Please help. Thank you.

@abouSara

This comment has been minimized.

Show comment Hide comment
@abouSara

abouSara Nov 28, 2016

in case where cudnn library is a tgz file, run
sudo tar -xvzf ~/Downloads/cudnn-8.0-osx-x64-v5.0-ga.tgz -C /Developer/NVIDIA/CUDA-8.0/
instead of
sudo tar -xvf ~/Downloads/cudnn-8.0-osx-x64-v5.0-ga.tar /Developer/NVIDIA/CUDA-8.0/

in case where cudnn library is a tgz file, run
sudo tar -xvzf ~/Downloads/cudnn-8.0-osx-x64-v5.0-ga.tgz -C /Developer/NVIDIA/CUDA-8.0/
instead of
sudo tar -xvf ~/Downloads/cudnn-8.0-osx-x64-v5.0-ga.tar /Developer/NVIDIA/CUDA-8.0/

@eddiesmo

This comment has been minimized.

Show comment Hide comment
@eddiesmo

eddiesmo Dec 20, 2016

Thank you so much for this guide!
Here are some other errors and solutions I encountered:

  1. make pytest doesn’t work!! Veclib issue:
    “ld: framework not found vecLib”
    Solved by reinstalling xcode command line tools

  2. import caffe doesn’t work! Due to a Malloc error. This is a leveldb issue.

Also posted this in my own blog:
https://eddiesmo.wordpress.com/2016/12/20/how-to-set-up-caffe-environment-and-pycaffe-on-os-x-10-12-sierra/

Thank you so much for this guide!
Here are some other errors and solutions I encountered:

  1. make pytest doesn’t work!! Veclib issue:
    “ld: framework not found vecLib”
    Solved by reinstalling xcode command line tools

  2. import caffe doesn’t work! Due to a Malloc error. This is a leveldb issue.

Also posted this in my own blog:
https://eddiesmo.wordpress.com/2016/12/20/how-to-set-up-caffe-environment-and-pycaffe-on-os-x-10-12-sierra/

@humphd

This comment has been minimized.

Show comment Hide comment
@humphd

humphd Dec 20, 2016

Thank you for this, it really helped me. I found that doing this on 10.12.2 required me to downgrade boost to 1.55 vs. 1.59 as mentioned above, or else make pycaffe always failed.

humphd commented Dec 20, 2016

Thank you for this, it really helped me. I found that doing this on 10.12.2 required me to downgrade boost to 1.55 vs. 1.59 as mentioned above, or else make pycaffe always failed.

@humphd

This comment has been minimized.

Show comment Hide comment
@humphd

humphd Dec 20, 2016

I also found that the version of protobuf used by brew and pip was breaking caffe at runtime (e.g., make pytest failing with crazy errors reading protobufs and __init__ got an unexpected keyword argument 'syntax' for every unit test). I found that I needed to uninstall/re-install an explicit version of protobuf as described in tensorflow/tensorflow#11 (comment), specifically I did:

$ brew uninstall protobuf
$ pip uninstall protobuf
$ pip install 'protobuf>=3.0.0a3'

humphd commented Dec 20, 2016

I also found that the version of protobuf used by brew and pip was breaking caffe at runtime (e.g., make pytest failing with crazy errors reading protobufs and __init__ got an unexpected keyword argument 'syntax' for every unit test). I found that I needed to uninstall/re-install an explicit version of protobuf as described in tensorflow/tensorflow#11 (comment), specifically I did:

$ brew uninstall protobuf
$ pip uninstall protobuf
$ pip install 'protobuf>=3.0.0a3'
@7ayden

This comment has been minimized.

Show comment Hide comment
@7ayden

7ayden Dec 29, 2016

I was so glad for the caffe easy installation gospel, yet there are tons of error occurring. However, I tried
$ brew uninstall protobuf
$ pip uninstall protobuf
$ pip install 'protobuf>=3.0.0a3'
then bash it.
and it shows ImportError: No module named caffe

7ayden commented Dec 29, 2016

I was so glad for the caffe easy installation gospel, yet there are tons of error occurring. However, I tried
$ brew uninstall protobuf
$ pip uninstall protobuf
$ pip install 'protobuf>=3.0.0a3'
then bash it.
and it shows ImportError: No module named caffe

@BirkhoffLee

This comment has been minimized.

Show comment Hide comment
@BirkhoffLee

BirkhoffLee Jan 8, 2017

Thanks for the tutorial! I've successfully built Caffe, used 2 days to figure out.

(中文版) For those Chinese who need help about this: https://blog.birkhoff.me/macos-sierra-10-12-2-build-caffe/

BirkhoffLee commented Jan 8, 2017

Thanks for the tutorial! I've successfully built Caffe, used 2 days to figure out.

(中文版) For those Chinese who need help about this: https://blog.birkhoff.me/macos-sierra-10-12-2-build-caffe/

@beiliubei

This comment has been minimized.

Show comment Hide comment
@tomasn4a

This comment has been minimized.

Show comment Hide comment
@brianscroggins24

This comment has been minimized.

Show comment Hide comment
@brianscroggins24

brianscroggins24 Feb 12, 2017

Has anyone used MKL as the math library, I can't seem to link it properly

Has anyone used MKL as the math library, I can't seem to link it properly

@alejandroposada

This comment has been minimized.

Show comment Hide comment
@alejandroposada

alejandroposada Apr 11, 2017

When I run python -c 'import caffe I get:

Traceback (most recent call last):
File "", line 1, in
File "/Users/alejandroposada/Library/Python/2.7/lib/python/site-packages/caffe/init.py", line 1, in
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/Users/alejandroposada/Library/Python/2.7/lib/python/site-packages/caffe/pycaffe.py", line 13, in
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver,
ImportError: dlopen(/Users/alejandroposada/Library/Python/2.7/lib/python/site-packages/caffe/_caffe.so, 2): Library not loaded: .5.dylib
Referenced from: /usr/local/lib/libcaffe.so.1.0.0-rc5
Reason: image not found

How can I solve this?
(make -j8 pytest 's result is OK)

alejandroposada commented Apr 11, 2017

When I run python -c 'import caffe I get:

Traceback (most recent call last):
File "", line 1, in
File "/Users/alejandroposada/Library/Python/2.7/lib/python/site-packages/caffe/init.py", line 1, in
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/Users/alejandroposada/Library/Python/2.7/lib/python/site-packages/caffe/pycaffe.py", line 13, in
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver,
ImportError: dlopen(/Users/alejandroposada/Library/Python/2.7/lib/python/site-packages/caffe/_caffe.so, 2): Library not loaded: .5.dylib
Referenced from: /usr/local/lib/libcaffe.so.1.0.0-rc5
Reason: image not found

How can I solve this?
(make -j8 pytest 's result is OK)

@liamcoau

This comment has been minimized.

Show comment Hide comment
@liamcoau

liamcoau May 14, 2017

I got

Liams-MBP:caffe lca$ brew install hdf5 opencv
Error: No available formula with the name "hdf5"

until I did brew tap homebrew/science

I got

Liams-MBP:caffe lca$ brew install hdf5 opencv
Error: No available formula with the name "hdf5"

until I did brew tap homebrew/science

@liamcoau

This comment has been minimized.

Show comment Hide comment
@liamcoau

liamcoau May 14, 2017

Also homebrew/versions is now deprecated

Also homebrew/versions is now deprecated

@ShivaliGoel

This comment has been minimized.

Show comment Hide comment
@ShivaliGoel

ShivaliGoel May 23, 2017

make: *** No rule to make target `all'. Stop.

I get this after running make -j8 all. Please help. Thank you !

make: *** No rule to make target `all'. Stop.

I get this after running make -j8 all. Please help. Thank you !

@FranDepascuali

This comment has been minimized.

Show comment Hide comment
@FranDepascuali

FranDepascuali Jun 17, 2017

If someone needs to run this version of caffe: https://github.com/weiliu89/caffe/tree/ssd
I've followed this guide and it works. Just make sure that:

  • hdf5 version = 1.10.1
  • protobuf version = 3.2.0_1

because brew downloads updated dependencies for those two

If someone needs to run this version of caffe: https://github.com/weiliu89/caffe/tree/ssd
I've followed this guide and it works. Just make sure that:

  • hdf5 version = 1.10.1
  • protobuf version = 3.2.0_1

because brew downloads updated dependencies for those two

@joyzhou28

This comment has been minimized.

Show comment Hide comment
@joyzhou28

joyzhou28 Sep 6, 2017

My MacBook's GPU is Intel Iris Graphics 6100, not cuda-based, so cannot run caffe with GPU on my mac? And there is no need to install cuda toolkit?

My MacBook's GPU is Intel Iris Graphics 6100, not cuda-based, so cannot run caffe with GPU on my mac? And there is no need to install cuda toolkit?

@chengsam

This comment has been minimized.

Show comment Hide comment
@chengsam

chengsam Sep 15, 2017

@ShivaliGoel Run the command in the caffe directory, i.e. cd caffe

chengsam commented Sep 15, 2017

@ShivaliGoel Run the command in the caffe directory, i.e. cd caffe

@adelaidepan

This comment has been minimized.

Show comment Hide comment
@adelaidepan

adelaidepan Dec 20, 2017

raffaelladeMacBook-Pro:Documents raffaella$ make -j8 all make: *** No rule to make target all'. Stop.

What should I do? I got this error.

adelaidepan commented Dec 20, 2017

raffaelladeMacBook-Pro:Documents raffaella$ make -j8 all make: *** No rule to make target all'. Stop.

What should I do? I got this error.

@haemi

This comment has been minimized.

Show comment Hide comment
@haemi

haemi Jan 5, 2018

brew tap homebrew/versions - deprecated
brew tap homebrew/science - deprecated

what to use instead?

haemi commented Jan 5, 2018

brew tap homebrew/versions - deprecated
brew tap homebrew/science - deprecated

what to use instead?

@doctorpangloss

This comment has been minimized.

Show comment Hide comment
@doctorpangloss

doctorpangloss Feb 24, 2018

@adelaidepan Run the command in the caffe directory, i.e., cd caffe.

Owner

doctorpangloss commented Feb 24, 2018

@adelaidepan Run the command in the caffe directory, i.e., cd caffe.

@doctorpangloss

This comment has been minimized.

Show comment Hide comment
@doctorpangloss

doctorpangloss Feb 24, 2018

@joyzhou28 You cannot run caffe with GPU on your Mac, and you do not need to install CUDA toolkit. Download the latest binary from their website if you don't need CUDA support.

Owner

doctorpangloss commented Feb 24, 2018

@joyzhou28 You cannot run caffe with GPU on your Mac, and you do not need to install CUDA toolkit. Download the latest binary from their website if you don't need CUDA support.

@AHEADer

This comment has been minimized.

Show comment Hide comment
@AHEADer

AHEADer Mar 20, 2018

dropbox failed....

AHEADer commented Mar 20, 2018

dropbox failed....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment