Getting LIBSVM to work with Octave on Windows

This post will help you get to the point where you can invoke LIBSVM commands from Octave, on Windows. I did this with an x64 build of Windows 8, Octave 3.6.2 x64, and LIBSVM 3.13. Your mileage may vary.

According to the LIBSVM FAQ, Octave support has been available since version 2.86 (of LIBSVM), and all you have to do was type make inside Octave. The README file in the LIBSVM matlab subfolder adds that prebuilt binaries  are included only for 64-bit Matlab on Windows. Those indeed did not work with Octave. There are instructions on building ‘libsvmread.mex’, ‘libsvmwrite.mex’, ‘svmtrain.mex’, and ‘svmpredict.mex’, but those didn’t work for me right out of the box, and it wasn’t clear what to do with the .mex files once they’re ready.

The LIBSVM distribution includes a matlab subfolder. This folder includes 5 C source files and 1 header file that together define an interface for Matlab and Octave to invoke LIBSVM. LIBSVM itself was compiled from C source code, available elsewhere in the LIBSVM folder structure. Neither Matlab nor Octave can invoke C code directly. It has to be compiled first into a .mex file (or files), which Matlab and Octave know how to deal with. The format and content of .mex files are highly dependent on the operating system, the processor architecture of Matlab/Octave (e.g. x86 or x64), and whether they are intended for use by Matlab or Octave. A .mex file built for use in Matlab, for example, will not work Octave.

At this point it should be clear that to invoke LIBSVM from Octave, we will have to compile C source code to .mex files for our version of Octave. Once the .mex files are available, we will have to put them somewhere Octave will know to look for them in. Compilation of C source code requires a C compiler, and we will use the compiler in Microsoft’s Visual Studio.

Here are the steps I followed:

  1. Make sure you install the Visual Studio version of Octave. I got my copy of octave-3.6.2-vs2010-setup.exe at the SourceForge download page and installed with the default options.
  2. Make sure you have a copy of Visual Studio 2010 or 2012. The professional versions of  VS2010 and VS2012 both worked fine. If you don’t have a copy, the Express version should be available for free from Microsoft (I didn’t try it though — let us know if it works!)
  3. Download the LIBSVM zip file and expand it in some folder. You can find the zip in the LIBSVM landing page — search for “download”.
  4. Open a command prompt window with Run As Administrator. Run vcvarsall.bat from the Visual C++ directory. This updates your path and adds environment variables to allow you to run the Visual C++ compiler from the command line:
    Executing vcvarsall.bat
  5. In the same command prompt window, add the Octave bin folder to your path. This will allow Octave to find the tools it needs to create .mex files. In particular, those tools include mkoctfile.exe and cc-msvc.exe:
    Add the Octave binaries folder to your path
  6. Edit Octave’s math.h file to reference the correct location of Visual Studio’s math.h. On my machine, the file to change is c:\Program Files (x86)\Octave-3.6.2\include\math.h. As of LIBSVM 3.13, the offending reference is at line 74. Octave’s math.h has a hardcoded reference to c:/Program Files/Microsoft Visual Studio 10.0/VC/include/math.h. Change it, if needed, to the correct location on your machine. On mine, it had to be changed to c:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/include/math.h.
    Note that you will have to use an editor that understands Unix-style newlines. Windows’ notepad doesn’t, and renders the file as one very long line. I used notepad++. You could also use Visual Studio’s editor to make the change. Another consideration is that you will need administrative privileges to save the modified file. One way to obtain those is to launch the editor from your Run-As-Administrator command prompt.Edit Octave's math.h
  7. Still in the same command prompt, start Octave and navigate to the matlab subfolder of LIBSVM:
    Start Octave and navigate to the matlab subfolder of LIBSVM
  8. In Octave, and when in the matlab subfolder of LIBSVM, type make. This executes the make.m script in the current folder, which builds the .mex files:
    Type 'make' inside Octave to execute make.m
  9. If all went well, the .mex files should now be present in the current directory. You can either leave them there and add the directory to the Octave search path (use the addpath command inside Octave), or move them to a folder Octave already to knows to search in. I did the latter:
    Copy the .mex files to a folder Octave knows to search in
  10. You’re almost done. Start Octave and try the svmtrain command:
    The svmtrain command is now available in Octave

This is as far as I got at this point. svmtrain appears to be available for calling from Octave. I did not try to generate a model from data just yet — I’ll update as soon as I’m confident that works as well.