LoginSignup
0
0

More than 3 years have passed since last update.

RailsアプリケーションのRubyとBundlerのバージョンをアップデートする

Last updated at Posted at 2019-05-24

環境

$ cat /etc/system-release
Amazon Linux AMI release 2018.03

# プロジェクトのディレクトリ内にて
$ rbenv -v
rbenv 1.1.1-30-gc8ba27f

$ rbenv versions
* 2.5.3 (set by /var/www/myapp/.ruby-version)

$ bundle -v
Bundler version 1.17.1

# すでにアップデートされたRailsアプリケーション
$ bin/rails -v
Rails 5.2.3

はじめに

Railsアプリケーションをアップデートするにあたり、RubyとBundlerのバージョンもアップデートする必要があったので備忘録として残しておきます。

Rubyのバージョンを確認

rbenvにインストールしたいバージョンが存在するか確認します。

$ rbenv install -l

以上のコマンドで該当バージョンが出てこなければ、ruby-buildをアップデートする必要があります。

$ cd /usr/local/rbenv/plugins/ruby-build && git pull && cd -

アップデートしたいRubyのインストール

# ruby2.6.2をインストール
$ rbenv install 2.6.2

# ruby2.6.2に切り替え
$ rbenv global 2.6.2

# 再読み込みし、切り替えの反映
$ rbenv rehash

# 反映されたことを確認
$ rbenv versions
  system
  2.5.3
* 2.6.2 (set by /var/www/myapp/.ruby-version)

トラブルシューティング

インストールしたRubyのバージョンに切り替わらない

# 正しい参照先
$ which ruby
/usr/local/rbenv/shims/ruby

rbenvでRubyをインストールした場合、以上のように正しい参照先は/.rbenv/shims/rubyとなります。それ以外だと参照先が間違っている可能性があるため、PATHを通します。EC2では全てのユーザーがログイン時にrbenvを使えるようにするために、/etc/profile.d/rbenv.shにPATHを通している場合があります。

# /etc/profile.d/rbenv.shにPATHの記載があるかもしれない場合は要確認!
$ cat /etc/profile.d/rbenv.sh
export RBENV_ROOT="/usr/local/rbenv"
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"

# 以上にPATHがなければ、~/.bash_profileに記述
$ echo 'export PATH=~/.rbenv/bin:$PATH' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

# ~/.bash_profileの変更を反映
$ source ~/.bash_profile

Rubyのバージョンをアップデートしたものの、bundle installができない

Gemfile.lockに記載されているBUNDLE_WITHとbundlerのバージョンが異なる場合、bundle installをした時に以下のようなエラーが出てしまいます。

$ bundle install --path vendor/bundle -j4
・・・
find_spec_for_exe': can't find gem bundler (>= 0.a) (Gem::GemNotFoundException)
・・・

まずはGemfile.lockにて、BUNDLED WITHを確認します。確認したバージョンのbundlerをインストールします。
bundlerの参照先を確認し、/.rbenv/shims/bundlerとなっていればOKです。

# BUNDLE_WITHでbundlerのバージョンを確認
$ vim Gemfile.lock
・・・
BUNDLED WITH
   1.17.3

# Gemfile.lockに記載されているバージョンと同じbundlerを指定してインストール
$ rbenv exec gem install bundler -v 1.17.3

# 再読み込みし、切り替えの反映
$ rbenv rehash

# 参照先の確認
$ which bundler
/usr/local/rbenv/shims/bundler
0
0
1

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