QuantLib installation on Mac OS X

Based on an initial version by Jack Harvard

Boost Installation

The preferred way to get Boost is through Homebrew (http://brew.sh/). By default, Homebrew will install Boost in /usr/local on older system versions and in /opt/homebrew on newer ones; you can find out which by running brew --prefix. From Terminal, run:

brew install boost

Alternatively, you can use MacPorts (http://www.macports.org/) which installs in /opt/local instead.

QuantLib Installation

Installation from Homebrew

If you don't need to modify the library, you might try to skip the compilation and install a precompiled binary version. Unofficial binaries are available from Homebrew; run

brew install quantlib

to install it. If you want to modify the library, instead, you'll have to compile it yourself as described in the next section.

Installation from a released version

Download QuantLib from its download page on GitHub, located at https://github.com/lballabio/QuantLib/releases. You want to download the tar.gz package (at the time of this writing, 1.29 is the latest version) and extract it by running

tar xzvf QuantLib-1.29.tar.gz
in Terminal. To install QuantLib, enter the folder you just created:
cd QuantLib-1.29

On Mac OS X 10.11 (El Capitan) and later, you'll need to pass additional environment variables to ./configure (thanks to Albert Azout for pointing it out). Run:

./configure --with-boost-include=/opt/homebrew/include/ \
            --with-boost-lib=/opt/homebrew/lib/ \
            --prefix=${HOME}/local/ \
            CXXFLAGS='-O2 -stdlib=libc++ -mmacosx-version-min=10.9' \
            LDFLAGS='-stdlib=libc++ -mmacosx-version-min=10.9'
(mind the backslash on the end of the lines; it tells the terminal to continue on the next line. You might also discard the backslash and write the whole command on a single line.) If your Boost installation is not in /opt/homebrew, change the paths above accordingly. The --prefix flag specifies where QuantLib will be installed; in this example, a folder local in your home folder, but you can pass a different one. For QuantLib 1.22 only, you will also need to add -std=c++11 to CXXFLAGS; later versions add their required -std flag automatically, while previous versions don't need it.

On Mac OS X 10.9 (Mavericks) and 10.10 (Yosemite), you'll need to pass different environment variables. Run:

./configure --with-boost-include=/usr/local/include/ \
            --with-boost-lib=/usr/local/lib/ \
            --prefix=${HOME}/local/ \
            CXXFLAGS='-O2 -stdlib=libstdc++ -mmacosx-version-min=10.6' \
            LDFLAGS='-stdlib=libstdc++ -mmacosx-version-min=10.6'
Again, for QuantLib 1.22 only, add -std=c++11 to CXXFLAGS and change your paths as needed.

On earlier systems, it's not necessary to pass most environment variables, so the command can be simplified to:

./configure --with-boost-include=/usr/local/include/ \
            --with-boost-lib=/usr/local/lib/ \
            --prefix=${HOME}/local/ \
Again, for QuantLib 1.22 only, add -std=c++11 to CXXFLAGS and change your paths as needed.

Also, if you only intend to build the QuantLib Python module, you might want to pass the --disable-shared option to ./configure to only build a static library and avoid problems with dynamic loading.

Finally, run:

to compile (you can use make -j4 or make -j8 to speed up compilation if you have multiple cores) and
make install
to install; if you specified a prefix outside your home folder, you might need sudo make install instead.

The above also installs a script quantlib-config that can be used to pass necessary flags to the compiler when using QuantLib. If you passed ${HOME}/local/ as the prefix, like in the example above, the script will be in ${HOME}/local/bin; you can add it to your executable path by running

export PATH="${PATH}:${HOME}/local/bin"
after which you should be able to run, for instance,
quantlib-config --cflags
and see a number of compilation flags printed. (Note that the new value of PATH will be reset when you close the terminal; see the documentation of your shell to see how to make it permanent.)

At this point, you can try to compile and run the examples. For instance,

cd Examples/BermudanSwaption
g++ `quantlib-config --cflags` BermudanSwaption.cpp -o bermudanswaption `quantlib-config --libs`
Note that you might have to add more flags to the command above: some users report it working as is, while others report having to add the same -stdlib and -mmacosx-version-min used in the compilation of QuantLib.