Learning to be Giant.

Installation of libsvm for Matlab R2012b on OS X 10.9 with Xcode 5.0


Tutorials on installation of libsvm for matlab on mac could be fetched easily on the internet. However, most of them did not mention some problems that could be met during the installation. Therefore, I’d like to make a more comprehensive version of tutorial.

My environment is like:

Mac OS X 10.9
Xcode 5
Matlab R2012b

Step 1.Preparation

Of course, you have to download libsvm from its official site: http://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html. There is no versions specially for matlab, so, just download the package.

You could unzip the package to any directory you like, but I’d prefer to unzip it to (matlabroot)/toolbox/libsvm. ## Step 2.Make Compile After preparation, launch matlab and go to the directory you unzipped the files to. For me, it will be like:

cd (matlabroot)
cd toolbox/libsvm/matlab

Now, we need to compile. Just type the following command:


If everything goes right, just wait for a few seconds, it will be done and nothing will be displayed. You will find some files with the suffix .mexmaci64.

Unfortunately, I went into trouble during the process.

Step 3.Troubleshooting

If anything goes wrong, try the following command first:

mex -setup

This one let you to choose a proper compiler. Depending on the environment, things like following will be prompted to the screen:

The options files available for mex are:
1: /Applications/MATLAB_R2012b.app/bin/mexopts.sh :
   Template Options file for building gcc MEX-files
0: Exit with no changes

Enter the number of the compiler (0-1):
1 <span style="color: #008000;">%Notice, you should choose one according to your wish</span>

Overwrite /Users/Usr/.matlab/R2012b/mexopts.sh ([y]/n)?

After all this, try Step 2 again. If it doesn’t work, read on.

Well, in fact, after typing make into the command line, I met the following error message:

/Applications/MATLAB_R2012b.app/bin/mex: line 305: gcc-4.2: command not found
/Applications/MATLAB_R2012b.app/bin/mex: line 1326: gcc-4.2: command not found

    mex: compile of ' "libsvmread.c"' failed.

If make.m fails, please check README about detailed instructions.

According to the MathWork,

/Applications/MATLAB_R2011b.app/bin/mex: line 305: gcc-4.2: command not found
This is happening because MATLAB is expecting GCC 4.2 to be present. This compiler was distributed with Xcode 4.0 and 4.1, but is no longer supplied as of Xcode 4.2. However, Xcode 4.2 and later include a similar compiler (GCC 4.2 front-end to LLVM) that MATLAB can be instructed to use instead. Applying the patch attached to this solution will instruct MATLAB to use the compiler supplied with Xcode 4.2 or later.

And you should find the patch and its installation instructions here: http://www.mathworks.com/matlabcentral/answers/94092

You may notice that in the support article above, it is said that the solution is only for Xcode 4.2~2.4 with OS X 10.6~10.8. But, in fact, even if you are in OS X 10.9 with Xcode 5, the problem is aroused for the same reason and the solution should be the same. Therefore, don’t worry if you are not in the environment mentioned in the article above.

Meanwhile, there is another issue mentioned in the article that you may encounter during the installation process:

 /Applications/MATLAB_R2012a.app/extern/include/matrix.h:852:20: error: stdlib.h: No such file or directory

Step 4. Testing

As everything goes right now, we should test if libsvm works fine. Download the heart_scale dataset from here: http://www.csie.ntu.edu.tw/~b91082/SVM/ and move it into your current work directory. Then:

>> load heart_scale
>> model = svmtrain(heart_scale_label,heart_scale_inst,'-c 1 -g 0.07');
optimization finished, #iter = 134
nu = 0.433785
obj = -101.855060, rho = 0.426412
nSV = 130, nBSV = 107
Total nSV = 130
>> [predict_label, accuracy, dec_values] = svmpredict(heart_scale_label, heart_scale_inst, model);
Accuracy = 86.6667% (234/270) (classification)

If your output match the output above, then congratulations, you could use the libsvm now.

Some Further Info

If you succeed the step 4, you could use libsvm. However, if your work directory doesn’t contain the .mexmaci64 files, you may find the svmtrain and svmpredict works a little bit unexpectedly. That is because the libsvm is not in your search path and matlab automatically called the original system svmtrain/svmpredict. What you should do is to add the libsvm to your search path by typing:


and add the libsvm/matlab to the path.

Meanwhile, if you try to use help or doc commands to get the help information about the two functions, what you will get are the documentations of the system implementation of the two functions instead of libsvm version. And the system version could be not so easy to use. If you want to check the documentation of libsvm, just refer to the README file in the libsvm/matlab directory.

OK, that’s all about it. This is the first time I write a so called “tutorial” and I do hope it will do a little bit help to you.