Ruby-on-Rails: Cannot set correct Ruby Version - ruby-on-rails

Dearest Community.
Here's an odd one, and I cannot figure this one out myself.
I am about to deploy a Ruby-on-Rails app to a production server. I like to keep all versions consistent between development and production as you do.
In my Gemfile I set the Ruby version to 2.7.1 (correctly), and on development I installed 2.7.1 and use that version locally for this specific project. All works just fine! - here's the entry:
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.2'
...
The Problem:
On production I installed and use (via rbenv) ruby 2.7.1. And I verified that the version is installed & in use! However, when it try to migrate my database or do any other task for that matter it outputs the following
Your Ruby version is 2.7.0, but your Gemfile specified 2.7.1
I do not even have 2.7.0 installed. So for the sake of it I edited the Gemfile on production to run the test. For the purpose of the test I set the ruby verions to 2.7.0 and run another command that fires up the rack. Now the message is:
Your Ruby version is 2.7.1, but your Gemfile specified 2.7.0
What is happening?! I have never ever had or saw a similar issue before. Any help is much appreciated! Thank you!
Here are the environment details:
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
$ rails -v
Rails 6.0.3.2
$ bundle -v
Bundler version 2.1.4

I find the best solution is to uninstall all versions of ruby from your ruby version manager (not familiar with rbenv, but you should be able to listed all installed versions then one by one uninstall the ones you don't need until only the one you need remains. If you need multiple versions, best setup a script that will let you quickly switch or setup isolated development environments (basically virtual machines), such that you can be sure it only has the version it needs.
After that, it will either work fine or you'll have an error message that might help pin down the root cause of the issue.

Related

Gemfile is not using the specified ruby version

I'm making a project and when I run a generate command I get the following error:
fullpath: /Users/adamgoldberg/shopify-sinatra-app/theappearsystemcontrol6
Your Ruby version is 2.3.1, but your Gemfile specified 2.2.2
Bundler::RubyVersionMismatch: Your Ruby version is 2.3.1, but your Gemfile specified 2.2.2
/Users/adamgoldberg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/definition.rb:417:in `validate_ruby!'
/Users/adamgoldberg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler.rb:91:in `setup'
/Users/adamgoldberg/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/setup.rb:19:in `<top (required)>'
/Users/adamgoldberg/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
/Users/adamgoldberg/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'bundler: failed to load command: rake (/Users/adamgoldberg/.rbenv/versions/2.3.1/bin/rake)
I have to use ruby version 2.3.1 for my project so I have attempted changing my Gemfile version.
my gemfile now contains this:
ruby "~> 2.3"
I have tried all sorts of commands so that the Gemfile recognises that I want to use a different ruby version. I have tried:
bundle update
bundle install
gem bundle install
rbenv rehash
even my Gemfile.lock says it is using 2.3.1:
RUBY VERSION
ruby 2.3.1p112
I have even deleted the project and started again.
Please help
An update: I deleted the project and restarted. here are the exact steps I took from my home directory
ruby -v: #2.3.1
git clone https://github.com/kevinhughes27/shopify-sinatra-app.git
gem install shopify-sinatra-app
shopify-sinatra-app-generator new myshop
and the same error as above appeared:
Your Ruby version is 2.3.1, but your Gemfile specified 2.2.2
My Gemfile looks like this:
source 'https://rubygems.org'
gemspec
it's practically empty... I haven't even specified the ruby version
I then ran
bundle install
bundle update
still the same error appears.
I then specified in my Gemspect the ruby version and it now looks like this:
source 'https://rubygems.org'
ruby "2.3.1"
gemspec
but still the same error appears
I don't think you can use version specifiers like that for the Ruby version. I've never seen any examples with that.
Perhaps try it like this if you want v2.3.1:
ruby "2.3.1"
The attempt you're taking here:
ruby "~> 2.3"
seems like the right approach, but you should use an exact Ruby version:
ruby "2.3.1"
It's worth mentioning that you don't necessarily need to manually specify your ruby version anywhere if you don't want to.
updated answer
i cloned the gem and tried installing it. I got the same error. I figured out where the source of the 2.2.2 dependency is. It's in the gem's source code, which for me is ~/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/shopify-sinatra-app-0.3.0. In examples/Gemfile there is a ruby '2.2.2' line.
I tried removing this line, but there was a different error installing activesupport. I think it's likely that the easiest solution for generating an app with this gem would be to install ruby 2.2.2

Upgrade Rails to latest version

I'm new to the Ruby on Rails environment and would like to get input in relation to upgrading my Rails environment from 4.0.2 to 4.2.3, which I assume is the latest version.
My Ruby version is 2.0.0p353 (2013-11-22) [i386-mingw32].
Is upgrading a straight-forward process? If not, what is the upgrade path I need to follow?
I normally use bundle install cmd to install all gems. Is there a similar command to upgrade to the latest version?
Do I need to backup my apps before I try anything?
In your gemfile change:
gem 'rails', '4.0.2'
to
gem 'rails', '4.2.3'
and in your terminal run bundle update.
If you are trying to update ONLY Rails, run: bundle update rails.

rails -v keeps saying Rails 3.2.13 after installing 4.2.2

What gives?
I typed this in gem install rails -v 4.2.2
After that, I tried rails -v, and it keeps coming out Rails 3.2.13
Why is this?
My ruby version after ruby -v
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
You can manage gems with bundler (http://bundler.io/)
Put this on your Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.2'
gem install bundler
and then
bundle install
bundle exec rails -v
If you still working without bundler, you can make
gem list
look at your rails version and then uninstall older versions
gem uninstall rails --version 3.2.13
and then
rails -v
I've encountered this before. And this is what I did.
Change version of rails
Just change the versions.
If you have multiple versions of Rails installed, you'll get conflicting information like this.
If you need to run a command with a specific version of Rails, you can specify the version as part of the command, eg.
rails _4.2.2_ -v
That will tell you you are using Rails 4.2.
Once you've created a new app by doing something like rails _4.2.2_ new app_name, then the Gemfile for that app will specify Rails 4.2.2 and you can drop the special prefix.

Bundle is switchng my rails version but I have no gem file. How to stop this?

I create a gemset with
$ rvm gemset create r3
$ rvm gemset use r3
$ gem install rails -v3.2.13
At this point
$ rails -v
now shows
$ Rails 3.2.13
but every time I do bundle with a project I've forked, I find that
rails -v
shows Rails 4.0.1 - which then gives issue with the project in question when running tests
(4.0.1 conflicts with 3.2.13).
My question is - if my Gemfile only has:
$ cat Gemfile
source 'https://rubygems.org'
gemspec
how is this happening? How can I make my command line ruby version stay at 3.2.13 and not switch to 4.0.1 Once it switches to 4.0.1 I seem to be stuck with that for that gemset and to create a 3.2.13 gemset I have to start over again.
Bundle works by finding the most up-to-date version of the gems that are compatible with the restrictions from the Gemfile. In this case, those restrictions are coming from the gemspec file, which presumably allows versions of Rails greater than 3.2.13. So it's picking the most up-to-date version of Rails allowed - which is 4.0.1. Your RVM configuration is not relevant here.
To lock your particular fork to Rails 3.2.13, just add the following:
gem 'rails', '3.2.13'
to the Gemfile in your fork. This will lock the local version to 3.2.13 when you run bundle.
if you wish to explicitly use different version of rails then the one that would be calculated from Gemfile then you need to use:
NOEXEC_DISABLE=1 rails ...
you can make it permanent for single shell session with:
export NOEXEC_DISABLE=1
rails ...
and to disable loading gems in versions calculated via Gemfile put it in you shell initialization file (like ~/.bash_profile or ~/.zlogin):
export NOEXEC_DISABLE=1
this happens because RVM installs gem rubygems-bundler which automatically analyzes your Gemfile when you run gem binaries and if a version specified via this file is available then it is loaded (even if it is specified only as dependency of your gems).

Installed Rails Gem not loading

I'm very new to Rails (and Ruby), and am having trouble installing and using gems. I'm trying to use ruby-tmdb (https://github.com/aarongough/ruby-tmdb) and there's very little documentation.
"sudo gem install ruby-tmdb" runs just fine and I can see the gem installed when I run "gem list --local"
But, when I try and run the app, I get the error "no such file to load -- ruby-tmdb".
I'm on Mac OS X Snow Leopard. Ruby 1.8.7. Rails 3.0.3. Gem 1.3.7.
Is the gem listed in your Gemfile? In Rails 3, all gem dependencies should be listed in the Gemfile, so that it is properly loaded when the app runs.
You should have something like the following line:
gem 'ruby-tmdb'
Then, run bundle install to ensure that all gem dependencies are installed, and to have Bundler save the lock file that will ensure that all copies of this application run with the same gem versions. From this point on, you will no longer have to write the require line yourself; Rails will load in all necessary gems as the environment loads.
You might get the same error even after this, but it's always worth going through the standard process to help narrow things down :)
$ sudo which gem
$ which gem
$ sudo ruby -v
$ ruby -v
Sometimes users have different gems and rubys compared to root.
A common problem is that a gem installed for ruby 1.8 by root isn't visible for the users ruby 1.9
gems for ruby 1.8 and gems for 1.9 are NOT compatible.
This probably isn't the issue but is something that may be worth considering.

Resources