Back of the Envelope

Observations on the Theory and Empirics of Mathematical Finance

[CompFin] Installing QuantLib and QuantLib-Python in Windows

with 13 comments

In our recent paper introducing QuantLib, we had an appendix which described how to set both QuantLib and QuantLib-Python up for use in Ubuntu and Windows. Since I rarely use Windows these days, for instructions for Windows we mostly relied on what was available on the QuantLib website and answers to relevant questions on stackoverflow.

When I got my hands on a Windows laptop recently, to my embarrassment I realized I could not use those instructions to get QuantLib and QuantLib-Python working on a Windows 7 machine. Needless to say this needed sorting out, and now it has been.

I’ll have the paper updated on our website shortly, but in the mean time here are the instructions for installing QuantLib and QuantLib-Python in Windows. These have been tested on three different Windows machines, so we are reasonably sure they work. However, if something doesn’t work for you, do drop in a comment.

Step-by-step by instructions for installing QuantLib and QuantLib-Python in Windows

  1. QuantLib requires a working C++ Environment, and for the purposes of instructions here (meant for a novice), it is recommended that the user install the free Microsoft Visual Express Desktop 2013 edition which comes with Visual C++ 12 (MSVC12).
  2. QuantLib-Python requires a working Python environment, and we recommend working with the Anaconda meta-package. Instructions are available from the Anaconda install page. For our purpose here, Windows users should work with the 32-bit version of Anaconda. It is recommended to install Anaconda with all the default settings, and update it by running \texttt{conda update conda} and \texttt{conda update anaconda}. Again, it does not matter if one installs Anaconda-2.x (with Python 2.7) or Anaconda-3.x (with Python 3), but it is recommended that the user install the Python 3 version, as that’s where the Python language seems to be headed.
  3. Compiling QuantLib requires Boost C++ libraries installed. And this is where the utility of working with MSVC becomes most apparent. Pre-packaged binaries for Boost are available for specific versions of MSVC (in our case MSVC12) from its sourceforge page, and installing Boost on MSVC is no more difficult than installing any other software in Windows. (Users should download the executable for the 32-bit architecture, as the free version of MSVC does not seem to contain the necessary utilities for compiling in 64-bit. There is no such problem if you have access to a professional edition of MSVC though.)
  4. After installing Boost, we need to install SWIG. Pre-packaged binaries for SWIG for Windows are available from its source page (latest version 3.0.5), and it is enough to extract the SWIG zip file in a convenient folder.
  5. Download the same version of QuantLib and QuantLib-Python (latest version 1.6)
  6. Launch the Visual Studio command prompt: All the commands below assume that we are working in Visual Studio command prompt (or if not, all the relevant Visual Studio related environment variables have been set). It can be launched from the Start menu (in Windows 7) or from Apps (in Windows 8). If you can’t find it, see here.
    • NB: Though we haven’t encountered any issue when using MSVC12, as pointed out by Andres in comments, when working with VS 2015, it might be safest to work with x64 Native Tools Command Prompt.
  7. After the above steps, installing QuantLib in Windows simply requires the following steps (mind the gaps):
     cd \path\to\QuantLib-1.6
    set myboost=\path\to\boost\boost_1_58_0
    msbuild /p:AdditionalLibPaths="%myboost\lib32-msvc-12.0" /p:Configuration=Release /p:Platform=Win32 QuantLib_vc12.sln 

    The last step above may take a while (on an Intel i5, Windows 7 machine with 4 GB RAM it took almost 45 minutes).

    Note that throughout in this post \texttt{\textbackslash path\textbackslash to\textbackslash someplace} represents the directory of \texttt{someplace}. For example, if \texttt{boost\_1\_58\_0} is installed in \texttt{C:\textbackslash boost}\texttt{\textbackslash path\textbackslash to\textbackslash boost\_1\_58\_0} should be taken to mean \texttt{C:\textbackslash boost\textbackslash boost\_1\_58\_0}.

  8. One should ensure that QuantLib is correctly installed in the previous step, as what follows assume a working QuantLib. If it is correctly installed, you should see something like the following message at some stage before the installation completes:
     Tests completed in 16m 50s
    Test suite "Master Test Suite" passed with:
    1002021 assertions out of 1002021 assertions passed
    609 test cases out of 609 passed 

    And at the very end:

     Build succeeded.
    0 Warning(s)
    0 Error(s)
    Time Elapsed 00:44:20.07
  9. Although not absolutely necessary, to ensure that all is well, it is recommended to also build QuantLib in Debug mode (replace ‘Release’ by ‘Debug’ in the \texttt{msbuild} command in step 7 above; and will take roughly half the time it took to build the Release version), and test the code in the QuantLib Usage section on the QuantLib install page for Windows.
  10. Installing QuantLib-Python requires a few extra settings. At the Visual Studio command prompt, set the following (again, mind the gaps):
     cd \path\to\QuantLib-SWIG-1.6\Python
    set PATH=\path\to\Anaconda3;\path\to\Anaconda3\scripts;\path\to\swigwin-3.0.5;\path\to\QuantLib-1.6\lib;%PATH%
    set QL_DIR=\path\to\QuantLib-1.6
    set VS100COMNTOOLS=%VS120COMNTOOLS%
    set INCLUDE=\path\to\boost_1_58_0;%INCLUDE%
    set LIB=\path\to\boost\lib32-msvc-12.0;%LIB%
    echo [build] > setup.cfg && echo compiler=msvc >> setup.cfg 
  11. Once the environment variables have been set in step 10 above, all we need is to run the following steps:
     cd \path\to\QuantLib-SWIG-1.6\Python
    python setup.py wrap
    python setup.py build
    python setup.py test
    python setup.py install 

    If you are wondering how would you know that QuantLib-Python has been installed correctly, after the \texttt{python setup.py test} step, you should see a message something like this:

    ---------------------------------------
    Ran 36 tests in 1.253s
    Ok
  12. To be doubly sure, you should now go to Luigi Ballabio’s QuantLib page and run the examples there.

