QuantLib-Python installation on Mac OS X
Installation from PyPI (recommended)
If you don't need to modify the wrappers, you can install a
precompiled wheel from PyPI. Once you have activated the Python
environment you want to use, make sure you have the latest version
of pip
by running:
python -m pip install -U pip
(note that your Python interpreter might be named python3
instead) and then run:
python -m pip install QuantLib
If a wheel is available for your system (which is likely) 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 are available at https://www.quantlib.org/install/macosx.shtml.
Setup
Before compiling the wrappers, you'll need to set up an appropriate environment:
- Some of the commands you'll run will need to
invoke
quantlib-config
(which was installed with QuantLib) to find out what 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 you must ensure thatquantlib-config
is in your executable path. - You'll need to install a few Python modules. It's good practice
to install them in a new virtual environment, to avoid possible
conflicts with your system Python installation. This also helps
in case you have multiple versions of Python on your machine. You
can create the virtual environment with
python3 -m venv .venv
and activate it with. .venv/bin/activate
(mind the single dot at the beginning of the line.) Once the environment is active, runpip install setuptools build tox pytest
If you're usingconda
to manage your environments, you know what to do instead. - You might have to set up a couple of environment variables:
on Mac OS X 10.11 (El Capitan) and later,export CXXFLAGS='-O2 -stdlib=libc++ -mmacosx-version-min=10.9' export LDFLAGS='-stdlib=libc++ -mmacosx-version-min=10.9'
on Mac OS X 10.9 (Mavericks) and 10.10 (Yosemite),export CXXFLAGS='-O2 -stdlib=libstdc++ -mmacosx-version-min=10.6' export LDFLAGS='-stdlib=libstdc++ -mmacosx-version-min=10.6'
on earlier systems,export CXXFLAGS='-O2'
- Contrary to popular belief, working from a released tarball doesn't require you to have SWIG installed.
Compilation
You can download released QuantLib-SWIG versions from GitHub at https://github.com/lballabio/QuantLib-SWIG/releases. You should download the same version as the version of QuantLib you installed; for the sake of example, I'll use version 1.36 which is the most recent version at the time of this writing.
Once you have the tarball, extract it by executing
tar xzf QuantLib-SWIG-1.36.tar.gzThis creates a folder
QuantLib-SWIG-1.36
;
enter it and configure QuantLib by executing:
cd QuantLib-SWIG-1.36 ./configure
After configuration, you can run
cd Python make
This will create a binary wheel in the dist
directory.
To check that it works, run
make check
from the Python
directory (you should already be there).
Once you're done, you can take the wheel and install it
with pip
in the environment of your choice.
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 an additional
step at the beginning of the process. Before running
the ./configure
script, you'll have to create it by
executing
./autogen.sh
To do this, you'll need automake, autoconf and libtool; you can get them packaged from Homebrew or MacPorts.
After the execution of ./autogen.sh
, the installation
proceeds as in the previous section. Note, though, that in this case
you'll need SWIG available; you can download and install it
from http://swig.org. At the time of
this writing, the wrappers use SWIG 4.3.0.