Matlab is one of the widely used product used for prototyping in data mining and machine learning. I am not a big fan of Matlab but it is very convenient for coding some algorithms. This post is a discussion about my experience in installing, running Matlab , debugging and compiling Matlab extensions in a 64 bit Ubuntu system.
One of the biggest problems I faced is that the student version of Matlab does not have a 64 bit edition working in Linux. Even though my lab would have had some commercial licenses, I was insistent on using my Linux machine for development. In this process, I learned a lot of interesting stuff and hopefully this post will be of some help to people trying out Matlab in Linux.
As a note , all these instructions work for R2008b , although, I think it should apply for future versions as well. Also I am assuming that you are using Ubuntu , even though changing the steps to other Linux versions should be easy.
As a standard disclaimer, all these steps worked for me and I hope they do work for you too. But no guarantees 🙂
Installing 32 bit Matlab in a 64 bit Ubuntu
When I was installing first, I got the basic information from this post in Ubuntu Forums . The typical way to install Matlab is to invoke the install_unix.sh which will give out some error like this
"/media/cdrom0/unix/update/bin/glnxa64/xsetup: not found" .
If you get this error here are the steps to follow :
a. sudo apt-get install ia32-libs
If you are using AMD64, (or even otherwise) , install the ia32-libs package to get the 32 bit libraries.
b. You would also have to install a 32 bit JRE as Matlab depends on it. Since there are too many JREs (Sun,gcj,OpenJDK), I will leave the exact steps unspecified.
c. sudo /media/cdrom0/install_unix.sh -glnx86
This command forces Matlab to install as if it is a 32 bit machine.
d. Registering your Matlab
Follow the instructions in this MathWorks post for registering your Matlab.
Invoking Matlab In 32 bit mode
Since you installed Matlab in 32 bit mode, you have to invoke it in 32 bit. Assuming Matlab command is in your path , invoke Matlab as
matlab -glnx86 &
If it is not , give the full path as (of course, assuming it is installed in /opt )
/opt/matlab/bin/matlab -glnx86 &
For convenience , I have setup an alias for Matlab as
alias matlab=’matlab -glnx86′
You can put this line in your .bashrc or .zshrc file so that it is set every time.
Compiling C and C++ source files using MEX in Ubuntu
For my research, I was using a library which had part of the code in C and C++. Matlab allows extensions to be written in these languages. The library authors had helpfully compiled it for Windows but I was not able to use it in Linux directly. My scenario was even harder because , I not only had to compile them in Linux, I had to cross compile them in 32 bit mode.
Note : Since my library had both C and C++ files, I had given instructions for both of them. Most of the time , the libraries are written in either of these languages and hence you will need only part of the instructions. If you are using a 32 bit Linux system, ignore the cross compilation steps.
I have to acknowledge this post for pointing me in the right direction. It is a little old , the steps did not work immediately for me (I had to install a few more packages) but my steps are simply an extension to his post.
Let us take it step by step.
a. Assume the file name is abc.c. You try mex abc.c and it spits out
Warning: You are using gcc version "4.4.0". The earliest gcc version supported with mex is "4.0.0". The latest version tested for use with mex is "4.2.0". To download a different version of gcc, visit http://gcc.gnu.org
b. So we need to install an older version of gcc to compile the code.
sudo apt-get install gcc-4.1 g++-4.1
c. Other packages "might" be needed but most of the time the above steps should work fine.
d. You must tell your mex to use gcc-4.1 instead of gcc-4.4 . To do this run the following command .
mex –setup
e. This will create a file ~/.matlab/R2008b/mexopts.sh. (If your version is different, the path will change too). Open it with some text editor.
f. If you are using 32 bit system (or installed 32 bit Matlab edition in a 64 bit system) go to the case glnx86 and change the following lines
CC=’gcc-4.4′ (or some other version)
CXX=’g++-4.4′ (or some other version)
to
CC=’gcc-4.1′
CXX=’g++-4.1′
g. If you are using 32 bit Matlab in 64 bit system then this script will always match glnxa64 case. So to force the system to use 32 bit mode, I added this line before the switch statement for architecture (around line no 34)
Arch="glnx86";
This should force the system to do a 32 bit compilation.
h. Try compiling the file again. If you get some error related to stubs (/usr/include/gnu/stubs.h:7:27: error: gnu/stubs-32.h: No such file or directory) , install the multilib packages needed for cross compilation.
sudo apt-get install gcc-4.1-multilib g++-4.1-multilib
i. If you still get some errors install the following packages. (I am not sure whether they are needed, but installing them solved the problem)
sudo apt-get install lib32gcc1 libc6-i386 libc6-dev-i386
j. Hopefully, by this time, the code must get compiled. If not , do a wild goose chase and install all possible 32 bit libraries that looked even remotely relevant like I did 😉
k. So far we have solved the compilation problems. You "might" get some error in linking. For example , I got this error when trying to link.
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.1.3/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.1.3/libstdc++.a when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.1.3/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.1.3/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
The step that solved this problem is this . (Beware, you changing some links in a critical system location , so use caution) . Assuming the /usr/lib32/libstdc++.so and /usr/lib32/libgcc_s.so dont exist , try
sudo ln -s /usr/lib32/libstdc++.so.6 /usr/lib32/libstdc++.so
sudo ln -s /usr/lib32/libgcc_s.so.1 /usr/lib32/libgcc_s.so
Of course, you might need to change the exact shared object filename (it may be different from so.6 )
l. After all these hard steps, your code should get compiled and linked successfully. Congratulations ( Evil smile 🙂 )
Some Random Thoughts
As I said, I am not a big fan of Matlab and Matlab in Linux is not helpful in changing that impression. The keyboard shortcuts (I use them a LOT) are totally different from other tools. Heck, they don’t even match the Matlab editor shortcuts in Windows. I can understand not using ctrl-c but others ? Now I have resorted to coding in vim and using the Matlab editor for only testing.
There are even more bad things. Unfortunately, the C library I am using seems to have some memory leak and I was trying to debug it. Actually, I am not sure if the Matlab code is bad or the C code. When I run it for large models it crashes with "Out Of Memory" error. The problem is that for some reason , the most useful commands for handling memory issues like memory and feature(‘memstats’) doesn’t seem to work (They throw, not supported error ). I am not sure if this problem occurs as I am using 32-bit Matlab in 64 bit or it is common to Matlab in Linux in general. I am currently doing a very crude analysis using free and top (gasp !) and other convoluted means to test my code. I am still researching more effective methods , so if any knows do chime in ! I will be very grateful ! If I find any good techniques, I will post them here.
If you are as fed up with Matlab as me , then there is an excellent open source alternative called Octave . It is mostly compatible with Matlab. If you are a bit more careful while coding, you can write code that will run in both of them without changes. There are some minor portability issues but that is not a deal breaker. The differences are details here and here. In fact, I am in the process of converting my code to run in Octave and will blog about it once it is done. There is quite some development behind Matlab like GUI at XOctave and QtOctave. I hope the migration should be reasonably painless.
So unless you are using some very Matlab specific features, you are better off porting the code to Octave. Then you will have the best of both worlds.
Hope you found this post useful. I will blog about my experiences with Matlab (gasp !), Octave, porting and other issues soon !
[…] who can index the new post immediately. I experienced this awesome feature after writing my post on Matlab . Within 2 hours, my page was in Google’s search results and I got couple of hits from it. I […]
Hey,
Great post. I am having the exact same experience right now with student version of matlab on 64-bit linux. So frustrating. Anyway, could you post what your mexopts.h looks like with the Arch hard-coded in there as “glnx86”. I can’t get it to work!
Thanks,
Skipper
Ah, you just put it right before the
“case” switch. Got it.
Great ! Glad that you were able to figure it out yourself.
[…] Installing, running matlab and compiling Matlab extensions in a 64 bit Ubuntu system […]