Install MeCab and MeCab-Ruby - detail

Install MeCab and MeCab-Ruby on Snow Leopard

I followed these pages (in Japanese).


mecab-ruby のインストール

The purpose of installing Mecab-Ruby is to use it from my 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 to process Japanese text with Mecab.  Please check this utility: CasualMecab.

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 and follow the installation instruction.

Xcode will be installed on the root directory of your hard drive (unless you installed it somewhere else).  Double click your system hard drive.  Go to Developer -> Applications -> Xcode.

But the easiest way to find it is Spotlight search. 

MeCab download

and download the following software.


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


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 a folder, mecab, in Download folder).

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 [ターミナル in Japanese] (Applications -> Utilities)

When you start Terminal, you will see the something like this.

My-Mac is my Mac's name (you can set this in the System Preferences -> Sharing -> Computer Name) and yasu is my home folder name (account name).  You should see yours on the screen. Type 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.97.tar.gz to Terminal window (you don't need to enter $, which is already displayed).

 $ cd (directory)

Then hit Return/Enter key.  Now you are in the folder.  You should see the folder name (mecab).  

My computer name is My-Mac and my user name is Yasu and I created a folder mecab with three files.  When I start, I see:

My-Mac:~ yasu$

Then type cd (and a space) and drag&drop the mecab folder onto the Terminal window.  The screen should look like this:

My-Mac:~ yasu$ cd /Users/yasu/Downloads/mecab 

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

My-Mac:mecab yasu$

The actual Terminal window should look like this:

Now enter the following commands line by line.  If the downloaded files are newer (different 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 $).  By the way, screen shots are with an older version and you might see some minor differences depending on your Mac and OS version.

First unarchive mecab-0.98.tar.gz.

 $ 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.

You should see something like this (after a long list of files) when it is unarchived.

Now you move to mecab-0.98 folder.

 $ cd mecab-0.98

Once you move to the folder, type the following command and hit return/enter key.

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

You need to enter password with sudo make install process.  This password is the same as your admin password on your Mac.

If MeCab is successfully installed, you will see something like this:

Now MeCab is installed on your Mac.  Let's install the dictionary.

Install MeCab dictionary (mecab-ipadic)

Go back to the original folder (mecab).  Type cd .. and hit return/enter key.

 $ 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.  If your file is newer, you might need to change the file name.

 $ 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 see something like below, the installation is successful.

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 folder).

 $ cd ..

Once you are in the original mecab folder, unarchive mecab-ruby-0.98.tar.gz and move to the unarchived folder.

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

Go to Finder and open a folder that contains the mecab-ruby-0.98 folder.  In the mecab-ruby-0.98 folder, you will find extconf.rb.  Open it with TextEdit or your favorite text editor.

If you use TextEdit, right click the file and select Open with(このアプリケーションで開く)-> Other...(その他)

Select TextEdit (テキストエディット) in the Applications folder.


At the end of the file, you will see:

 $CFLAGS += ' ' + `#{mecab_config} --cflags`.chomp
 have_header('mecab.h') && create_makefile('MeCab')

You need to 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.

On Terminal type ruby extconf.rb and hit return/enter key.

 $ ruby extconf.rb

You should see something like this:

Go back to the mecab-ruby-0.98 folder in Finder.  You will see Makefile is created.

Open Makefile with a text editor (TextEdit).

Once the file is open, 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 -lm 

Add -liconv at the end of the line.

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

Save the file and close it.

Go back to Terminal.  Type make;sudo make install and hit return/enter key.

 $ make;sudo make install

If install is successful, you should see something like this:

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

 $ ruby test.rb

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