LoginSignup
18
12

More than 5 years have passed since last update.

herokuでpython+django+scikit-learn+mecab(1)

Posted at

herokuでpythonのscikit-learn+mecabを使うときの方法。

herokuにはRubyでmecabを扱うためのheroku-buildpack-mecabは用意されているが、python用のものはない。そこでheroku-buildpack-linuxbrewを使ってmecabをbrew installする。

また、heroku-buildpack-pythonではscipyやscikit-learnなど、cコンパイラが必要なlibraryはインストールできない。代わりにheroku-buildpack-condaでpythonの代わりにminicondaをインストールすることでこれらのpackageがinstallされる。

これら複数のbuildpackを扱うために、heroku-buildpack-multiでappを作成する必要がある。

まずheroku用のローカルレポジトリ内で

$ git init
$ heroku create --buildpack https://github.com/heroku/heroku-buildpack-multi

でアプリを作成する。

次にローカルレポジトリ内に .buildpacksを作成して

.buildpacks
https://github.com/kennethreitz/conda-buildpack.git
https://github.com/sunny4381/heroku-buildpack-linuxbrew.git

と記述して保存。
buildpack-multiはこのファイル内に記載されているbuildpackをインストールしてくれる。

同ディレクトリ内に .cellar を作成して

.cellar
mecab
mecab-ipadic

と記述して保存。
buildpack-linuxbrewはこのファイル内に記載されているアプリケーションをインストールしてくれる。

pip、condaでインストールしたいライブラリはそれぞれ
requirements.txt、conda-requirements.txt に記載すると勝手にインストールしてくれる。

$ pip freeze > requirements.txt

または

$ conda list > requirements.txt

注意点として、mecab-pythonはpipではinstallできないことである(もしかしてpipのversionを上げればいける??)
sh: 1: mecab-config: not foundというエラーが発生

そこでmecab-pythonはheroku上で手動でインストールする(かなりの力技・・・)
その準備として、ローカルレポジトリ内で

$ curl -O https://mecab.googlecode.com/files/mecab-python-0.996.tar.gz
$ tar zxfv mecab-python-0.996.tar.gz
$ rm https://mecab.googlecode.com/files/mecab-python-0.996.tar.gz

mecab-pythonを展開する。

またmecab-python-0.996内のsetpu.pyの'mecab-config''/app/.linuxbrew/bin/mecab-config'に変更する。(参照元:http://qiita.com/saicologic/items/ab70e14f7e2ec2ee0b4d)

これらのことが終了したのちに、bashでデプロイする。

$ heroku config:add LD_LIBRARY_PATH=/app/.linuxbrew/lib
$ heroku config:set MECAB_PATH=/app/.linuxbrew/lib/libmecab.so
$ git add .
$ git commit -m 'initial'
$ git push heroku master

デプロイ後に、heroku上でmecab-python を手動でインストールする。

$ heroku run bash
~/ cd mecab-python-0.996
~/ python setup.py build
~/ python setup.py install

でOK.。最後に同じくheroku上でpythonを起動して import MeCabできるか確認。

これでheroku上でpython + scikit-learn + mecabを使うことができるが、ファイルサイズがめちゃくちゃ重くなる・・・

無料の300Mだとかなり厳しい気が・・・
(参照元:http://qiita.com/shouta-dev/items/cd538a77f2b729333025)

18
12
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
12