36

I have recently moved from RVM to Rbenv and when attempting to execute rails I am getting an error like the one below

Pauls-Air:~ $ rails
rbenv: rails: command not found

The `rails' command exists in these Ruby versions:
  2.1.2
Yves M.
  • 29,855
  • 23
  • 108
  • 144
Boss Nass
  • 3,384
  • 9
  • 48
  • 90

8 Answers8

101

After installing a gem via the command line in a ruby version you have to execute rbenv rehash as described in the docs here and here

For example:

$ rbenv install 2.2.0
$ gem install bundler
$ rbenv rehash
$ gem install rails
$ rbenv rehash
Klaus
  • 1,591
  • 1
  • 12
  • 18
12

You need to install Rails for each Ruby version within rbenv. Try running rbenv version, I might expect that 2.1.2 is not the ruby version in use for the current project (local version) or maybe your global version.

quilligana
  • 241
  • 2
  • 7
  • 1
    Yes you are correct. That is insane! If RVM doesn't require doing the same that alone is justification to dump rbenv and switch to RVM. Does anybody know? – user2101068 Jul 08 '17 at 19:58
4

I ran into the same issue, but none of these other solutions (or any of the others I found elsewhere) worked. I was about to go back to RVM, so I decided to get rid of rbenv completely and it paved the way to the solution.

Try the following - it worked for me:

  1. uninstalling rbenv, remove all references rbenv in your bash profile, and remove the remaining rbenv file folder and its contents.

  2. Reinstall rbenv with homebrew.

  3. Add it back to your bash profile:

    export PATH="$HOME/.rbenv/bin:/usr/local/bin:$PATH"
    eval "$(rbenv init -)"
    
  4. Restart the shell:

    exec $SHELL -l
    
  5. Check the path:

    echo $PATH
    
  6. Install Rails:

    gem install rails
    
  7. rbenv rehash

Note: I consulted this for part of this answer: https://www.codementor.io/tips/3732499178/solution-for-rbenv-rails-is-not-currently-installed-on-this-system-to-get-the-latest-version-simply-type

tuomastik
  • 4,559
  • 5
  • 36
  • 48
Bob
  • 49
  • 1
  • I found that uninstalling and re-installing rbenv solved this for me. I didn't need to remove rbenv-specifics from .zshrc, but I did manually delete ~/.rbenv after uninstalling and before re-installing rbenv via homebrew. – eebbesen Sep 10 '21 at 20:58
3

Try to set up your environment with 2.1.2 version running this command line in your terminal:

$ rbenv shell 2.1.2

It works to me

brugobi
  • 259
  • 3
  • 5
2

Like for example if you want to install Ruby 2.5.3 with Rails 6 follow this way:-rbenv global 2.5.3

$ gem update --system

$ rbenv install 2.5.3

$ rbenv global 2.5.3

$ gem install rails -v 6.0.2.2

$ ruby -v

$ rails -v

1

Ensure that the .ruby-version file in your project's directory contains the same ruby version as the one you installed with rbenv.

Mohsin Mahmood
  • 3,238
  • 3
  • 21
  • 25
1

For me, I set up my environment with the listed "The `rails' command exists in these Ruby versions".

$ rbenv shell 2.1.2
$ rails -v

It works.

Asyraf
  • 609
  • 6
  • 14
  • You can also add rails to the version you are trying to use: `rbenv shell 2.7.1` then run `gem install rails`. – stormdrain Oct 22 '21 at 13:39
0

The problem is that your global ruby version doesn't match your installed somewhere locally version which is 2.1.2. Try executing anywhere in bash shell:

rbenv global 2.1.2

That way rails will be found by rbenv in your $HOME directory and anywhere else.

rbenv global

Sets the global version of Ruby to be used in all shells by writing the version name to the ~/.rbenv/version file. This version can be overridden by an application-specific .ruby-version file, or by setting the RBENV_VERSION environment variable.