先日新しい12インチMacBookを買った ので、今開発環境などを整えたりしています。Ruby on Railsの環境も然りといったところです。これまではRubyをRVMで管理していましたが、今はrbenvの方が主流になってきているので、今回の新しいMacBookでは、rbenvを使ってRubyとRailsの環境を構築することにしました 。環境構築方法をメモしておきます。
rbenvとは rbenvは、複数のRubyのバージョンを管理し、プロジェクトごとにRubyのバージョンを指定して使うことを可能としてくれるツール です。(変わった名称についてはきっと「R ub y env ironment」の略でしょう。読み方は「アールビー・エンブ」または「アールベンブ」。)
他に同じような機能を持ったツールとして「RVM」 というものがあります。rbenvとRVMの違いなどについては、以下を参考にしてください。
Homebrewでrbenvをインストールする まずrbenvはRVMと相性が悪いので、RVMがインストールされている場合は、事前にRVMをアンインストールしておきます。
常に最新版がアップされているということからGithubからダウンロードしてインストールする方法 が推奨されていますが、自分は手軽さを選んでMacのパッケージマネージャーであるHomebrew を使ってrbenvをインストールしました。
Homebrewのインストール方法などは当ブログの過去記事などを参考にしてください。
Homebrewをアップデートする まずrbenvをインストールする前にHomebrewをアップデートして、Fomula(パッケージ)を最新の状態にしておきます。
$ brew update
Updated Homebrew from 19883e74 to 2c822cee.
Homebrewを使ってrbenvをインストールする Homebrewをアップデートしたら、さっそくrbenv とruby-build をインストールします。[brew install] コマンドはスペース区切りで複数のパッケージの一括インストールが可能です。ruby-build はUNIX互換環境に異なったバージョンのrubyをコンパイル、インストールするための[rbenv install] コマンドを使えるようにするrbenvのプラグイン です。
$ brew install rbenv ruby-build
==> Downloading https://github.com/sstephenson/rbenv/archive/v0.4.0.tar.gz
######################################################################## 100.0%
==> Caveats
To use Homebrew's directories rather than ~/.rbenv add to your profile:
export RBENV_ROOT=/usr/local/var/rbenv
To enable shims and autocompletion add to your profile:
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
==> Summary
🍺 /usr/local/Cellar/rbenv/0.4.0: 31 files, 152K, built in 3 seconds
==> Installing dependencies for ruby-build: autoconf, pkg-config, openssl
==> Installing ruby-build dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring autoconf-2.69.yosemite.bottle.1.tar.gz
🍺 /usr/local/Cellar/autoconf/2.69: 70 files, 3.1M
==> Installing ruby-build dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.28.yosemite.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.28.yosemite.bottle.2.tar.gz
🍺 /usr/local/Cellar/pkg-config/0.28: 10 files, 612K
==> Installing ruby-build dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2a-1.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2a-1.yosemite.bottle.1.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
/usr/local/etc/openssl/certs
and run
/usr/local/opt/openssl/bin/c_rehash
This formula is keg-only, which means it was not symlinked into /usr/local.
Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.
Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries
Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:
LDFLAGS: -L/usr/local/opt/openssl/lib
CPPFLAGS: -I/usr/local/opt/openssl/include
==> Summary
🍺 /usr/local/Cellar/openssl/1.0.2a-1: 463 files, 18M
==> Installing ruby-build
==> Downloading https://github.com/sstephenson/ruby-build/archive/v20150519.tar.gz
######################################################################## 100.0%
==> ./install.sh
🍺 /usr/local/Cellar/ruby-build/20150519: 162 files, 688K, built in 3 seconds
この状態でrbenvがインストールされたか、rbenvのバージョンを確認してみると「.rbenv」が見つからない と怒られます。
$ rbenv -v
/usr/local/Cellar/rbenv/0.4.0/libexec/rbenv---version: line 17: cd: /Users/maechabin/.rbenv: No such file or directory
インストール方法に書いてある通り、[rbenv init] コマンドを「~/.bashrc」に追加して実行するようにします。
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
再度rbenvのバージョンを調べると、「rbenv 0.4.0」と出てちゃんとインストールされていることがわかります。
$ rbenv -v
rbenv 0.4.0
rbenvでRubyをインストールする まず、rbenvがインストールされたら、rbenvを使ってRubyをインストールします。
Rubyをインストールする インストール可能なRubyのバージョンを調べます。
$ rbenv install -l
Available versions:
1.8.6-p383
1.8.6-p420
1.8.7-p249
・・・
・・・
インストールしたいバージョンを指定して、[rbenv install] コマンドでインストールしたいバージョンを指定してRubyをインストールします。
$ rbenv install 2.2.2
Downloading ruby-2.2.2.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/5ffc0f317e429e6b29d4a98ac521c3ce65481bfd22a8cf845fa02a7b113d9b44
Installing ruby-2.2.2...
Installed ruby-2.2.2 to /Users/maechabin/.rbenv/versions/2.2.2
グローバルバージョンを指定する [rbenv global] コマンドで、すべてのシェルで使うデフォルトのRubyのバージョンを指定することができます。
rbenv global 2.2.2
ローカルバージョンを指定する ちなみに特定のディレクトリ内で[rbenv local] コマンドを実行すると、そのディレクトリ以下で適用されるRubyのローカルバージョンを指定することができます (グローバルバージョンより優先されます)。ローカルバージョンは、ディレクトリ内に作られる「.ruby-version」 ファイルに書き込まれます。
$ rbenv local 1.9.3-p327
指定したローカルバージョンを解除するには、以下のコマンドを実行します。
$ rbenv local --unset
バージョンの確認 [rbenv version] コマンドで、現在のアクティブなバージョンと、どのようにセットされいるかの情報を一緒に表示してくれます。
$ rbenv version
2.2.2 (set by /Users/maechabin/.rbenv/version)
また、[rbenv versions] コマンドで、rbenvで管理しているインストール済みのRubyのバージョンを表示させることができます。アスタリスクが付いているものが、現在のアクティブなバージョンとなっています。
$ rbenv versions
1.8.7-p352
1.9.2-p290
* 1.9.3-p327 (set by /Users/sam/.rbenv/version)
jruby-1.7.1
rbx-1.2.4
ree-1.8.7-2011.03
Bundlerをインストールする Rubyのインストールが完了したら、次にBundleコマンドを使えるようにするために、「Bundler」をインストールします。BundlerはRubyプロジェクトにおけるGemやそのバージョンなどの依存関係を管理してくれるツール です。
[gem install bundler] コマンドだけでもインストールできますが、今回は[rbenv exec] コマンドを前につけて、bundlerをインストールします。
$ rbenv exec gem install bundler
Fetching: bundler-1.9.9.gem (100%)
Successfully installed bundler-1.9.9
Parsing documentation for bundler-1.9.9
Installing ri documentation for bundler-1.9.9
Done installing documentation for bundler after 4 seconds
1 gem installed
[rbenv exec] コマンドをつけることによって、「選択中のバージョンのRubyへのパスを $PATHの先頭に追加して実行してくれ、確実に現在のRuby環境でコマンドが実行されることを保証してくれる」 ということのようです。
新しいバージョンのRubyやGemをインストールしたら、rbenvで管理しているRubyで使えるようにするため に[rbenv rehash] コマンドを実行します。
$ rbenv rehash
Bundlerのバージョンが表示されれば、Bundlerのインストールは完了です。
$ rbenv exec bundler -v
Bundler version 1.9.9
Bundlerを最新版にバージョンアップする場合 ちなみにBundlerを最新版にバーションアップする場合は、以下のコマンドを実行します。
$ rbenv exec gem update --system
$ rbenv exec gem update bundler
Rails環境を準備する インストールしたBundlerを使って、Railsの環境を構築していきます。
Railsプロジェクト用のディレクトリに移動し、以下のコマンドでGemfileを作ります。Gemfileは、Node.jsのnpmで言う所のpackage.jsonのようなもので、Railsで使用するGemの依存関係を管理するためのファイル です。
$ rbenv exec bundle init
Writing new Gemfile to /Users/maechabin/Sites/Project/rails-test/Gemfile
作成されたGemfileを開き、「#gem “rails”」の部分の「#」を削除し、コメントアウトを外します。
$ vim Gemfile
# A sample Gemfile
source "https://rubygems.org"
gem "rails"
「– – path vendor/bundle」 オプションをつけて[bundle install] コマンドを実行します。「vendor/bundle」ディレクトリ内にGemfileに記載のGemがインストールされます。
$ rbenv exec bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.4.2
Installing i18n 0.7.0
Installing json 1.8.2
Installing minitest 5.6.1
Installing thread_safe 0.3.5
Installing tzinfo 1.2.2
Installing activesupport 4.2.1
Installing builder 3.2.2
Installing erubis 2.7.0
Installing mini_portile 0.6.2
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
/Users/maechabin/.rbenv/versions/2.2.2/bin/ruby -r ./siteconf20150523-16570-wa1zme.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
-----
The file "/usr/include/iconv.h" is missing in your build environment,
which means you haven't installed Xcode Command Line Tools properly.
To install Command Line Tools, try running `xcode-select --install` on
terminal and follow the instructions. If it fails, open Xcode.app,
select from the menu "Xcode" - "Open Developer Tool" - "More Developer
Tools" to open the developer site, download the installer for your OS
version and run it.
-----
・・・
・・・
今回自分の環境では「コマンドラインツールがちゃんとインストールされていない」と怒られたので、以下のコマンドを実行して「コマンドラインツール」をインストールしました。
$ xcode-select --install
再度[bundle install] コマンドを実行すると、今度は「Bundle complete!」となり、無事Gemのインストールが完了しました。
$ rbenv exec bundle install --path vendor/bundle
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/..
Resolving dependencies...
Using rake 10.4.2
Using i18n 0.7.0
Using json 1.8.2
Using minitest 5.6.1
Using thread_safe 0.3.5
Using tzinfo 1.2.2
Using activesupport 4.2.1
Using builder 3.2.2
Using erubis 2.7.0
Using mini_portile 0.6.2
Installing nokogiri 1.6.6.2
Installing rails-deprecated_sanitizer 1.0.3
Installing rails-dom-testing 1.0.6
Installing loofah 2.0.2
Installing rails-html-sanitizer 1.0.2
Installing actionview 4.2.1
Installing rack 1.6.1
Installing rack-test 0.6.3
Installing actionpack 4.2.1
Installing globalid 0.3.5
Installing activejob 4.2.1
Installing mime-types 2.5
Installing mail 2.6.3
Installing actionmailer 4.2.1
Installing activemodel 4.2.1
Installing arel 6.0.0
Installing activerecord 4.2.1
Using bundler 1.9.9
Installing thor 0.19.1
Installing railties 4.2.1
Installing sprockets 3.1.0
Installing sprockets-rails 2.3.1
Installing rails 4.2.1
Bundle complete! 1 Gemfile dependency, 33 gems now installed.
Bundled gems are installed into ./vendor/bundle.
[bundle list] コマンドで、インストールされたGemを確認します。Rails 4.2.1もちゃんとインストールされていることがわかります。
$ rbenv exec bundle list
Gems included by the bundle:
* actionmailer (4.2.1)
* actionpack (4.2.1)
* actionview (4.2.1)
* activejob (4.2.1)
* activemodel (4.2.1)
* activerecord (4.2.1)
* activesupport (4.2.1)
* arel (6.0.0)
* builder (3.2.2)
* bundler (1.9.9)
* erubis (2.7.0)
* globalid (0.3.5)
* i18n (0.7.0)
* json (1.8.2)
* loofah (2.0.2)
* mail (2.6.3)
* mime-types (2.5)
* mini_portile (0.6.2)
* minitest (5.6.1)
* nokogiri (1.6.6.2)
* rack (1.6.1)
* rack-test (0.6.3)
* rails (4.2.1)
* rails-deprecated_sanitizer (1.0.3)
* rails-dom-testing (1.0.6)
* rails-html-sanitizer (1.0.2)
* railties (4.2.1)
* rake (10.4.2)
* sprockets (3.1.0)
* sprockets-rails (2.3.1)
* thor (0.19.1)
* thread_safe (0.3.5)
* tzinfo (1.2.2)
Railsアプリケーションを作る これでRailsのプロジェクトをいつでもスタートできるようになりました。さっそくRailsアプリケーションを作ってみます。
上記でGemをインストールしたRailsプロジェクトのディレクトリ内で、[bundle exec rails new .] コマンドを実行すると、Railsアプリが生成されます。Gemfileを上書きするかどうか聞かるので、「y」と入力し上書きを許可します。
$ rbenv exec bundle exec rails new .
exist
create README.rdoc
create Rakefile
create config.ru
create .gitignore
conflict Gemfile
Overwrite /Users/maechabin/Sites/Project/rails-test/Gemfile? (enter "h" for help) [Ynaqdh] y
以下のようにRailsアプリ用のフォルダやファイルが生成されます。
さっそく[bundle exec rails s] コマンドで、サーバーを起動してみます。
$ rbenv exec bundle exec rails s
=> Booting WEBrick
=> Rails 4.2.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2015-05-23 02:57:56] INFO WEBrick 1.3.1
[2015-05-23 02:57:56] INFO ruby 2.2.2 (2015-04-13) [x86_64-darwin14]
[2015-05-23 02:57:56] INFO WEBrick::HTTPServer#start: pid=37603 port=3000
Started GET "/" for ::1 at 2015-05-23 02:58:08 +0900
Processing by Rails::WelcomeController#index as HTML
Rendered vendor/bundle/ruby/2.2.0/gems/railties-4.2.1/lib/rails/templates/rails/welcome/index.html.erb (2.7ms)
Completed 200 OK in 29ms (Views: 16.5ms | ActiveRecord: 0.0ms)
ブラウザのアドレスバーに「localhost:3000」 と入力し、以下の画面が表示されれば成功です。
まとめ 以上、rbenvでRubyを管理し、Rails開発環境を構築する方法となります。これまでRVMを使っていましたが、rbenvと比べてまずRVMはRVM自身のインストールから大変だったような印象があります。rbenvはインストールも簡単だし、Rubyの管理もシンプルに行えるような印象です 。まだrbenvに関してはよくわかっていない部分もあるので、少しずつ使いながら覚えていければいいかなと思っているところです。
最後に今回参考にさせていただいたブログ記事などを紹介しておきます。
それから、定番のRails本も併せて紹介しておきます。
『パーフェクト Ruby on Rails』 著者: すがわらまさのり, 前島真一, 近藤宇智朗, 橋立友宏 出版社: 技術評論社 発売日: 2014年6月6日 ISBN: 4774165166
Amazon.co.jpで詳細を見る
コメント
ピンバック: 【Mac】Ruby on Railsの環境構築〜動作確認までを行う - Reasonable Code