LoginSignup
2
2

More than 5 years have passed since last update.

AmazonEC2上のubuntuにrbenvでRubyをイントールする(ついでにRailsも)

Posted at

準備

AmazonEC2にインスタンスを作成します。
http://aws.amazon.com/jp/ にアカウントの作成からsshで接続まで詳しい手順が記載されているので割愛します。

gitのインストール

$ sudo apt-get install -y git-core

rbenvをインストール

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

設定

コマンドラインでrbenvを使用するためにパスを通します。
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

続けて自動補完を有効にするrbenv initの設定です。
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

上記の設定を読み込みます。
$ exec $SHELL -l

rbenvの確認

$ rbenvと入力してバージョンが表示されたら準備完了です。以下は表示例です。

rbenv 0.4.0-131-g1a7f49d
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/sstephenson/rbenv#readme

ruby-buildをインストール

rbenvはあくまでrubyのバージョンを切り替えてくれるだけで、rubyをインストールしてくれるわけではありません。rubyのインストールにはruby-buildを使用します。
$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build

rubyをインストール

$ rbenv install -lでインストール可能なrubyをリスト表示します。
最新版の2.1.5をインストールしてみます。
$ rbenv install 2.1.5
グローバル環境に設定
$ rbenv global 2.1.5
バージョンの確認
$ ruby -v # => 2.1.5

Railsのインストール

gemでrailsをインストールします。
$ gem install rails
するとエラーが出現。

ERROR:  Error installing rails:
    ERROR: Failed to build gem native extension.

        /usr/bin/ruby1.9.1 extconf.rb
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mkmf (LoadError)
    from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from extconf.rb:1:in `<main>'

ここを参照すると
$ sudo apt-get install ruby1.9.1-dev
すると解決出来そうです。
気を取りなおしてrailsをインストールすると大丈夫そう。
rails -v
でバージョン確認してみると
Rails 4.2.0
と表示されたので準備完了です。これでrubyとrailsで開発する環境は整いました。

2
2
0

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
2
2