QuantLib-Python installation on Mac OS X

Installation from PyPI

If you don't need to modify the wrappers, you might want to try installing a precompiled binary version. The availability of binaries depend on your operating system; to try to install them, run:

  python -m pip install QuantLib

(If you have multiple versions of Python installed, run the above with the one you want to use QuantLib with.) If a binary package is available for your system, it will be installed and you will be able to leave this page and use it right away; if not, you'll have to compile it yourself as described in the next section.

Installation from a released version

The following assumes that you already installed QuantLib. Instructions for that are available at http://quantlib.org/install/macosx.shtml. In particular, check that you have provided the required options and environment variables to ./configure.

You can download released QuantLib-SWIG versions from GitHub at https://github.com/lballabio/QuantLib-SWIG/releases.

Once you have the tarball, extract it by executing:

tar xzf QuantLib-SWIG-1.22.tar.gz

(1.22 is the most recent version at the time of this writing; you might have downloaded another one, but take care to use one compatible with the version of QuantLib you installed.) This creates a folder QuantLib-SWIG-1.22; enter it and build QuantLib by executing:

On Mac OS X 10.11 (El Capitan) and later,

  cd QuantLib-SWIG-1.22/Python
  export CXXFLAGS='-O2 -std=c++11 -stdlib=libc++ -mmacosx-version-min=10.9'
  export LDFLAGS='-stdlib=libc++ -mmacosx-version-min=10.9'
  python setup.py build

On Mac OS X 10.9 (Mavericks) and 10.10 (Yosemite),

  cd QuantLib-SWIG-1.22/Python
  export CXXFLAGS='-O2 -std=c++11 -stdlib=libstdc++ -mmacosx-version-min=10.6'
  export LDFLAGS='-stdlib=libstdc++ -mmacosx-version-min=10.6'
  python setup.py build

On earlier systems,

  cd QuantLib-SWIG-1.22/Python
  export CXXFLAGS='-O2 -std=c++11'
  python setup.py build

Contrary to popular belief, working from a released tarball doesn't require you to have SWIG installed. After building, you can run

  python setup.py install
to perform a system installation; however, you might consider either passing an installation path, as in:
  python setup.py install --prefix=/your/desired/location
or building a wheel with
  python setup.py bdist_wheel
and use it for installation in the location you want; this will require the wheel package installed.

There are a couple of caveats to the above. The first is that the setup.py script will need to invoke quantlib-config (which was installed with QuantLib) to find out what other flags should be passed to the compiler and linker; they will also include the additional include directories you might have specified when you built QuantLib, so you'll be covered even if you have, say, Boost in a non-standard place. This means that quantlib-config must be in your path. If you installed QuantLib in /usr/local like Homebrew or in /opt/local like MacPorts, you should be already set up.

The second is that simply calling python as written above will find the system installation of Python. If you want to use a different one (for instance because you installed multiple versions of Python, or you want to use an Anaconda installation) you must call your chosen Python interpreter; for instance, if you have Python 3.9 installed as /Library/Frameworks/Python.framework/Versions/3.9/bin/python3, you'll have to run:

  CXXFLAGS='...' LDFLAGS='...' /Library/Frameworks/Python.framework/Versions/3.9/bin/python3 setup.py build

Once you're done, you can try to run a few examples to check your installation. To do this, you can execute:

  python setup.py test

Installation from a git repository

If you want to compile from a checkout of a git repository (such as the official one at https://github.com/lballabio/quantlib-swig, or a fork of it that you might have created) you'll need SWIG available; you can download and install it from http://swig.org or, again, get it packaged from Homebrew or MacPorts.