RVM tips

07 Feb 2015 | | rails

"Things I find most useful and hard to remember with RVM.."

RVM Tips

Anyone who sets up a ROR rails environment sooner or later will bet directed by some link or comment to RVM. I for one initially found the website documentation hard to read and find things, but I guess that’s because I did not know RVM.

Usually when you are learning rails, the last thing you want to do is start up yet another learning task. But this is one that probably you need to do first, and save the others for when you get going.

I will list the things I find most convenient and helpful about RVM. Most for my own reminding because there are tons of sites out there that go way more into it. There are other things out there(rbenv being one), and I actually wanted to try it. But it is not compatible with RVM on the same box, so go figure. You can’t go wrong with any, just choose one.

RVM implode

This may happen to you, and it did happen to me. I somehow got my environment messed up, or I guess you could say somehow my gems and dependencies got out of sync. No matter what I tried, I kept getting some weird error message everytime I tried to build and run my application.

I figured i needed to start from scratch with a fresh install. But how?

Used rvm implode from the terminal to remove all traces of RVM. If for some reason it does not get everything, it will tell you just to manually delete your .rvm directory.

After that, just reinstall RVM. By the way, this action will also remove all of your existing GEMS etc that hopefully you installed with RVM in the first place. Install RVM(which will install ruby), and then install rails. With each you do have the option to say what version of ruby and rails you want, and in fact, in my case I went back to one that I know was working with what I was previously doing. I think somehow the latest version of Ruby was not playing well with my Jekyll.

1
2
3
4
def use_RVM
puts "rvm implode"
puts "To remove all traces of RVM installs and start over"
end

What RVM is good for

In a nutshell, RVM will let you switch between different versions of Ruby. This is good when you want to check out something under an older version(maybe you are maintaining an application that is not upgraded yet), and also when a new release comes out. You want to be cautious just in case it breaks something in all the things you are currently working on.

Secondly, RVM lets you manage your GEMS as collections. As you know a rails application uses many gems, all with versions. And with many of them having dependencies on other gems, just updating or installing one can quick lead to some kind of mess where you don’t know where you are, especially with something does not work.

So what you can do is, for each new Rails application, create a “gemset” that will be a compartmentalized environment built just for that application. In that way when you install or update the gems, it will have no impact on the other apps you may be working on.

Now to me, that always seemed a bit redundant, in that often times you are installing the same gems over and over. But in the long run, using this little bit of extra storage will pay off in a more clear and trouble-free workflow.

Here are some quick RVM commands to know before we get into setting up gemsets:

1
2
3
rvm list(to see a list or Ruby versions on your machine)
rvm gemset list(to see a list of gemsets)
rvm gemset use gemsetname(switch between gemsets)

Creating Gemsets

Let’s say you are checking out a Rails application you download from Railscasts, and its asking for some GEM version. You jump into terminal and grab it. Did you know that without using Gemsets, you just updated or changed that Gem for your other rails apps you may be working on? Thats because without Gemsets, your “rails environment” which is composed of gems may change each time you update or download a gem.

If you use the RVM gemset feature for each project, then each project will have its own specific set/versions of rails. So the way I do it, for newer stuff where I always want the latest, I use the latest gems.

But in some cases when I am testing out something that maybe be several releases old, I will set up an environment just for that app.

One way to create a gemset is as follows: Let’s say you have ruby-2.2.0 and want a gemset for your new app call “myapp”

1
2
3
rvm use ruby-2.2.0@myapp --create
gem install rails 
rails -v

This will create a gemset with the name “ruby-2.2@myapp” using ruby 2.2 and the version of rails installed. Each time before you go into your app directory, you will issue the rvm USE command, unless you create a “sticky gemset” which I recommend.

If you want a specific version of rails, change the “gem install rails” to “gem install rails –version=##.##” where ## is the version number.

Sticky Gemsets

As you saw once you create a gemset, you need to set it or “use” it each time before you go into your app directory. A better way is to create what is called a ‘sticky gemset.”

RVM gives us a convenient technique to make sure we are always using the correct gemset when we enter the project directory. It will create hidden files to designate the correct Ruby version and project-specific gemset.

Enter this command to create the hidden files:

1
rvm use ruby-2.2@myapp --ruby-version

This will create

1
2
.ruby-gemset
.ruby-version

The –ruby-version argument creates two files, .ruby-version and .ruby-gemset, that set RVM every time we cd to the project directory. Without these two hidden files, you’d need to remember to enter rvm use ruby-2.2@myapp every time you start work on your project after closing the console.

(see RVM website on using gemsets)

comments powered by Disqus(comments off)

Older · View Archive (32)

Blog Notes Blog

"First things first comes before last things last.."

Blog notes

I wrote on another post Wordpress Death about why I abandoned my previous WP based blog. It was becoming a distraction, not so much the content but rather and administration of wp itself. What do those spammers hope to accomplish anyway. They have moved from your home phone to your home website.

That blog started late 2014 and had lots of Ruby and Rails notes based on my Tealeaf Academy bootcamp experience. My previous hosting provided want $75 to restore my site once I cancelled it but I was not in the mood.

I am going to start fresh here. Rather than notes about ruby hashes or check boxes, I think this time I am going to keep it higher level, and possible blog about …well lets see where it goes.

1
2
3
def say_hello_jekyll
puts "Hello!"
end

Newer

Rails with Postgres and MySql

"If you can just get it setup, you are halfway there...or...its not the mountains ahead that wear you down, but rather the grain of sand in your shoe."