My co-author has a more detailed post on his blog discussing this, and in addition to providing motivation for some of our choices, he also describes how to install QuantLib and QuantLib-Python using only open-source tools (for “free as in free speech”) and without using Visual Studio. Highly recommended.

Update (8th July): Paper updated with these instructions.

Advertisements

Written by Vineet

July 7, 2015 at 2:16 am

13 Responses

Subscribe to comments with RSS.

  1. Thank you very much.
    I followed your tutorial,and it worked for me at first.
    But,when I type ‘import QuantLib’,I get the following

    D:\quantlib-swig-1.7\python> python
    .
    .
    .
    >>>import QuantLib
    .
    .
    .
    File “QuantLib\QuantLib.py”, line 20, in swig_import_helper
    import QuantLib
    ImportError: No module named _QuantLib

    where is _QuantLib, what should I do next?

    xuruilong100

    December 8, 2015 at 9:46 pm

    • Sorry for the late reply, but it has been bit of an ‘off time’. But I guess Luigi’s response on the quantlib-users forum has already helped resolve the issue 🙂 Pls let me know if you encounter any other issue, and I’ll try and check at my end too.

      Vineet

      December 30, 2015 at 7:29 pm

    • Just to add: While the post doesn’t discusses installation without anaconda, you might find the discussion on my colleague’s blog useful.

      Vineet

      December 30, 2015 at 7:48 pm

  2. On the 7th step cmd writes: “msbuild” is not an internal on external command
    after

    msbuild /p:AdditionalLibPaths="%myboost\lib32-msvc-12.0" /p:Configuration=Release /p:Platform=Win32 QuantLib_vc12.sln

    Grigoriy Tarasov

    April 8, 2016 at 4:14 am

    • Did you correctly edit your PATH environment variable?

      Vineet

      August 12, 2016 at 10:49 pm

  3. Vow, QuantLib must be pretty cool stuff, I will try using it. 🙂

    Quasar

    May 30, 2016 at 6:12 am

  4. I would just add that it is important to select the proper Visual Studio command prompt. At least in Visual Studio 2015, there are several command prompts available, and the first one “Developer Command Prompt” has the %LIB% variable pointing to the x86 architecture libs. However, inside stup.py there is an if case that checks the system directly, and if the system is 64bits, it will try to compile for x64 architecture.

    This is easily solved if one starts the “x64 Native Tools Command Prompt” instead.

    Andres Hernandez

    June 18, 2016 at 1:58 pm

    • It’s been a bit of an off time on the blog, but thanks Andres, and agree with you. Will modify Have modified the post to reflect that.

      Vineet

      August 12, 2016 at 10:36 pm

  5. Hi! Could you help me to install QuantLib for Python and/or R? I have the following problem:
    SWIG(1) : Warning 125: Use of the include path to find the input file is deprecated and will not work with ccache

    P.S. I use Windows 10 x64, SWIG 3.0.10, boost 1.60, Visual Studio 2015

    Thank you

    Roman

    July 5, 2016 at 1:38 pm

    • Sorry for the delay in responding. I am not an active Windows user, but when we tried on three diff. machines, we did not see this error. Let me get hold of a Windows machine again, and I’ll try again. It would help if you could list down your exact steps, so we could try and see where exactly you are stumbling.

      Vineet

      August 12, 2016 at 10:38 pm

  6. Hi, thanks a lot for this tutorial, it looks great, however I’m a bit stuck at this line
    msbuild /p:AdditionalLibPaths="%myboost\lib32-msvc-12.0" /p:Configuration=Release /p:Platform=Win32 QuantLib_vc12.sln

    I’m getting the following error and can figure out what wrong, maybe you can help me?

    MSBUILD : error MSB1011: Specify which project or solution file to use because this folder contains more than one project or solution file.
    ‘quot’ is not recognized as an internal or external command,
    operable program or batch file.
    ‘quot’ is not recognized as an internal or external command,
    operable program or batch file.

    I tried quite a bit around, but given my little knowledge, I can’t seem to figure the problem out 😦
    Any help would be very appreciated!
    Thanks,
    Tobias

    Tobias

    August 1, 2016 at 10:56 pm

    • Sorry for the delay in responding and you may have already got a response by now. If not, did you try by uninstalling/deleting all previous instances of QuantLib (inc. any leftovers in the reg)? When in a crunch, and when the main purpose is to get the thing installed, in Windows that does seem to do the trick sometimes (and often much quicker than finding out why something isn’t working). If you tried that too, let me know the exact sequence of your steps and I would try and replicate at my end and see if I can help.

      Vineet

      August 12, 2016 at 10:46 pm

  7. Hi, I’m getting ‘build failed’ error. …\QuantLib-1.9\QuantLib.vcxproj”(default targets) –FAILED.
    also getting same message for …\QuantLib_vc14.sln

    TRACKER: error TRK0005: Failed to locate: “CL.exe”. The system cannot find the file specified file”…\QuantLib.vcxproj

    however I see the vcxproj file but not CL.exe.

    any idea what this could be?

    miko

    January 24, 2017 at 2:31 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: