railsコマンドやrubyコマンドが使えなくなってしまった。。。
railsコマンドやrubyコマンドが使えなくなってしまった
開発中に[railsコマンド]や[rubyコマンド]急にが使えなくなってしまってかなり焦って、ハマってしまったことがあったので、こちらにまとめます。
[libvips]gemをインストールするのに以下を実行しました。
$ brew install vips
そうすると、いきなり下記のような状態になってしまった。。。。
$ higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % rails s /Users/higmonta/.rbenv/shims/rails: line 21: /usr/local/Cellar/rbenv/1.1.2/libexec/rbenv: No such file or directory $ higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % rails c /Users/higmonta/.rbenv/shims/rails: line 21: /usr/local/Cellar/rbenv/1.1.2/libexec/rbenv: No such file or directory $ higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % rails -v /Users/higmonta/.rbenv/shims/rails: line 21: /usr/local/Cellar/rbenv/1.1.2/libexec/rbenv: No such file or directory $ higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % ruby -v /Users/higmonta/.rbenv/shims/ruby: line 21: /usr/local/Cellar/rbenv/1.1.2/libexec/rbenv: No such file or directory
エラーメッセージより、以下のようにファイルやディレクトリがあるのかを確認した。
$ higmonta@higuchimiyukiyuunoMacBook-Pro shims % pwd /Users/higmonta/.rbenv/shims $ higmonta@higuchimiyukiyuunoMacBook-Pro shims % ls brakeman erb nokogiri rdoc ruby-rewrite sprockets bundle erubis oauth reek sass tapp bundler faker pry resque sass-convert thor byebug gem puma resque-scheduler scss tilt cap htmldiff pumactl resque-web scss-lint whenever capify irb racc ri sdoc wheneverize code_climate_reek kwalify rackup rspec sdoc-merge yard coderay launchy rails rubocop slim-lint yardoc coffeelint.rb ldiff rails_best_practices ruby slimrb yri dotenv listen rake ruby-parse spring $ higmonta@higuchimiyukiyuunoMacBook-Pro rbenv % pwd /usr/local/Cellar/rbenv $ higmonta@higuchimiyukiyuunoMacBook-Pro rbenv % ls 1.2.0
[/Users/higmonta/.rbenv/shims/rails]に関しては、ファイルが存在しているのが確認できました。
[/usr/local/Cellar/rbenv/1.1.2/libexec/rbenv]に関しては、[/usr/local/Cellar/rbenv]ディレクトリの下に、[1.2.0]しか存在しませんでした。
[rails]コマンドと[ruby]コマンドが使えなくなってしまったので、パスが通っているかを以下のように確認しました。
$ higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % which rails ~/.rbenv/shims/rails $ higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % which ruby ~/.rbenv/shims/ruby
パスは、通っているようですね。
rbenvでrubyがインストールされているかを以下のようにして確認しました。
$ higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % rbenv versions system * 2.5.1 (set by /Users/higmonta/workspace/fishing_cooking/.ruby-version) 2.6.3 2.6.4 2.6.5 2.6.6 3.0.0
結論と解決策
$ brew install vips
上記の際にrbenvが1.1.2から1.2.0に更新され、[rails]コマンドや[ruby]コマンドが1.1.2に依存しているパスで動いていたため、コマンドが使えなくなってしまった。
以下のように[rbenv rehash]をすると直る(rbenvのバージョンが変わってしまい、以前のバージョンのパスに依存しているrailsコマンドやrubyコマンドをrbenv rehashすることにより、~/.rbenv/shims/
にrailsコマンドやrubyコマンドがコピーされる)かもと思い以下を実行しました。
※[rbenv rehash]をすると~/.rbenv/versions/*/bin/
以下のファイル(Gemは、ここに格納される)を~/.rbenv/shims/
以下にコピーしてくれる。
コピーする理由としては、通常パスが通っているのは~/.rbenv/shims/
以下になっている為、こちらにコピーしないとGemが提供するコマンドが使えない為です。
通常開発中にGemをインストールして、~/.rbenv/versions/*/bin/
にGemが格納され、~/.rbenv/shims/
以下にコピーしないと使えないはずが使えるのは、Gemをインストールした時にrbenv rehash
も行ってくれている為です。
higmonta@higuchimiyukiyuunoMacBook-Pro fishing_cooking % rbenv rehash rbenv: cannot rehash: /Users/higmonta/.rbenv/shims/.rbenv-shim exists
上記を実行するとまたエラーになってしまいました。
rbenv rehash
を実行すると/Users/higmonta/.rbenv/shims/.rbenv-shim
が既に存在するとなってしまったので、このファイルを削除してから再度rbenv rehash
をしました。
higmonta@higuchimiyukiyuunoMacBook-Pro shims % rm ~/.rbenv/shims/.rbenv-shim shims % rbenv rehash
上記を実行したら、やっと通常通り[rails]コマンドや[ruby]コマンドが使えるようになりました!!
ポイント
rbenv rehash
のやっていること
通常[rails]コマンドやGemの提供するコマンドは、以下の部分に格納されます。
higmonta@higuchimiyukiyuunoMacBook-Pro bin % pwd /Users/higmonta/.rbenv/versions/2.5.1/bin higmonta@higuchimiyukiyuunoMacBook-Pro bin % ls bootsnap coderay listen racc rubocop scss bundle erb nokogiri rackup ruby slimrb bundler erb2slim oauth rails ruby-parse spring byebug gem pry rake ruby-rewrite sprockets chromedriver-helper html2slim puma rdoc sass thor chromedriver-update irb pumactl ri sass-convert tilt
上記の場合は、Rubyのバージョンが2.5.1の場合にその下にこのバージョンでインストールしたGemが格納される。
Rubyのバージョンが違う場合は、それに対応しているディレクトリ~/.rbenv/versions/(Rubyのバージョン)/bin
以下にインストールしたGemが格納される
通常パスが通っているのは、~/.rbenv/shims/
な為、このディレクトリ以下にファイルをコピーすることで、Gemが提供しているコマンドが使えるようになる
higmonta@higuchimiyukiyuunoMacBook-Pro shims % pwd /Users/higmonta/.rbenv/shims higmonta@higuchimiyukiyuunoMacBook-Pro shims % ls bootsnap coffeelint.rb launchy rails_best_practices ruby spring brakeman dotenv ldiff rake ruby-parse sprockets bundle erb listen rbs ruby-rewrite tapp bundler erb2slim nokogiri rdoc sass thor byebug erubis oauth reek sass-convert tilt cap faker pry resque scss typeprof capify gem puma resque-scheduler scss-lint whenever chromedriver-helper html2slim pumactl resque-web sdoc wheneverize chromedriver-update htmldiff racc ri sdoc-merge yard code_climate_reek irb rackup rspec slim-lint yardoc coderay kwalify rails rubocop slimrb yri
eval "$(rbenv init -)"
のやっていること
・(1) ~/.rbenv/shims
を環境変数PATHの先頭に追加する
・(2) コマンドの補完用のシェルスクリプトの読み込み
・(3) rbenv rehash の実行
・(4) shディスパッチャーをインストールする
参考記事
いまさらですがLinuxで環境変数を設定したい - Qiita
zshのmacでPATHを通す方法(設定ファイルに環境変数を加える) | 素人エンジニアの苦悩