LoginSignup
11
9

More than 5 years have passed since last update.

Railsでlibv8/therubyracerがMacOSX(Yosemite)でインストールできない問題対策

Last updated at Posted at 2015-09-13

動機と問題

やりたいことはRailsの開発環境をMacOS上に構築することです。もう少し具体的にすると、

  • RailsアプリケーションはLinuxサーバで公開する(nginx, unicornで動作させる)
  • 開発環境(手元のmac)では動作確認はしたいけどrails server(WEBrick)で十分
  • git, chefで管理したいのでコードはできるだけ共通にしたかった

です。rails newしたあとのGemfileのコメントを外してunicornを使えるようにしておきます。

app/Gemfile
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.7'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer',  platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring',        group: :development
# Use unicorn as the app server
gem 'unicorn

ここで問題となるのが、OS X Mavericks以降(?)で確認されているらしい問題で、Mac上でbundle installするときにlibv8therubyracerのインストールが失敗してしまいます。bundle installしたときのエラーはこのような感じ。

An error occurred while installing therubyracer (0.12.2), and Bundler cannot
continue.
Make sure that `gem install therubyracer -v '0.12.2'` succeeds before bundling.

どうやらMacの環境と相性があるらしく、それぞれに合うバージョンを入れる必要があるようです。

$ bundle config build.libv8 — –with-system-v8
$ bundle config build.therubyracer — –with-v8-dir

Railsでtherubyracerがインストールできない問題を解決こちらの記事など参考になるかもしれません。

それでもダメだった

それでもなぜ記事にしているかというと、半日戦ってダメだったからです(原因はよくわかりません…)。
つまり、上記のGemfileではMac上でbundle installが通らない、とはいえGemfileはひとつにしておきたいしtherubyracerはサーバで必要なので消したくない状態です。

ということで、別の逃げ道を探します。

解決策

therubyracerが必要なのはサーバだけなので、ローカルではこれをインストール対象にしなければよさそうです。

具体的には、gem 'therubyracer', platforms: :rubyの項目をグループにして、--withoutオプションをつけてbundle installします。

group :production do
    # See https://github.com/sstephenson/execjs#readme for more supported runtimes
    gem 'therubyracer',  platforms: :ruby
end

毎回オプションをつけるのも面倒なので、configに追加しておきます。

app/.bundle/config
BUNDLE_WITHOUT: production

これでMac上でもbundle installが通りました。
サーバではwithoutを設定せず通常通りbundle installして、therubyracerもインストールしてやります。

(この環境依存はどうにかならないものか…)

11
9
1

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
11
9