Controlling ruby version used in Rails - ruby-on-rails

I have an app using Rails 3.0.6 which I run on two machines. I started with ruby 1.8.7 and recently installed ruby 1.9. I swapped my link in '/usr/bin/ruby' to point to the 1.9 install. When I run 'ruby --version' I get the 1.9. However, in my rails app, when I check 'RUBY_PLATFORM' it shows rails is running with 1.8.7.
How do I tell Rails which ruby environment to use? Strangely, this is not the easiest thing to search for. Probably because I don't know enough about it to form a specific question.

https://rvm.io/rvm/install
Install RVM using these instructions, then you can manage and switch between different Ruby versions and gemsets very easily. Using RVM you can have both 1.8.7 and 1.9.x (or any other version) on your system without having to constantly manually change your path and ruby alias.
Older versions of RVM used the .rvmrc file to automatically switch the ruby within a directory. Newer versions use the .ruby-version file.
To switch between rubies, just type rvm use 2.0.0 or whatever version it is you want to use. For ongoing projects it's a good idea to add the .ruby-version file in the root of the directory that contains the version string you want to use, i.e.
2.0.0

Related

Ability to switch between Ruby on Rails versions, one from rbenv, other from rvm

I first used rbenv to install ruby ver 2.2 and Rails ver 4.2 on Linux CentOS 7, then used rvm to install ruby ver 2.4 and rails ver 5.2. Is there a way to switch between these easily or do they both have to be installed using the same method for this to work?
Better you can use rvm. Rvm is the better tool to switch between many versions with the command
rvm use ruby-2.5.1 or rvm use 2.5.1
For list out all rvm just use
rvm list
within rvm, you can manage the versions nicely no need of rbenv here.
I prefer RVM, it's easy to switch between version, also you can use multiple rails version for that ruby easily
install rvm, install ruby and create a gemset with rvm use 2.4#rails_5_2 --create will create a gemset with name rails_5_2
for switching between versions. I use .ruby-version and .ruby-gemset files in my project so i don't need to specify version all the time
in the .ruby-version file write the ruby version
2.4
and inside ruby-gemset write gemset name
rails_5_2
so whenever you will run the project it takes ruby and rails version from there
you can create this two file manually or you can run this command in terminal from your application directory which will create this two files
rvm --ruby-version use 2.4#rails_5_2
I fixed this by removing RVM and installing the newer versions with rbenv instead. Having both versions installed with the same method allowed it to be easy to switch when needed. The shims automatically use the correct Rails versions for each project respectively. As far as I can tell, the Rails versions are locked in with whichever one you started the project with. You can switch the Ruby version for each project by using:
# rbenv local 2.4.1
I chose rbenv over rvm because I read several articles of people praising rbenv's lightweight approach and ease of use over rvm.
NOTE: the "#" character is the beginning of the command line in Linux CentOS 7, NOT a character that you type in.
EDIT: Thank you guys for the recommendations, though. It got me to look more into using rbenv vs. rvm.

Safe way to upgrade Ruby versions and gemsets in Rails applications

I want to upgrade our Ruby version and a bunch of gems on our production website that we are currently running. We use RVM to manage our Ruby versions and gemsets.
I know how to install a new Ruby version using RVM and install gems in a gemset. I also understand that if I run bundle install it will install my gems into the Ruby version I am running the command from.
Is there some way to pre-create a Ruby version with a gemset in it and then swap my production site to use this new Ruby version and gemset? I know this can be done manually, but is there anyway to do it with a gemfile?
Yes, you can specify the Ruby version in the Gemfile:
source 'https://rubygems.org'
ruby '2.1.3'
This allows you to set the version that would be used if you deploy to Heroku, for example. Your question does not detail your production environment, so it's not clear if it can use the Ruby version in the Gemfile.
Locally, the app will run against whichever Ruby version is active with RVM. The only way to know if it will work is to run the specs against the new Ruby version. Your question did not mention specs, but if you have a complete set of specs then this is the proper way to find out if the new Ruby version will work.
You can install new versions of Ruby and gem sets all day long without it affecting a running application. Once an application is running, it continues running under the same version of Ruby the entire time.
You can have RVM install new Rubies, manipulate gemsets, etc., as long as you don't remove the version of Ruby the application needs. If the binary disappears your app might crash/lock-up if the system needs to load something that has been deleted.
This totally depends on your setup. I am anwsering for a Passenger/Apache setup.
With Passenger and Apache, you can set my PassengerRuby in the Apache Vhost file so it uses that particular version of Ruby, I would set it to your current ruby path and restart Apache like so:
PassengerRuby <path-to-ruby>
Then you you can install your new ruby version and gems, once you install everything you can change the PassengerRuby to the new ruby path and restart Apache. This should migrate your Ruby version smoothly, since you can always fallback to your old Ruby version.

Rails appears as not installed even though it is installed

