rails rbenv: rails: command not found - ruby-on-rails

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

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

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.

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:
uninstalling rbenv, remove all references rbenv in your bash profile, and remove the remaining rbenv file folder and its contents.
Reinstall rbenv with homebrew.
Add it back to your bash profile:
export PATH="$HOME/.rbenv/bin:/usr/local/bin:$PATH"
eval "$(rbenv init -)"
Restart the shell:
exec $SHELL -l
Check the path:
echo $PATH
Install Rails:
gem install rails
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

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

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

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.

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

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.

Related

Rbenv not using the correct version

In my rails project, when I try to run bundle install, I get the following error:
Your Ruby version is 2.3.7, but your Gemfile specified 2.5.3
However, when I run ruby --version I get:
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]
...and running rbenv version gives me:
2.5.3 (set by /Users/jamesmulholland/proj/repo-name/.ruby-version)
What is cauing this the wrong version of Ruby to be used here, and how do I fix it?
Other Context and Failed Fixes
Running rbenv versions gives:
system
2.5.3 (set by /Users/jamesmulholland/proj/repo-name/.ruby-version)`
This issue has occurred during a move from Ruby 2.5.1 to Ruby 2.5.3. At the same time, I moved from rvm to rbenv. I suspect I may have uninstalled rvm incorrectly as I ran rm -rf ~/.rvm rather than rvm implode but /etc/rbenv/ is empty and running rvm commands fails. rvm is removed from my .zshrc. When I continued to run into this issue after this method of uninstalling rvm, I reinstalled rvm and uninstalled using the rvm implode process in case there were any other traces of rvm left that were causing issues. This also failed to fix the issue.
I completely removed the directory and pulled a fresh copy from GitHub. No success.
Checking out an old commit does not fix the error (I get Your Ruby version is 2.3.7, but your Gemfile specified 2.5.1 instead)
The application runs fine on my colleague's computer.
Potentially relevant section of my .zshrc (the rvm equivalent is commented out):
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
.ruby-version is 2.5.3
Gemfile contains:
source 'https://rubygems.org'
ruby "2.5.3"
Try with this.
In your rails project folder check the presence of .ruby-version file and put inside the same ruby version specified into Gemfile.
(if this file is not present, create it.)
~/your-rails-project/.ruby-version file:
2.5.3
~/your-rails-project/Gemfile file:
source 'https://rubygems.org'
ruby '2.5.3'
...
Then install that version with rbenv:
$ rbenv install 2.5.3
$ rbenv rehash
$ rbenv local 2.5.3
$ rbenv global 2.5.3
Now check that you are using the right version with:
$ ruby -v

How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting

I am getting this error while running server, how do I fix this?
You better install Ruby 2.2.5 for compatibility. The Ruby version in your local machine is different from the one declared in Gemfile.
If you're using rvm:
rvm install 2.2.5
rvm use 2.2.5
else if you're using rbenv:
rbenv install 2.2.5
rbenv local 2.2.5
else if you can not change ruby version by rbenv,
read here
If you have already installed 2.2.5 and set as current ruby version, but still showing the same error even if the Ruby version 2.3.0 is not even installed, then just install the bundler.
gem install bundler
and then:
bundle install
If you are using rbenv then make sure that you run the "rbenv rehash" command after you set local or global ruby version. It solved the issue for me.
rbenv rehash
Your Gemfile has a line reading
ruby '2.2.5'
Change it to
ruby '2.3.0'
Then run
bundle install
Had same issue. I'm using rbenv and which ruby would show the rbenv version:
/Users/Mahmoud/.rbenv/shims/ruby
which bundle though would show:
/usr/local/bin/bundle
After looking in every possible place, turns out my problem was that I needed to update path in ~/.zshrc in addition to ~/.bash_profile (where I originally had the changes)
if you're running zsh add those two lines in ~/.zshrc (or the equivalent file) in addition to ~/.bash_profile
export PATH="$HOME/.rbenv/shims:$PATH"
eval "$(rbenv init -)"
After saving, quit terminal and relaunch before retrying. Hopefully this would help.
Two steps worked for me:
gem install bundler
bundle install --redownload # Forces a redownload of all gems on the gemfile, assigning them to the new bundler
A problem I had on my Mac using rbenv was that when I first set it up, it loaded a bunch of ruby executables in /usr/local/bin - these executables loaded the system ruby, rather than the current version.
If you run
which bundle
And it shows /usr/local/bin/bundle you may have this issue.
Search through /usr/local/bin and delete any files that start with #!/user/bin ruby
Then run
rbenv rehash
I had this problem but I solved it by installing the version of the ruby that is specified in my gem file using the RVM
rvm install (ruby version)
After the installation, I use the following command to use the the version that you installed.
rvm --default use (ruby version)
You have to install bundler by using the following command in order to use the latest version
gem install bundler
After the above steps, you can now run following command to install the gems specified on the gemfile
bundle install
it can also be in your capistrano config (Capfile):
set :rbenv_ruby, "2.7.1"
Add the following to your Gemfile
ruby '2.3.0'
I am on Mac OS Sierra. I had to update /etc/paths and add /Users/my.username/.rbenv/shims to the top of the list.
If you have some dependency on the version of the Ruby , then install the appropriate version. otherwise change the version in the gemfile in the current directory.
rbenv install <required version>
rbenv local <required version>
Even after installation it was showing the same error for me, so I just restart the mac, then do the bundle install, it works :)
it should show something like this
<user>#<repo>% rbenv versions
system
* 2.3.7 (set by <app>)
For $ Your Ruby version is 2.3.0, but your Gemfile specified 2.4.1.
Changed 2.4.1 in Gemfile to 2.3.0
Refer the below link to install the required version.
https://nrogap.medium.com/install-rvm-in-macos-step-by-step-d3b3c236953b
$ \curl -sSL https://get.rvm.io | bash
rvm install 2.7.1
run:
rbenv global
if old version
then run:
1)
brew update
brew install ruby-build
2)
brew install rbenv
3)
rbenv install 2.7.5
4)
rbenv init
5)
rbenv shell 2.7.5
6)
eval "$(rbenv init - zsh)"
list commands for rbenv - run simple:
rbenv
I install rvm and rbenv it not help me so i go the project and open Gemfile change the ruby version with recommend version and than follow the command cd ios -> bundle install
Your project is ready to Run now.
Open Gemfile and find
ruby '2.2.5'
Change it to
ruby '2.3.0'
then install bundle

Bundle install not using rbenv local Ruby version

I am trying to install gems for a new Rails project using bundler:
$ bundle install --path
I've set my local Ruby version to 2.3.1 using rbenv, but bundler is still using my system Ruby (2.0.0).
$ rbenv local
2.3.1
$ echo $PATH
/Users/jenniferpierce/.rbenv/shims:/Users/jenniferpierce/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/PostgreSQL/9.5/bin
$ which ruby
/Users/jenniferpierce/.rbenv/shims/ruby
$ ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
$ bundle install --path
Your Ruby version is 2.0.0, but your Gemfile specified 2.3.1
My bash profile includes:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
I've run:
$ rbenv rehash
and restarted my terminal. Maybe I'm missing something super obvious? Any ideas would be appreciated.
To add to this, in my case, I first had to run rbenv rehash and then install the bundler gem, and then re-run bundle install. Without all of those steps, it kept the same old Ruby version.
You should be able to just run bundle install without the --path. I believe --path allows you to specify a different path than the one in your system, but if you modified your bash profile as you suggested, there is no need for that.
Also, if you just changed your ruby version for the local folder, you should run a gem install bundler.

Having trouble with Ruby versions - rbenv

I recently updated my computer to Mac OS X El Capitan and had to reinstall homebrew and rbenv.
When I go to bundle install in one of my rails app and run bundle install I get: "Your Ruby version is 2.0.0, but your Gemfile specified 2.2.0".
However, when I do ruby-v I get: ruby 2.2.0p0 . Why am I seeing two different versions and how can I change it?
rbenv -h will tell you what to do.
"rbenv versions" List all Ruby versions available to rbenv
"rbenv global" Set or show the global Ruby version
"rbenv local" Set or show the local application-specific Ruby version
to set, just do "rbenv local 2.2.0" or "rbenv global 2.2.0"
Also, there is the .ruby-version file that can also set ruby version.
Look at the instructions from rbenv main github page for more info. They do a pretty good job describing how to set the active ruby version.
https://github.com/sstephenson/rbenv
It seems that you must update your executables (bundle install), try this rbenv rehash. Take from here
rbenv rehash Installs shims for all Ruby executables known to rbenv (i.e., ~/.rbenv/versions//bin/). Run this command after you install a new version of Ruby, or install a gem that provides commands.
This is a duplicate of another question and you can find the answer here https://stackoverflow.com/a/53849574/3182171
For convenience I report also here.
Try with this.
In your rails project folder check the presence of .ruby-version file and put inside the same ruby version specified into Gemfile.
(if this file is not present, create it.)
~/your-rails-project/.ruby-version file:
2.2.0
~/your-rails-project/Gemfile file:
source 'https://rubygems.org'
ruby '2.2.0'
...
Then install that version with rbenv:
$ rbenv install 2.2.0
$ rbenv rehash
$ rbenv local 2.2.0
$ rbenv global 2.2.0
Now check that you are using the right version with:
$ ruby -v
You can now execute
$ bundle install

Ruby version issues with Rbenv

I am setting up my new machine (Mac Yosemite) and when I run the command "bundle" I get the following error:
Your Ruby version is 2.2.1, but your Gemfile specified 2.2.0
I'm trying to change the version w Rbenv but I think it's not working.
ruby -v
// Ruby 2.2.1p85
rbenv version
// 2.2.0
Can anyone help me? Thanks in advance!
Try to use rbenv local [version number].Refer to this for more understanding.
I had a similar problem but with ruby -v 2.2.0 and Mac OSX El Capitan. What finally worked for me was:
gem install bundler (apparently it was no longer installed)
rbenv rehash
bundle install
I was able to find the answer on the github for rbenv.
https://github.com/sstephenson/rbenv/issues/697
Assuming you have Ruby -v 2.2.0 installed (rbenv install 2.2.0 otherwise), change your ruby version with rbenv:
rbenv global 2.2.0
after install rails you must do this: rbenv rehash, in order to tell rbenv to see rails as executable.
follow this tutorial to set up Ruby and Ruby on Rails in your machine: https://gorails.com/setup/osx/10.10-yosemite
Note: In my case. after install rbenv the command rbenv global 2.2.0 doesn't affect my current ruby version (ruby -v still be the previous one). I fix this issue reinstalling rbenv and run this command:
# Add rbenv to bash so that it loads every time you open a terminal
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
source ~/.bash_profile
immediately afterwards, as explained above in the tutorial.

Resources