COBRA on Matlab 64

From Symbiopedia
Jump to: navigation, search

Last Edited: --Ben 14:01, 24 August 2012 (PDT)

NOTE: These instructions still work for all the versions indicated, but Gurobi has recently released version 5. Gurobi 5 includes a matlab interface, so compiling and installing gurobi_mex won't be required for using gurobi with COBRA. See COBRA 2.0.5 on Matlab 64 for updated instructions

These notes are for the 64 bit version of Matlab R20011a, on Windows 7 Professional edition. If you are using any other platform, or the 32 bit version of Matlab, you'll need to do some different things, though some of these notes may help point you in the right direction.

Even in Win 7, there are other ways to get this software working, but this is what worked for me. Your paths may be different, your preferred toolbox directories might be different. But, if you get things working in the order presented here, it may work for you, too.

Here are my install notes for:

which requires the installation of:

  • libSBML with the MATLAB/Octave binding
  • a working matlab 64 bit compiler to build the gurobi mex file (Optional - if you want to use optknock)

Note: compared to earlier versions, the install process for this software has advanced quite a bit (particularly libSBML). Most of the pain now is due to the fact that Matlab 64 doesn't come with a compiler to build .mex files, so you need to install one if you want to use gurobi. If you don't need to do things like optknock or MOMA, the default install works well for FBA, and the compiler stuff described below is not required.

This process has become much less painful than in the past. There is still room for improvement on the COBRA install software (specifically, initcobratoolbox does not handle the windows 7 / Matlab user access control approach as gracefully as libSBML, so has to be run each time matlab starts), and room for improvement with gurobi_mex (I wish they distributed binaries!), but many thanks go to the people involved with developing libSBML/SBML Toolbox, OpenCOBRA, Gurobi, and Gurobi_mex.


  1. Get COBRA working for all users on the computer
  2. read/write SBML files for use with COBRA
  3. use OptKnock


  • Windows 7 Professional edition
  • 64 bit version of Matlab R20011a
  • Administrator access to the Windows box


Get the COBRA toolbox

  • Download cobra 2.0.3
  • start matlab as admin (right click on the matlab icon, select "run as administrator").
  • test the download:
    • in matlab, navigate to Cobra_Install_Path (the cobra directory that you just downloaded) and run initCobraToolbox.
      It should run, but you will get errors.
      note: glpk works! That's a great improvement over previous versions!
      note 2: Although sbmltoolbox is in \cobra_2.0.3\cobra\external\toolboxes\SBMLToolbox-4.0.1\, that doesn't get added to path by initcobratoolbox. Also, this isn't really how I want my toolboxes set up.

So, let's clean it up a bit (I like to put toolboxes that I install in $MATLABROOT\toolbox\installed\), and get the SBML toolbox working so we can read and write SBML.

  • Quit matlab.
  • Copy the cobra_2.0.3 folder from your download location to C:\Program Files\MATLAB\R2011a\toolbox\installed (or wherever you want to install this toolbox). note:this requires administrator privileges

Install libSBML

Looking at the SBML Toolbox readme, it says "IMPORTANT: You must have installed libSBML with the MATLAB/Octave binding prior to installation of SBMLToolbox."

So, let's get libSBML version 5.1.0-b0 with the matlab binding working.

note: I only plan to use libSBML with matlab (and not python, or whatever other bindings are available).

  • test the libSBML install
    • in matlab, type which translateSBML
      I got C:\Program Files\SBML\libSBML-5.1.0b0-libxml2-x64\bindings\matlab\matlab\TranslateSBML.mexw64
    • Next, restart matlab (as normal user, not as admin), and test again to check that paths were saved - it works

neat, so libSBML is working. Next, the SBML toolbox.

Install the SBML toolbox

  • restart matlab (as admin)
  • In Matlab, cd to C:\Program Files\MATLAB\R2011a\toolbox\installed\cobra\external\toolboxes\SBMLToolbox-4.0.1\toolbox\ (or wherever your COBRA toolbox directory is)
  • install the SBML Toolbox by typing install. It should say its successful.
  • quit matlab, restart as normal user.
  • double check that path is okay (type which matchName - if matlab finds it, things are good)

