Install MeCab and MeCab-Ruby

Install MeCab and MeCab-Ruby on Snow Leopard

The instruction on this pages is based on the following pages (Japanese).

RとLinuxと...

http://cms.ias.tokushima-u.ac.jp/index.php?RMeCab

mecab-ruby のインストール

http://homepage.mac.com/matsuan_tamachan/software/MecabRuby.html

The purpose of installing Mecab-Ruby is to use it from my other programs, so you need to use Terminal and command line. If you just want to install Mecab-Ruby and use Mecab only from Ruby, you can easily install it using MacPorts. The instruction is here. I wrote a simple utility program, CasualMecab, to process Japanese text with Mecab. Check this page.

In case you are not familiar with Terminal, I've also made a page with screenshots.

First, you need to install Xcode Tools. It's on Leopard CD or you can download it from Apple Developper Connection (you need to register [free]). If you have already installed Xcode on your computer, skip this section.

Put the Leopard DVD in your Mac. Go to Optional Install folder -> Xcode Tools and open XcodeTools.mpkg. Follow the installation instruction.

Download MeCab files

MeCab download (sourceforge)

http://sourceforge.net/project/showfiles.php?group_id=177856

and download the following software.

mecab

mecab-ipadic

mecab-ruby

Click Download for each of these and click the file name on the next page to download.

mecab-0.98.tar.gz

mecab-ipadic-2.7.0-20070801.tar.gz

mecab-ruby-0.98.tar.gz

These three (version might be different) should be on your local drive.

If you use Safari and Open "Safe" files after downloading option is checked, you might see .tar files. You can use these but the process will be slightly different.

Once you download these files, create a new folder and put these three in it (I created it in Download folder and named it mecab).

Install MeCab

The character code for Japanese is set to UTF-8 here because it is the default for Mac OS X. It means the text input should be encoded in UTF-8 to process with Mecab.

Start Terminal.app (Applications -> Utilities)

$ cd (directory)

First you need to go to the folder where three downloaded files are stored. Enter cd (with a space) and the path to the folder with three files. You can enter cd (and a space) and drag&drop the folder that contains mecab-0.98.tar.gz to Terminal window (you don't need to enter $, which is already displayed). Then hit Retern/Enter key. Now you are in the folder. You should see the folder name (mecab).

If your computer name is My iMac and your user name is Me and you created a folder mecab with three files, you first see:

My-iMac:~ me$

when you start Terminal.app. Then type cd (and a space) and drag&drop the mecab folder onto the Terminal window. You should see:

My-iMac:~ me$ cd /Users/me/Downloads/mecab

Hit the return/enter key. Then you will see:

My-iMac:mecab me$

Now enter the following commands line by line. If the downloaded files are newer (differnt file names), change the file names in the commands accordingly. If the downloaded files are the same versions, you can copy&paste these lines from here (without $).

$ tar zxvf mecab-0.98.tar.gz

If Safari opened .gz files and created .tar, double click mecab-0.98.tar file on your finder before you proceed.

$ cd mecab-0.98

$ ./configure --with-charset=utf-8;make;sudo make install

This process takes a few minutes depending on how fast your Mac is. You need to enter password with sudo make install process. This password is the same as your admin password on your Mac.

Install MeCab dictionary (mecab-ipadic)

Go back to the original folder (mecab).

$ cd (directory)

or

$ cd ..

This cd .. means go back one directory, so the current directory is set to the original folder (folder that contains mecab-0.97 folder). If the mecab-ipadic-2.7.0-20070801.tar.gz file is placed in another folder, drag&drop the folder after typing cd (with a space after it). Once you are in the folder, copy&paster the following commands one by one.

$ tar zxf mecab-ipadic-2.7.0-20070801.tar.gz

$ cd mecab-ipadic-2.7.0-20070801

$ ./configure --with-charset=utf-8;make;sudo make install

If you use CasualMecab, you can stop here. If you want to use Mecab from Ruby, you need to install MeCab-Ruby.

Install MeCab-Ruby

Go back to the original folder (mecab).

$ cd (directory)

or

$ cd ..

Once you are in the original folder, enter following commands one by one.

$ tar zxf mecab-ruby-0.98.tar.gz

$ cd mecab-ruby-0.98

If you know how to edit content of a file from Terminal, you can, but on Mac, you can simply go to Finder and open the files to edit. So go to Finder and open a folder that contains the mecab-ruby-0.97 folder. In the mecab-ruby-0.97 folder, you will find extconf.rb. Open it with TextEdit or your favorite text editor.

At the end of the file, you will see:

$CFLAGS += ' ' + `#{mecab_config} --cflags`.chomp

have_header('mecab.h') && create_makefile('MeCab')

add

$LDFLAGS = '-L/usr/local/lib'

in between above two lines. So the last three lines should look like this:

$CFLAGS += ' ' + `#{mecab_config} --cflags`.chomp

$LDFLAGS = '-L/usr/local/lib'

have_header('mecab.h') && create_makefile('MeCab')

Save the file and go back to Terminal.

Type ruby extconv.rb and hit return/enter key.

$ ruby extconf.rb

Go back to the mecab-ruby-0.98 folder in Finder. You will see Makefile is created. Open it with a text editor and find a text string "LDSHARED = cc " to find the following line.

LDSHARED = cc -arch i386 -arch x86_64 -pipe -bundle -undefined dynamic_lookup

Replace cc with $(CXX).

LDSHARED = $(CXX) -arch i386 -arch x86_64 -pipe -bundle -undefined dynamic_lookup

Next, search a string "LIBS = $(LIBRUBYARG_SHARED)".

LIBS = $(LIBRUBYARG_SHARED) -lstdc++ -lmecab -lpthread -ldl

Add -liconv at the end of the line.

LIBS = $(LIBRUBYARG_SHARED) -lstdc++ -lmecab -lpthread -ldl -liconv

Save the file and close it.

Go back to Terminal and enter the following commands one by one.

$ make;sudo make install

Now you can use Mecab from Ruby. You can check if the installation is successful by running a test script.

$ ruby test.rb

If Mecab and Mecab-Ruby are successfully installed, you will see Mecab output.

I was able to install these with this instruction. If you find any error, please let me know.