Safe way to upgrade Ruby versions and gemsets in Rails applications - ruby-on-rails

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.

Related

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.

Controlling ruby version used in 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

Having multiple versions of Rails installed on OS X

OS X 10.6 has Rails 2.2.2 installed by default.
How to upgrade to Rails 2.3.8?
Since Rails 3 is out, is it possible to have multiple versions of Rails (like 2.3.8 and 3) installed on OS X?
(For example, I could be working on two Rails projects, one is Rails 2.x and the other is Rails 3.x).
Use rvm
RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments from interpreters to sets of gems...
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...
I wouldn't bother with gemsets myself just do as other people have mentioned:
gem install -v=2.3.8 rails
and then to use:
rails _2.3.8_ new rails app
Note that this _versionnumber_ thing is a standard way of specifying a version of any rubygems installed executable.
Use
gem install -v=2.3.8 rails
where -v=[version number you want to install]
Then
list gem rails
In your config/environment.rb you can state which version of Rails you want to use.
To upgrade your OS version of the rails gem:
sudo gem install rails --version 2.3.8
While you're at it, you can also upgrade your OS rubygems system itself, overwriting the binary that OS X comes with. This might seem kind of hacky, but it's a stable thing that everyone does.
sudo gem install rubygems-update
sudo update_rubygems
Regarding managing rails versions in your projects, if your only needs are:
Specifying the version of rails that you want on a rails project
Specifying different versions of rails and/or other gems in each of multiple rails projects
Then rvm is overkill. The only thing you need, and what is also a complete and utter pleasure to work with, is Bundler. Bundler is like Software Update for your rails project. In fact, Rails 3 comes with Bundler by default, you just have to list the gems you need in Gemfile and it takes care of the rest. You don't need to worry about which versions of your gems are installed in the OS.
(You do however need the appropriate version of the rails gem installed in the OS in order to generate the rails project in the first place)
http://rubygems.org/
It's all you need =)

Resources