Why "Rails -v" and "Bundle exec rails -v" is different - ruby-on-rails

I have staging and productions servers through AWS. After a certain point of time(I think it was after I upgraded ruby version to 2.1), my staging/production servers couldn't find rails so I had to ssh in and redownload Rails and Ruby, but now when I run "rails -v" it gives me 2.3.14, but when I run "bundle exec rails -v" I get the proper 3.2.16.
I'm guessing this is because of paths, but not entirely sure how to solve this. Some of the answers I found suggested changing the .bashrc and changing the $PATH variable, but wasn't exactly sure what to change it to.
This is my .bashrc:
#PATH=$PATH:$HOME/.rvm/bin # how it was before
PATH = $PATH:$HOME/.rvm/gems/ruby-2.1.0/bin #edited this just now to see if it works. it didnt..

bundle exec rails will use the rails version defined in your Gemfile. On the other hand, a simple rails will run the latest version available on your computer.
What probably happened is this:
you had a rails version from rvm that was the same as the one used in your Gemfile (3.2.16), so you were not noticing any difference. After upgrading ruby, you have to reinstall all gems you did install on your previous version. Otherwise it will fallback to the latest available, which can be the system version (ie. not from rvm). This may explain why you have such an outdated one.
Note that a proper bundle install will install all the gems required in your Gemfile and then will do the job for you.

Related

Can't run older version of Ruby on Rails project

I'm running Ruby 2.3.0 and the project was built using 2.1.5
When I run the command
"$ rails server"
I'm given a result of
or if I change the gemfile manually to 2.3.0
I've tried
"bundle install"
but it doesn't work either, I can fix the latter problem with
"gem install XYZ -v 'version of XYZ needed'"
but there're too many gems to install manually.
I understand that this may be a ridiculously simple question and I'm really sorry but I've tried my best with finding a solution to no avail.
You need to install proper ruby version. Easiest way to do it is using rvm.
Then, with the right ruby version selected (rvm use ruby-2.1.5) you should run bundle install and then bundle exec rails server.

'rails c' doesn't work, but 'bundle exec rails console' does

I'm setting up the staging and production server on Ubuntu, deploy with Capistrano. Both server has pretty much the same setup. But for for some reason. I can access console on production server with rails c produciton. But when I try to access console on staging server using rails c staging
The program 'rails' can be found in the following packages:
* ruby-railties-3.2
* ruby-railties-4.0
Try: sudo apt-get install <selected package>
The only way to access console on my staging server is to do
bundle exec rails console staging
On staging
the rvm seems to be correct, I can see the ruby version I'm using.
the gemset that the application use is there.
the application is running fine. Just can't access console with 'rails c'
But for some reason, I can't access console with rails c. Any idea how to solve this issue?
I guess you didn't install rails globally. First, check your rails version you are currently using by bundle exec rails -v. Then, install rails globally by gem install rails -v <version>. This should solve your issue.
Run bundle install only makes rails available inside bundler. In most cases, this should be enough, because bundler helps prevent headaches of version conflicts.
A couple of questions to understand better the situation...
Have you tried running bundle install for the current ruby version set on rvm for that application on that server?
Have you updated the gems for another application/branch? sometimes that happens because the rails c command uses the latest gems available and bundle exec... locates and uses the gems tied to the Gemfile.lock.
The application should run fine because it uses the gems that the Gemfile.lock has listed.

Using specified version of rails when starting?

I have multiple version of rails installed. But when I start the server with "rails s" it always uses the latest one. Is there a way to specify the version of rails (and all dependent gems) that should be used? (And how would I specify that in Pasenger?)
Yes, I could use RVM or similar - but this I would like to avoid.
Thanks in advance!
Phusion Passenger author here.
The version of Rails that is loaded does not depend on 'rails server', and also does not depend on Passenger. RVM also has got nothing to do with it.
It only depends on your Gemfile.lock, which locks down all gem versions in your project. If you want a different version of Rails, you need to modify your Gemfile to specify that exact version, and run bundle install to update Gemfile.lock.
You can't do that with rails server command. Every project depend from a GemFile. But you can create a bash script that will move a custom specify GemFile to the default GemFile then start the server.
vi /script/rails4_to_rails3
#/bin/bash
mv /opt/web/my_app/GemFile /opt/web/my_app/GemFile.rails4
cp /script/GemFile.rails3 /opt/web/my_app/GemFile
bundle install
rails s
I'm not a Passenger Expert but in my opinion it's impossible to.

New Gems Not Found in Production with Rails 3, Bundler and RVM

