[RUBY] Enable natto / mecab gem on Heroku environment

Purpose

Since mecab is not installed in the Heroku environment, if you simply push it, it will fail in the build. There were various methods such as heroku-buildpack-mecab and heroku-buildpack-linuxbrew, but The more I looked it up, the more deprecated storms. The result of trial and error as to whether it can be done with buildpack is the method of using heroku-buildpack-apt this time. Record how to build an environment using heroku-buildpack-apt and heroku-buildpack-ruby.

Target

--People who want to use gem natto / mecab in Rails / Sinatra environment on Heroku --People who are not familiar with Docker and want to prepare the above environment by a method that does not use Docker

heroku-buildpack-apt

heroku-buildpack-ruby

procedure

Gemfile Make sure the following gems are installed in the Gemfile

Gemfile


gem 'mecab', '0.996'
gem 'natto'

buildpack settings

bash


heroku buildpacks:add --index 1 https://github.com/heroku/heroku-buildpack-apt
heroku buildpacks:add --index 2 heroku/ruby

Now you have an environment that uses these two buildpacks. Reference

Setting environment variables

It is installed under /app/.apt/usr/lib/x86_64-linux-gnu via buildpack. mecabrc goes in /app/.apt/etc/mecabrc. Go through the path there

bash


heroku config:set MECABRC=/app/.apt/etc/mecabrc
heroku config:set MECABRC=/app/.apt/etc/mecabrc
heroku config:set MECAB_PATH=/app/.apt/usr/lib/x86_64-linux-gnu/libmecab.so

Creating an Aptfile

Write the packages you want to be installed on heroku-buildpack-apt. Create ʻAptfile` at the root of the repository

Aptfile


mecab
libmecab-dev
mecab-ipadic
mecab-ipadic-utf8

Deploy

Deploy

bash


git add .
git commit -m 'commit message'
git push heroku master

Conclusion

I did my best with buildpack, but I think it's overwhelmingly peaceful to do it with Docker.

Recommended Posts

Enable natto / mecab gem on Heroku environment
Introducing Bugsnag on Heroku
Yay! I'm on Heroku!
Use Corretto 11 on Heroku
Error resolution on Heroku