Date Modified Tags ruby / Rails

rhaco使わないでWebサービス作るならもうPHP使わずに他の言語でやってみようかなと思い、 DjangoとRailsを迷ったあげく、ChefでRuby使うからRubyの勉強という意味を込めてRailsでやってみることにした。

とりあえずまずはインストール出来ないと話にならないので、Railsをインストールしてみた話。

Ubuntu使ってるので、aptitude install railsでさくっと終わるかなと思ってTwitterで何となく

shigepon: railsはバージョンいくつを使うと吉なんでしょ?

とつぶやいたら、@sugamasaoさんから

sugamasao: @shigepon しがらみがなければ、最新の4.0.2が良いと思います。お試しで触って見るなら、4.1のbeta版を使って見ても良いかもしれません〜

という話が!そうなるとrubyも2系入れないとダメじゃん!と思いつつ

shigepon: @sugamasao ただ、うちのubuntuのrubyが1.8なので、まずは2.0にしないといけないっすね。

とボヤいたら、やっぱり

sugamasao: @shigepon なるほど〜!1.8系はもうオワコンなので、2系を入れたほうが良いですねwちなみに、いまは2.1がリリースされてます!

とのお達しが。んじゃ2系入れるかとググりながらインストールしてみた。 参考にしたページは次のとおり

手順

行ったり来たりしてるからこんな感じでスッと出来てないけど、だいたいこんな感じ

rbenvインストール

git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
mkdir -p ~/.rbenv/plugins
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

~/.profileに以下を追加

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

んで

source ~/.profile

opensslは入ってたから特に何もしなかったけど必要なら

sudo aptitude install openssl

iconv, readlineは最初から入ってるんじゃないかな? んでインストール

sudo rbenv install -l
rbenv install 2.1.0
rbenv global 2.1.0

rbenvで何かやったら

rbenv rehash

を忘れずに

Bundlerインストール

rbenv exec gem install bundler
rbenv rehash

Railsインストール

bundle init
vim Gemfile

Gemfile内に

gem "rails", "4.0.2"

を追加。

bundle install --path vendor/bundle
bundle exec rails new hoge --skip-bundle
rm -f Gemfile
rm -f Gemfile.lock
rm -rf .bundle
rm -rf vendor/bundle
cd hoge
bundle install --path vendor/bundle
echo '/vendor/bundle' >> .gitignore
bundle exec rails server

で途中下のようにハマりながら起動を確認した。

はまったポイント

Ruby on Railsをインストールする時にプロジェクトのディレクトリで

bundle install -path vendor/bundle

すると、

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb 
checking for sqlite3.h... no
sqlite3.h is missing. Try 'port install sqlite3 +universal'
or 'yum install sqlite-devel' and check your shared library search path (the
location where your sqlite3 shared library is located).
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/usr/bin/ruby1.9.1
    --with-sqlite3-dir
    --without-sqlite3-dir
    --with-sqlite3-include
    --without-sqlite3-include=${sqlite3-dir}/include
    --with-sqlite3-lib
    --without-sqlite3-lib=${sqlite3-dir}/lib
    --enable-local
    --disable-local


Gem files will remain installed in /home/helio/.bundler/tmp/23089/gems/sqlite3-1.3.8 for inspection.
Results logged to /home/helio/.bundler/tmp/23089/gems/sqlite3-1.3.8/ext/sqlite3/gem_make.out

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

というエラーが出たのは

sudo aptitude install sqlite3 libsqlite3-dev
bundle install --path vendor/bundle

で出なくなった。インストールパスの指定が無ければpathオプションは要らない。

bundle exec rails serverで

autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

というエラーが出て、サーバを起動出来なかった。これは プロジェクトに入っているGemfileに書いてあるtherubyracerに関する行をコメントアウトして

bundle install --path vendor/bundle

とすれば起動出来るようになった。