I'm new to rails. After finally getting my environment to work properly with RVM 2.0.0 rails 4.0.5, all of a sudden rails disappeared and I keep getting the message: Rails is not currently installed on this system...
When I quit terminal, reopen it, and type:
$ rails v
It shows 4.0.5
However, as soon as I change directory into my rails app and check the rails version I get the message that rails is not installed.
Any idea what's going here?
When you use rvm you have multiple versions of ruby installed. You select which one you want like this:
rvm use 2.0
or
rvm use 2.1
You can also specify a ruby version in a .ruby-version file in a directory. Then when you change to that directory in the terminal, RVM will switch versions for you.
Each version of ruby has its own set of installed gems. Rails is a gem.
Ergo, when you installed rails, you were in your default ruby version (probably the one build-in on your system). When you change directory to your rails application, RVM is kicking in to switch to the correct ruby version for you. But you don't have rails installed in that version.
So the solution is to switch to the directory for your rails app and run:
bundle install
This will install your gem bundle for your application, including the rails gems, and it will do it into the correct ruby version.

Ruby On Rails 3.2.13 - Using Different Gemsets For Rails Applications

I currently have several Rails applications running version 3.2.13 that use Ruby 1.9.3. I plan to eventually upgrade my current applications to Rails 4.0 then upgrade Ruby to 2.0. I need to time the conversion to Ruby 2.0 carefully since from my understanding Phusion Passenger will only handle one version of Ruby without some nose bleeding solutions which I do not plan to attempt to implement at this point.
I currently use one gemset for all my Rails applications. I would like to convert my Rails applications one by one from 3.2.13 to the current version of Rails 4.0 at some point. When I have tried to update one of my applications to a newer version of Rails I was told I had to do a update rails command.
What I want to do is to be able to run some of my applications using Rails 3.2.13 and others using Rails 4.0 until I am sure they will all run properly under Rails 4.0. Once I do this I will review the differences between Ruby 1.9.3 (if any) and upgrade all of them to use Rails 2.0. At this point I understand that I would just create the gemset for 2.0.0 or whatever the latest stable version of Ruby 2 is.
I have done web searches and checked the RVM website. I understand how the gemsets are assigned to the version of Ruby you want to run. However I'm not seeing anything about using gemsets with the same version of Ruby but using different versions of Rails or other combinations of gems on the same machine for different applications. Can this be done?
Any help would be appreciated.
Take a closer look! ;)
First, create a new gemset with a name corresponding to your project
https://rvm.io/gemsets/creating/
ex: rvm gemset create project_name
Then create/edit .rvmrc file in project directory: rvm use 1.9.3#project_name
One pitfall is that if you execute following commands
cd some_project
cd ../other_project
and other_project doesn't have gemset specified you'll stay in some_project gemset. To avoid that you can create .rvmrc file with default gemset in your "projects" directory (if you have one)
Project Folder
|___Gemfile
|__.ruby-gemset
|__.ruby-version
Gemfile with all the required gems mention in your project folder
.ruby-gemset should have gem set name like sample-gemset
.ruby-version file should have specific ruby version (e.g 2.0)
rvm gemset list
it will show gem set created with sample-gemset and do the
bundle install
which will install all the specific gems for this project under sample-gemset,
same you can do for other project to create other gem set, in this way you can maintain different rails/gems and ruby version for different projects with rvm.
Your Gemfile.lock nails down which version of every gem you use - you don't need to worry about gemsets.
bundle install will always install the correct versions and your app will always use the versions in Gemfile.lock, even if there are other versions floating around

How to configure Rails to use different version of Ruby?

I'm trying to deploy my Rails 3.0.1 application which works with Ruby 1.9.2.
However, I noticed that Rails 1.8.7 is installed on the server.
What steps should I take to install Ruby 1.9.2 on the server, and force my application to use it ?
I use Apache and Passenger.
I would recommend installing RVM and using that to set the version of Ruby that you want on the server.
Here's a rundown of its advantages in a production environment:
RVM allows users to deploy each
project with its own completely
self-contained and dedicated
environment--from the specific version
of ruby all the way down to the
precise set of required gems to run
the application. Having a precise set
of gems also avoids the issue of
version conflicts between projects,
causing difficult-to-trace errors and
hours of hair loss. With RVM, NO OTHER
GEMS than those required are
installed. This makes working with
multiple complex applications where
each has a long list of gem
dependencies efficient. RVM allows us
to easily test gem upgrades by
switching to a new clean set of gems
to test with while leaving our
original set intact. It is flexible
enough to even have a set of gems per
environment or development branch--or
even individual developer's taste!
It's very easy to setup and use, especially compared to managing the Ruby versions yourself. I've done both and I prefer RVM much more now. Once you have RVM installed, using Ruby 1.9.2 is as easy as:
rvm install 1.9.2
rvm use 1.9.2
And then you can check which version of Ruby your production environment is using with:
ruby -v
Assuming you use Apache, reinstall Passenger with
$ passenger-install-apache2-module
while having Ruby 1.9.2 active. If you use RVM just type rvm use 1.9.2 to make that version of Ruby active. Follow the on-screen instructions for how to modify your httpd.conf.

Resources