10
7

More than 3 years have passed since last update.

【Rails】Cloud9でRVMからrbenvに移行する手順

Last updated at Posted at 2020-06-22

目的

Colud9でデフォルトで入っているRVMをアンインストールして、rbenvをインストールすること。

手順

RVMのアンインストール

早速、RVMをアンインストールします。

$ rvm seppuku

Are you SURE you wish for rvm to implode?
This will recursively remove /home/ec2-user/.rvm and other rvm traces?
(anything other than 'yes' will cancel) 
> yes
Removing rvm-shipped binaries (rvm-prompt, rvm, rvm-sudo rvm-shell and rvm-auto-ruby)
Removing rvm wrappers in /home/ec2-user/.rvm/bin
Hai! Removing /home/ec2-user/.rvm
/home/ec2-user/.rvm has been removed.

Note you may need to manually remove /etc/rvmrc and ~/.rvmrc if they exist still.
Please check all .bashrc .bash_profile .profile and .zshrc for RVM source lines and delete or comment out if this was a Per-User installation.
Also make sure to remove `rvm` group if this was a system installation.
Finally it might help to relogin / restart if you want to have fresh environment (like for installing RVM again).

念のため、アンインストールされていることを確認。

$ rvm -v
cat: /home/ec2-user/.rvm/VERSION: No such file or directory
bash: /home/ec2-user/.rvm/scripts/base: No such file or directory
cat: /home/ec2-user/.rvm/VERSION: No such file or directory
rvm  () by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]

アンインストールはされましたが、設定ファイルにRVMの定義が残ってしまっています。
(以下、参考)

$ grep rvm ~/.bashrc
rvm_silence_path_mismatch_check_flag=1 # prevent rvm complaints that nvm is first in PATH
[[ -s "$HOME/.rvm/environments/default" ]] && source "$HOME/.rvm/environments/default"
export PATH="$PATH:$HOME/.rvm/bin"
$ grep rvm ~/.bash_profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
$ grep rvm ~/.profile
export PATH="$PATH:$HOME/.rvm/bin"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
$ grep rvm ~/.zshrc
export PATH="$PATH:$HOME/.rvm/bin"

vi コマンドでRVMに関わる定義を削除していきます。
(私は、念のためにコメントアウトしました。)
コメントアウトした記載はこちら。

~/.bashrc
#[[ -s "$HOME/.rvm/environments/default" ]] && source "$HOME/.rvm/environments/default"
#export PATH="$PATH:$HOME/.rvm/bin"
~/.bash_profile
#export PATH="$PATH:$HOME/.rvm/bin"
#[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
~/.profile
#export PATH="$PATH:$HOME/.rvm/bin"
#[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
~/.zshrc
#export PATH="$PATH:$HOME/.rvm/bin"

rbenvのインストール

下記からインストールします。
(リンク先のBasic GitHub Checkoutの手順より)
https://github.com/rbenv/rbenv

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

Cloning into '/home/ec2-user/.rbenv'...
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 2852 (delta 0), reused 1 (delta 0), pack-reused 2847
Receiving objects: 100% (2852/2852), 550.44 KiB | 827.00 KiB/s, done.
Resolving deltas: 100% (1781/1781), done.

手順に従い、コンパイル。

$ cd ~/.rbenv && src/configure && make -C src

bash: /home/ec2-user/.rvm/scripts/initialize: No such file or directory
bash: /home/ec2-user/.rvm/scripts/hook: No such file or directory
make: Entering directory `/home/ec2-user/.rbenv/src'
gcc -fPIC     -c -o realpath.o realpath.c
gcc -shared -Wl,-soname,../libexec/rbenv-realpath.dylib  -o ../libexec/rbenv-realpath.dylib realpath.o 
make: Leaving directory `/home/ec2-user/.rbenv/src'

パスが通るように設定していきます。

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc

新しくターミナルを開いて、rbenvコマンドが使えることを確認しておきます。

$ rbenv -v
rbenv 1.1.2-30-gc879cb0

シェルにrbenvの初期設定をします。

$ rbenv init
# Load rbenv automatically by appending
# the following to ~/.bash_profile:

eval "$(rbenv init -)"

指示された通り、~/.bash_profile:にeval "$(rbenv init -)"を追加します。
(先ほど同様にviコマンドで編集。)
保存したら、下記のコマンドでshellを更新します。

$ source ~/.bash_profile

その後、手順に書いてある通りにターミナルを新しく開いて下記のコマンドを実行します。

$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
Checking for `rbenv' in PATH: /home/ec2-user/.rbenv/bin/rbenv
Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: /home/ec2-user/.rbenv/plugins/ruby-build/bin/rbenv-install (ruby-build 20200520-10-g157c719)
Counting installed Ruby versions: none
  There aren't any Ruby versions installed under `/home/ec2-user/.rbenv/versions'.
  You can install Ruby versions like so: rbenv install 2.2.4
Checking RubyGems settings: OK
Auditing installed plugins: OK

※もし、こちらのエラーが発生したら、再度、shellの更新もしくはターミナルの再起動で解消されるはずです。
Checking for rbenv shims in PATH: not found

※もし、こちらのエラーが発生したら、ruby-buildが必要なため、下記をのコマンドを実行します。
Checking `rbenv install' support: not found

$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

Cloning into '/home/ec2-user/.rbenv/plugins/ruby-build'...
remote: Enumerating objects: 89, done.
remote: Counting objects: 100% (89/89), done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 10984 (delta 46), reused 56 (delta 25), pack-reused 10895
Receiving objects: 100% (10984/10984), 2.31 MiB | 2.16 MiB/s, done.
Resolving deltas: 100% (7246/7246), done.

エラーが何も発生してなければ、インストールしたいRubyのバージョン(今回は2.7.1)を指定して、下記のコマンドを実行します。

$ rbenv install 2.7.1

デフォルトをインストールしたバージョンに指定します。

$ rbenv global 2.7.1

反映されていることを確認します。

$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

$ which ruby
~/.rbenv/shims/ruby

Railsのインストール

最後にRailsをインストールします。

$ gem install rails

正しくインストールされたか確認しておきます。

$ rails -v
Rails 6.0.3.2
$ which rails
~/.rbenv/shims/rails

以上です。

参考にさせていただいたサイト

https://qiita.com/TakahashiKat/items/7a45bbd5d0e675c1de3f
https://qiita.com/ozipi/items/29d7ae0b21e476dd7b8e
https://qiita.com/makoto_kw/items/cc73b628c9ef039f6e6f
https://qiita.com/Kodak_tmo/items/73147ed4f0eec54d6e94
https://qiita.com/KakeiAkihiko/items/50e7af8ca9fd711443d3
http://blog.akanumahiroaki.com/entry/2019/01/12/170000

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