rbenv reports different version of ruby than bundler - ruby-on-rails

I'm trying to setup a rails app on a new machine and I'm having some trouble. I'm using a .ruby-version file to specify the ruby version we're using for the project which contains the following:
2.0.0-p643
Here's what happens when I try to look at what ruby versions I'm using
➜ app git:(master) ✗ rbenv versions
system
* 2.0.0-p643 (set by /Users/stephen/workspace/prm/.ruby-version)
➜ app git:(master) ✗ ruby -v
ruby 2.0.0p643 (2015-02-25 revision 49749) [x86_64-darwin14.5.0]
So far so good. And yet when I try to install my dependencies with bundler, I get this:
➜ app git:(master) ✗ bundle
Your Ruby version is 2.2.3, but your Gemfile specified 2.0.0
Why does rbenv report to me that I'm using the version I've intended by my ruby-version file yet when I try to run bundle it complains that I'm using the wrong version? I could see if it said that the patch level is causing problems because my gemfile just specifies 2.0.0 but why is it telling me that I'm using 2.2.3 when rbenv is telling me otherwise.
I tried uninstalling bundler, thinking that perhaps the bundler was the system bundler for some reason and that's what was causing the problem. Same problem.
For reference this is using the latest version of rbenv that was installed using homebrew on OSX Yosemite.

Related

Rails ignores ruby installed by rbenv

I am using ruby 2.7.6 for my project. On my laptop (KDE Neon) it was installed via rbenv and was working correctly.
After KDE was updated today I started getting
Your Ruby version is 3.0.2, but your Gemfile specified 2.7.6
To fix that I tried reinstalling both rbenv and ruby that comes with Ubuntu. Also tried rbenv rehash with no results. So far rbenv installs ruby correctly and correct vesion is displayed when I do ruby -v or bundle install in project folder but trying to run rails s gives me an error
Your Ruby version is 3.0.2, but your Gemfile specified 2.7.6
rbenv is installed into /home/some_user/.rbenv/
$PATH shows
bash: /home/some_user/.rbenv/shims:/home/some_user/.rbenv/shims:/home/some_user/.rbenv/bin:/home/some_user/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin which I am not sure is correct.
You're not running the correct rails executable. Try prefixing your rails command with bundle exec, for example if you want to run the dev server, type bundle exec rails s instead of rails s.

Trying to install Rails 6 using RVM

I have installed RVM successfully on my Mac and using it I have installed Ruby 2.6.3. I have set my current and default version of Ruby to be 2.6.3. Everything seems to be fine because when I enter ruby -v into the terminal it tells me I'm using ruby 2.6.3.
My problem comes when I try to install Rails 6, I'm entering:
gem install rails --version=6.0.0 -no-ri -no-rdoc
It seems to install correctly however when I try to confirm with:
rails -v
I get:
Rails 6 requires Ruby 2.5.0 or newer.
You're running
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64- darwin18]
Please upgrade to Ruby 2.5.0 or newer to continue.
Why am I getting this message when my ruby -v command is telling me that I am using ruby 2.6.3
Any help would be very welcome.
You're running ruby 2.3.7p456. You need to set your local ruby version to 2.6.3.
Try which ruby to see which ruby version is loaded.
rvm install 2.6.3
rvm use 2.6.3
You can then see which ruby is running with which ruby.
See the basics on the rvm docs
As an aside, I also recommend recommend rbenv over rvm; however, you can only have one installed. If you have both installed, you're going to have a bad time.
To make sure you do not have both installed try which rbenv, and that should return rbenv not found
Please edit your original question with the output of the following commands:
which -a ruby
echo $PATH (That will help debug if your $PATH is set up incorrectly.)
which rbenv
ls (in your project directory)
i also had the similar problem which i resolved by uninstalling rvm completely and then installing rbenv.

Cannot install rails version 4.2.5 on Ubuntu in WSL when I have ruby 2.2.1 active. nokogiri requires Ruby version >= 2.3.0

