Friday, July 8, 2011

Installing mlabwrap on OS-X and Linux

Mlabwrap is an excellent Python wrapper for Matlab, allowing you to use Matlab functionality from within Python scripts. I recently tried to install it on OS-X (10.6) and Linux (11.4). I ran into minor issues with both OS'.

The first is that the that ships with version 1.1 of mlabwrap has a minor compile error related to the definition of a datatype. In my case this was fixed by changing:

# line 13


which tells mlabwrap to look for Matlab versions 7.3 and on. I made this change on both OS-X and Linux.

The second issue was with OS-X only, and relates to how mlabwrap references the Matlab shared libraries. Although mlabwrap builds successfully, import errors related to "@loader_path/libeng.dylib" and "@loader_path/libmx.dylib" occur.

I fixed this using the "install_name_tool" command to modify the library from mlabwrap so that it uses the absolute paths to these shared libraries:

The commands are:
sudo install_name_tool -change "@loader_path/libmx.dylib" "/Applications/Matlab_R2011a_Student/bin/maci64/libmx.dylib"

sudo install_name_tool -change "@loader_path/libeng.dylib" "/Applications/Matlab_R2011a_Student/bin/maci64/libeng.dylib"

Note that these commands assumes you are in the site-packages directory of your Python installation (or wherever you have install mlabwrap). You will also need to change the change the "/Matlab_R2011a_Student" portion of the paths to suit your Matlab installation.


Anonymous said...

hi James

I am using a OS-X 10.6 and I successfully installed mlabwrap following your suggestions. Post installation I did:
sudo install_name_tool -change "@loader_path/libmx.dylib" "/Applications/Matlab_R2010a/bin/maci64/libmx.dylib" /Library/Python/2.6/site-packages/

sudo install_name_tool -change "@loader_path/libeng.dylib" "/Applications/Matlab_R2010a/bin/maci64/libeng.dylib" /Library/Python/2.6/site-packages/

however, when I 'from mlabwrap import mlab', I get:
Traceback (most recent call last):
File "", line 1, in
File "", line 188, in
import mlabraw
ImportError: dlopen(/Library/Python/2.6/site-packages/, 2): Library not loaded: /Applications/Matlab_R2010a/bin/maci64/libeng.dylib
Referenced from: /Library/Python/2.6/site-packages/
Reason: image not found

Is there anything thing that you can suggest I check out?
Rethabile Khutlang

James Gregson said...


Sorry for the delay in responding. I would check that the file 'libeng.dylib' exists in the path /Applications/Matlab_R2010a/bin/maci64/libeng.dylib, if not then that is likely your problem. However I didn't encounter this issue myself so it's hard to be more helpful than that.

Good luck,


Rethabile Khutlang said...

Hi James

It works! I checked that file 'libeng.dylib' existed and it did. Then when 'import mlabraw' for the first time, it wanted matlab license and it worked.


student said...

Hi Jmaes,

I also followed your approach, and changed the path to libeng.dylib, libmat.dylib, and libut.dylib.

But at the end, I receive the following error which I am not sure how to fix it:

Library not loaded: libtbb.dylib
Referenced from: /Applications/
Reason: image not found

Do you have any idea how to solve this problem?

Thanks a lot

Benjamin Reedlunn said...

Hi James,

Thanks for the instructions. Your suggestion to edit the file helped me install mlabwrap. However, I found I did not have to use the install_name_tool to point to the .dylib files. The mlabwrap website ( recommends running

>export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH$:/Applications/MATLAB701/bin/mac/
>export MLABRAW_CMD_STR="/Applications/MATLAB701/bin/matlab -nodesktop"

prior to running matlab. In my install of Matlab 2010b, the mac directory does not exist, but maci64 does. So I added

>export MLABRAW_CMD_STR="/Applications/ -nodesktop"

to my .bash_profile. This did the trick. When I import mlabwrap I don't run into any errors.

Thanks for your help. I probably would not have figured this out without your blog posting.