Ubuntu14.04にrbenvを用いてrubyをインストール
Ubuntu 14.04にrbenvを使ってRuby On Railsを導入
- rbenvによるバージョン管理を意識した導入を目的としています
環境
OS: ubuntu 14.04 64bit
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.1 LTS"
ruby: 2.1.5 stable
rails: 2.1.8
必要なパッケージをインストール
$ sudo apt-get install git -y
$ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv
$ sudo apt-get install libsqlite3-dev libcurl4-openssl-dev -y
$ sudo apt-get install zlib1g-dev libssl-dev libreadline6-dev libyaml-dev libxml2-dev libxslt-dev -y
$ sudo apt-get install g++ -y
$ sudo apt-get install apache2 openssh-server apache2-prefork-dev libapr1-dev libaprutil1-dev -y
初期化コマンドを記述しておく
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ source ~/.bashrc
ruby-buildのインストール
$ git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
rbenvでインストール可能なリストを表示
$ rbenv install -l
.
.
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
2.2.0-dev
.
.
rbenvで特定バージョンのrubyをインストール
$ rbenv install 2.1.5
$ rbenv rehash
アンインストールしたい時は以下のコマンド
$ rbenv uninstall 2.1.5
$ rbenv rehash
システムで使用しているrubyのバージョンを確認
$ rbenv global
system
現在はシステム標準のrubyが割り当てられているので変更する
$ rbenv global 2.1.5
$ rbenv global
2.1.5
$ rbenv rehash
gemをインストールした後のrbenv rehashを自動化する
このままではgemをインストールまたはアンインストールするたびにrbenv rehashする必要がある
これを自動化してくれるrbenv-rehashというgemがあるのでインストール
https://github.com/ryansouza/rbenv-rehash
$ gem i rbenv-rehash
ここまででrubyはOK
オプション的な追記
rbenvで特定のプロジェクトで使用するrubyのバージョンを指定する
$ mkdir rbenv-test
$ cd rbenv-test
$ vi .ruby-version
2.1.5を追記
これで、rbenv-testディレクトリの中ではバージョン2.1.5が使用されるようになる
rbenvのアップグレード
$ cd ~/.rbenv
$ git pull
Ruby On Railsのインストール
gemがインストールされていなかったら適宜しましょう gemが自動で作成するri rdocドキュメントを無効化する
$ echo 'install: --no-rdoc --no-ri' >> ~/.gemrc
$ echo 'update: --no-rdoc --no-ri' >> ~/.gemrc
railsのインストール(version 4.1.8)
$ gem install rails --version 4.1.8
ディレクトリごとにrubyのバージョンを指定する場合は以下
railsのルートディレクトリ
$ vi .ruby-version
2.1.5
異常が起きた時の追記
rails console エラー
$ rails console
するとreadlineがないとエラーがでたので以下の対処
$ gem install rb-readline
Gemfileに以下を追記
gem 'rb-readline'
パッケージをインストール
$ bundle install
以上でOK
rubyもRailsを消したい
gem, apt-getで入れたものを削除することに加え以下を実行
$ gem uninstall railties '4.1.8'
$ rm -rf /usr/local/lib/ruby
$ rm -rf /usr/lib/ruby
$ rm -f /usr/local/bin/ruby
$ rm -f /usr/bin/ruby
$ rm -f /usr/local/bin/irb
$ rm -f /usr/bin/irb
$ rm -f /usr/local/bin/gem
$ rm -f /usr/bin/gem