LoginSignup
8
6

More than 3 years have passed since last update.

「rbenv: rails: command not found」の対処法

Posted at

発生原因

これは主にRuby(rbenv)のversionを新しくした場合に発生するそうです。
私の場合は2.5.1から2.6.4にして$ rails newした際に発生しました。

~/MyApp
❯ rails _5.1.6_ new portfolio
rbenv: rails: command not found

The `rails' command exists in these Ruby versions:
  2.5.1

状況確認

まずrbenv(2.6.4)の中身を確認してみます。

~/.rbenv/versions/2.6.4/bin
❯ ls
bundle  bundler erb     gem     irb     rake    rdoc    ri      ruby

注目すべきはbundleとrailsの有無みたいです。
私の場合はbundleは有ってrailsが無いですね。
ちなみに今まで使用していた(2.5.1)の中身を見てみると

~/.rbenv/versions/2.5.1/bin
❯ ls
annotate        gem             nokogiri        rspec           spring
aws.rb          gem2gv          pry             rubocop         sprockets
bundle          git2gv          puma            ruby            thor
bundler         htmldiff        pumactl         ruby-parse      tilt
byebug          i18n-tasks      rackup          ruby-rewrite    update_rubygems
coderay         irb             rails           ruby2gv         xml2gv
dot2ruby        launchy         rake            sass            yard
erb             ldiff           rdoc            sass-convert    yardoc
erd             listen          ri              scss            yri

こんな感じでした。当然ですがbundleもrailsも有りますね。

対処する

私の場合はrailsが無かったのでインストールします。
まずRailsチュートリアルに沿って.gemrcに対して次のコマンドを設定した後に実行しました。

~
❯ printf "install: --no-document \nupdate:  --no-document\n" >> ~/.gemrc

~
❯ gem install rails -v 5.1.6
Fetching concurrent-ruby-1.1.5.gem
Fetching thread_safe-0.3.6.gem
Fetching tzinfo-1.2.6.gem
Fetching i18n-1.8.2.gem
Fetching activesupport-5.1.6.gem
Fetching nokogiri-1.10.7.gem
Fetching mini_portile2-2.4.0.gem
...
# 省略

bundleが無かった方は$ bundle installで解決するそうです。
両方無い方は$ bundle installした後に$ gem install railsを行ってください。

動作確認

実行できるか確認してみます。

~/MyApp
❯ rails _5.1.6_ new portfolio
      create  
      create  README.md
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
...
# 省略

無事うまくいきました!

8
6
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
8
6