Install the COBRA toolbox

  • in matlab, cd to C:\Program Files\MATLAB\R2011a\toolbox\installed\cobra
  • type initCobraToolbox (I get some warnings about non-installed solvers. That's okay at this point.)
  • check if the COBRA toolbox is working
    • type testAll (At this point, read/write SBML should work, but most things that involve other solvers shouldn't because only have GLPK - for me, 14 of 19 tests passed, and the 5 that didn't all used TomLab)

At this point, if you should be set to use FBA (optimizecbmodel). You only need to install a different solver (such as Gurobi) if you want to do other functions, such as C13Fitting, GDLS, MOMA, or OptKnock. I do want to use optknock, so I installed gurobi, as described below.

Install Gurobi

To get Gurobi working with the COBRA toolbox, you need Gurobi (a standalone program), and gurobi_mex (a translator that makes Gurobi available to Matlab. You have to compile gurobi_mex from within matlab, but matlab x64 doesn't come with a compiler, so you need to get one.

Get Gurobi working

You'll need to get Gurobi. There is a free academic license available.

Note: the strikethroughs below are because the academic Gurobi license doesn't really allow for use by all users on the Windows box. Each user has to have their own license.

  • download gurobi (I am using version 4.5.1).
  • To install it for all users, follow these instructions. Specifically, instead of just double clicking on the .msi file to install gurobi, run msiexec /i Gurobi-4.5.1-win64.msi allusers=1 from the windows command prompt.
  • Install the license
    • type grbgetkey in the windows start menu, then right click to run as admin.
      • Enter your key, then install it to C:\gurobi451. - hopefully any user on the system can run it now.
  • Test gurobi
    • start the gurobi interactive shell from the desktop icon. If it starts, Gurobi is working. You can quit it once you see it works.

Get a compiler working with matlab x64

Mathworks has a good tutorial. It has screenshots, unlike these notes. They also supply the compiler and SDK for download.

I didn't find the mathworks tutorial until after I had done the following, which basically does the same thing as the mathworks tutorial.

The free (as far as cost) compiler I used is Microsoft Visual C++ 2010 Express Edition.

However, the free version doesn't include 64-bit tools (the Microsoft tech notes say "To enable 64-bit tools on Visual C++ Express, install the Windows Software Development Kit (SDK) in addition to Visual C++ Express.")

  • Download and install MSVC++
  • Download and install the Windows SDK for Windows 7 and and .Net Framework 4 (I used version 7.1) - this takes a while. I accepted defaults when given a choice, and added in any components that looked worthwhile (sorry, I didn't make a note of which ones - if in doubt, add everything: disk space is cheap).
  • restart matlab (as a normal user should be fine, but I'm not sure here - if it doesn't work, first try starting matlab as an admin. If that's required, email me to fix this step.)
  • make sure that your matlab compiler is set up correctly:
    • in matlab, type mex -setup
    • allow matlab to autodetect compilers. It should find:

[1] Microsoft Visual C++ 2010 Express in C:\Program Files (x86)\Microsoft Visual Studio 10.0\

now we're ready to try to compile gurobi mex.

Get and compile gurobi_mex

  • download gurobi_mex
  • in matlab, change to the gurobi mex directory, and compile gurobi with a command like:
    mex -O -largeArrayDims -I"C:\Gurobi451\win64\include" "C:\folder\gurobi_mex.c" "C:\Gurobi451\win64\lib\gurobi45.lib" "C:\Program Files\MATLAB\R2011a\extern\lib\win64\microsoft\libut.lib"
    (note, replace "folder" with path to the gurobi_mex folder).
    This command will create a file called gurobi_mex.mexw64.
  • restart matlab (as a normal user) to update environmental variables
  • Test gurobi_mex
    • in matlab, cd to the gurobi mex folder
    • run the supplied test scripts (such as test_gurobi_mex_LP)
  • if all's well, quit matlab - we can move gurobi to where we want
    • I copied gurobi_mex to the Cobra toolbox extern folder, C:\Program Files\MATLAB\R2011a\toolbox\installed\cobra\external\gurobi_mex_v1.55\
  • Next, test gurobi with COBRA
    • in matlab, change to the COBRA directory (ie, C:\Program Files\MATLAB\R2011a\toolbox\installed\cobra)
    • type initcobratoolbox. I got some errors, but also see:
      LP solver set to gurobi successful
      MILP solver set to gurobi successful

Modify matlab startup and initcobratoolbox

For convenience, I edited the matlab startup script and initcobratoolbox so errors go away and the path is updated correctly whenever matlab starts.

Before doing this, I had the following errors when I ran initcobratoolbox:

Warning: QP solver CPLEX through Tomlab not usable: tomRun.m not in Matlab path 
> In changeCobraSolver at 181
  In initCobraToolbox at 67
Warning: qpng solver has not been fully tested - results may not be correct 
> In changeCobraSolver at 191
  In initCobraToolbox at 67
QP solver set to qpng successful
Warning: MIQP solver CPLEX through Tomlab not usable: tomRun.m not in Matlab path 
> In changeCobraSolver at 220
  In initCobraToolbox at 78
MIQP solver set failed
CB map output set to svg successful

To make them go away I did the following:

  • modify initcobratoolbox lines 67 and 78 to be:
    QPsolverOK = 0;%changeCobraSolver(char(CobraQPSolver),'QP');
    MIQPsolverOK = 0; %changeCobraSolver(CobraMIQPSolver,'MIQP');

last, so that I don't have to type initcobratoolbox every time I want to use the cobra toolbox, I added 'C:\Program Files\MATLAB\R2011a\toolbox\installed\cobra\initCobraToolbox' to startup.m (which I created in MATLAB_DIRECTORY/toolbox/local/startup.m -I had to copy startupsav.m to startup.m to make this file, and then start matlab as admin to edit the file)