I recently set up a production server for a Rails 3 app.
On the initial deploy everything worked fine. On a subsequent deploy, new gems that were added to the project are not being found. I am getting uninitialized constant and undefined method errors when I reference classes and methods from the newly added gems, respectively.
When I originally set things up I was using a gemset with RVM, but have tried to simplify things by removing the gemset and only using RVM to manage rubies.
When I run bundle list every thing is there. All of the gem are in <app_root>/shared/bundle/ruby/1.9.1/gems/
It seems like maybe it's loading the gems from some other location, like the original gemset, but I have deleted it.
Some other details, I am using Capistrano, of course, Unicorn, my GEM_HOME=/home/deployer/.rvm/gems/ruby-1.9.3-p286
Let me know if there is any other info I can provide.
Thanks.
if you use bundle install --deployment then you also have to prefix commands with bundle exec when you execute the command, like:
bundle exec rake db:migrate
If you use bundler/capistrano integration then it does it for you in capistano but in console you need to use bundle exec manually.
RVM was helping you to avoid it with rubygems-bundler gem - but you changed gemset so the generated wrappers are no more available. You would have to clean <app_root>/shared/bundle/ruby/1.9.1/gems/ and run bundle install again to regenerate the wrappers to avoid typing bundle exec again.
I have similar problems. It turns out Capistrano isn't properly restarting unicorn. SSHing into the server and stopping then starting unicorn did the job. Now to find a fix for Capistrano's shenanigans...

How do you use multiple rails versions with rbenv?

Is it possible to use multiple versions of rails using rbenv (e.g. 2.3 and 3.1)? This was easy with gemsets in rvm, but I'm wondering what the best way is to do it now that I've switched to rbenv (also, I'm looking for a way to do it without rbenv-gemset).
not sure if you got an answer to this, but I thought I'd offer what I did and it seemed to work.
So once you get rbenv installed, and you use it to install a specific ruby version, you can install multiple versions of rails to for that ruby.
STEP 1. Install whatever version(s) of rails you want per ruby version
% RBENV_VERSION=1.9.2-p290 rbenv exec gem install rails --version 3.0.11
By using the "RBENV_VERSION=1.9.2-p290" prefix in your command line, you're specifying which ruby rbenv should be concerned with.
Then following that with the "rbenv exec" command, you can install rails. Just use the version flag as in the example to specify which version you want. Not sure if you can install multiple versions in one shot, but I just run this command as many times as needed to install each version I want.
Note: This will all be managed within your rbenv directory, so it's perfectly safe and contained.
STEP 2. Build a new rails project by specifying the rails version you want.
% RBENV_VERSION=1.9.2-p290 rbenv exec rails _3.0.11_ new my_project
STEP 3. Don't forget to go into that project and set the local rbenv ruby version.
% cd my_project
% rbenv local 1.9.2-p290
Now if you want to delete this project, just delete it as normal.
If you want to delete / manage a rails version from rbenv gems, you can use regular gem commands, just prefix your command line with:
% RBENV_VERSION=1.9.2-p290 rbenv exec gem {some command}
And of course, you can delete a complete ruby version and all its shims, etc that are managed within rbenv pretty easily. I like how self contained everything is.
Hope this helps.
For reference, this is a pretty good walk through of at least some of this stuff:
http://ascarter.net/2011/09/25/modern-ruby-development.html
There is a rbenv plugin called rbenv-gemset which should behave similar to the rvm gemset-command but since rbenv was never intended to work this way, I haven't tried it.
I usually manage Rails versions with Bundler as Nathan suggested in the comments of one of the other answers. I create a Gemfile with my desired Rails version, run bundle install, create the Rails application, let it replace the Gemfile and let Bundler take over:
mkdir my-rails-app
cd my-rails-app
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '3.2.17'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
bundle update
If you want more detail, I wrote an article on my blog about it.
Hope it helps!
If you have setup ruby using rbenv the following will work.
Installing rails, the latest version (7.x as of Oct 2022)
gem install rails -v 7.0.2.4
# Find exe
rbenv rehash
To create a rails project with the latest rails version,
rails new project_1
This will create a rails application with the latest version, to verify we can see the rails version in the Gemspec file (or) see the logs during the installation,
Installing rails, 6.x.x.x version
Assuming we are going to install rails 6.0.4.8, then issue the following commands
gem install rails -v 6.0.4.8
rbenv rehash
Now, to create a rails project with 6.0.4.8 version (which is installed previously), specify the rails version along with the rails command.
rails _6.0.4.8_ new project_2
This will create a rails application with the 6.x version, to verify we can see the rails version in the Gemspec file (or) see the logs during the installation,
Other notes
Similarly, we can manage any no of rails versions in any number of
projects.
rbenv rehash Installs shims for all Ruby executables known to
rbenv
In this approach, you don't need to set or modify any ruby
environment variables.
You don't need to modify Gemspec file by yourself.
The instructions work as of Oct 2022.

Resources