Trying to install rails version 4.2.5 on Ubuntu in Windows Subsystem for Linux on Windows 10 when I have ruby 2.2.1 installed and active I get the following error:
~$ gem install rails -v 4.2.5
ERROR: Error installing rails:
nokogiri requires Ruby version >= 2.3.0.
I need these versions to collaborate on a project which uses these versions.
I started by installing WSL (1 not 2) on windows 10, then downloaded and installed Ubuntu Linux. Then I installed rbenv, updated dependencies, installed ruby 2.5.1 which was fairly easy.
I had problems installing ruby 2.2.1 but got there after doing
$ sudo apt-get install -y libssl1.0-dev
to install an earlier version of libssl which ruby 2.2.1 needs.
I installed node.js and an earlier version of bundler using
$ gem install bundler -v 1.17.3
as trying
$ gem install bundler
failed with a similar error.
(I also installed ruby 2.5.1 and rails 4.2.5 on top of it with few problems)
Trying various diagnostics showed rails was not installed for ruby 2.2.1
~$ rails
rbenv: rails: command not found
The `rails' command exists in these Ruby versions:
2.5.1
(but not in ruby 2.2.1, which is where I need it)
Looking at my files in windows File Explorer (only looking) I can see that while my ruby 2.5.1 gems include rails my ruby 2.2.1 files are incomplete and do not include rails.
I think the PATH is correct.
$ echo $PATH returns
/home/johnloughran/.rbenv/plugins/ruby- build/bin:/home/johnloughran/.rbenv/shims:/home/johnloughran/.rbenv/bin:/usr/
local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local
/games:/mnt/c/Program
Files/WindowsApps/CanonicalGroupLimited.Ubuntu18.04onWindows_1804.2019.522.0_
x64__79rhkp1fndgsc: ........ :/snap/bin
I modified my .bashrc file to add various entries to the PATH which have been included above.
I repeated the whole process twice starting from scratch (uninstalling Ubuntu) with the same block at the end.
What is my best option here to try and continue?
Thanks Josien for your comments. In the first case my collaborators have these versions of Ruby and Rails working together, but its a few years now since they installed them. One, David, helped me by trying a fresh install of rails 4.2.5 on ruby 2.2.1 which ran into the same problem above with nokogiri.
He solved it by installing a lower version of nokogiri in a new test directory, which I repeated as:
test$ gem install nokogiri -v 1.9.1
This worked. Then I installed rails as
test$ gem install rails --version 4.2.5
At last .... it worked!!!! :-)
To prove it I was able to get rails to return its version correctly.
test$ rails -v
Rails 4.2.5
Then I was able to create an app as
<working-directory>$ rails new learn-ruby
which works, almost, having hit another dependency problem, but hey ... that's programming!
Re your second comment I do not know enough about this stuff to know how relevant it is that this version of ruby is deprecated and the rails version is getting there too.

Bundler using wrong Ruby version

When I try to start my Rails server, I get the following error:
$ bundle exec rails s
Your Ruby version is 2.3.7, but your Gemfile specified ~> 2.3.8
I don't understand why that happens though, since I set Ruby to 2.3.8 both using Homebrew and using rbenv. 2.3.7 is the version of the system's ruby. I am using Mac OS 10.14.4.
$ ruby -v
ruby 2.3.8p459 (2018-10-18 revision 65136) [x86_64-darwin18]
$ rbenv version
2.3.8 (set by /Users/ceasar/foo/.ruby-version)
$ which -a ruby
/Users/ceasar/.rbenv/shims/ruby
/usr/local/opt/ruby#2.3/bin/ruby
/usr/local/bin/ruby
/usr/bin/ruby
$ which -a bundle
/Users/ceasar/.rbenv/shims/bundle
/usr/local/bin/bundle
$ /usr/bin/xcodebuild -version
Xcode 10.2
Build version 10E125
$ brew list ruby#2.3
/usr/local/Cellar/ruby#2.3/2.3.8_1/bin/erb
/usr/local/Cellar/ruby#2.3/2.3.8_1/bin/gem
/usr/local/Cellar/ruby#2.3/2.3.8_1/bin/irb
/usr/local/Cellar/ruby#2.3/2.3.8_1/bin/rake
/usr/local/Cellar/ruby#2.3/2.3.8_1/bin/rdoc
/usr/local/Cellar/ruby#2.3/2.3.8_1/bin/ri
/usr/local/Cellar/ruby#2.3/2.3.8_1/bin/ruby
/usr/local/Cellar/ruby#2.3/2.3.8_1/include/ruby-2.3.0/ (25 files)
/usr/local/Cellar/ruby#2.3/2.3.8_1/lib/libruby.2.3.0.dylib
/usr/local/Cellar/ruby#2.3/2.3.8_1/lib/pkgconfig/ruby-2.3.pc
/usr/local/Cellar/ruby#2.3/2.3.8_1/lib/ruby/ (1211 files)
/usr/local/Cellar/ruby#2.3/2.3.8_1/lib/ (3 other files)
/usr/local/Cellar/ruby#2.3/2.3.8_1/share/emacs/ (7 files)
/usr/local/Cellar/ruby#2.3/2.3.8_1/share/man/ (4 files)
/usr/local/Cellar/ruby#2.3/2.3.8_1/share/ri/ (13487 files
$ bundle env | head -n 40
## Environment
```
Bundler 2.0.1
Platforms ruby, x86_64-darwin-18
Ruby 2.3.8p459 (2018-10-18 revision 65136) [x86_64-darwin18]
Full Path /Users/ceasar/.rbenv/versions/2.3.8/bin/ruby
Config Dir /Users/ceasar/.rbenv/versions/2.3.8/etc
RubyGems 2.5.2.3
Gem Home /Users/ceasar/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0
Gem Path /Users/ceasar/.gem/ruby/2.3.0:/Users/ceasar/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0
User Path /Users/ceasar/.gem/ruby/2.3.0
Bin Dir /Users/ceasar/.rbenv/versions/2.3.8/bin
Tools
Git 2.19.2
RVM not installed
rbenv rbenv 1.1.2
chruby not installed
```
## Bundler Build Metadata
```
Built At 2019-01-04
Git SHA d7ad2192f
Released Version true
```
## Bundler settings
```
build.libv8
Set for the current user (/Users/ceasar/.bundle/config): "--with-system-v8"
build.mysql2
Set for the current user (/Users/ceasar/.bundle/config): "--with-mysql-config=/usr/local/Cellar/mysql#5.7/5.7.24/bin/mysql_config"
path
Set for your local app (/Users/ceasar/Desktop/beacon/.bundle/config): "vendor/bundle"
disable_shared_gems
Set for your local app (/Users/ceasar/Desktop/beacon/.bundle/config): true
```
I don't see anything here which would make me think it shouuld be using 2.3.7.
I'm not sure what else to try.
How does Bundler decide which version of Ruby to use, and how can I configure it to use either rbenv or my homebrew Ruby installation?
How about to try to use 'rvm'?
rvm install 2.4.2
This command will install another version of ruby, 2.4.2
And you can simply check the versions which all you have as
rvm list.
After this, run the version which you wish as the below,
rvm <version> or rvm use <version>
Hope this will help you!
Summary: The wrong version of bundler causes the problem. Use a ruby version manager to install the needed ruby version. Then install the correct version of the bundler gem for your project.
Details: In my case I'm running a rails example on github that required an older version of ruby and bundler and got the error described.
To manage my different ruby versions, I'm using ruby-install and chruby both installed with homebrew.
I followed these steps to resolve the problem on macOS Catalina v10.15.7:
Resolution steps
% ruby-install 2.4.1
# open a new shell so chruby will find 2.4.1
% chruby 2.4.1
% which ruby
/Users/richardlogwood/.rubies/ruby-2.4.1/bin/ruby
% gem install bundler:1.16.1
% which bundle
/Users/richardlogwood/.gem/ruby/2.4.1/bin/bundle
% cd (to the rails project directory)
# Note: it's a rails project so there was a binstub
# for bundler, hence `bin/bundler` in the project directory*
% bin/bundler install
% rake db:{create,migrate}
% bin/rails s
Before running the above steps, the error messages told me which version of ruby and bundler I needed to install. The system bundler was the wrong version (I'm on a mac) and the initial error message using that version gave me the error below. Only after installing the needed bundler gem (see above) was the correct ruby version resolved (see above).
Errors before resolution
% bundle install
Your Ruby version is 2.6.3, but your Gemfile specified 2.4.1
% which ruby
/Users/richardlogwood/.rubies/ruby-2.4.1/bin/ruby
% which bundle
/usr/bin/bundle
Note, the required bundler version was indicated by this error message:
Could not find 'bundler' (1.16.1) required by your ... /Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:1.16.1`
I don't suggest using RVM instead of RBEnv. RBEnv is one of the most up-to-date solutions around, and most people stick with it.
Can you please try running gem update --system to update your bundler first.
Install Rbenv and run the following commands in your project directory to install 2.3.8:
rbenv install 2.3.8
rbenv local 2.3.8
bundle install --path=vendor/cache
I had rbenv installed and also installed Ruby manually at some point because of certain issues which I thought I cleaned up. But it seems I had /usr/local/bin/{bundle,bundler} still present which was evident when I did: which bundle that showed it's location not under shims (belonging to rbenv) so I simply had to remove these e.g. rm -rf /usr/local/bin/{bundle,bundler} and all was working again.

Cannot create a new Rails app

I am running Ruby -v 2.2.0
Everytime I run the rails new or rails -v I get an error that reads:
rbenv: rails: command not found
The `rails' command exists in these Ruby versions:
2.2.0
I was running Ruby 2.1.5 but just switched to 2.2.0 because of the error message. Now that I switched I am still getting the same error message. Does anyone know what the problem could be?
Every installed ruby version have separate set of gems.
This rbenv message says that rails gem installed only in 2.2.0 version but currently rbenv set to use some another version.
You can list of available rbenv commands by rbenv command in terminal. There are some useful of them which may help to solve this question:
rbenv versions will display all installed ruby versions (and show asterisk with currently selected version).
rbenv global 2.2.0 will set global Ruby version.
rbenv local 2.2.0 will set Ruby version for current folder only.
rbenv shell 2.2.0 will set Ruby for current shell session only.
It could be that proper version of Ruby not installed. Install it and Bundle:
rvm install 2.2.0
bundle install

Resources