Note: All referenced commands are using a Bash shell on a Mac OS X Terminal.
RVM (Ruby Version Manager)
What is RVM?
RVM is a platform designed to help you manage local versions of Ruby. It separates versions of Ruby and each of their associated gems that have been installed for that version.
Let’s jump right into to some uses of RVM once it has been installed.
RVM Commands
Well, we can’t manage ruby without having it in the first place. I’m going to go ahead and install some versions of Ruby.
Install the latest version of Ruby
rvm install ruby
Install a specific version of Ruby (replace X.X.X with version number)
rvm install ruby-X.X.X
For demonstration I have installed the latest version of Ruby (ruby-2.6.0) as well as two other versions (ruby-2.3.0 and ruby-2.5.3).
Return a list of currently installed versions
rvm list
Here is what this looks like when I run it on my machine.
I’m currently running the latest version. Go me. However, what if I just got assigned to a project that requires me to run any version before 2.6.0. I have a couple commands at my disposal.
Change the current version of Ruby (replace X.X.X with version number)
rvm use X.X.X
This will temporarily change the version of Ruby in my shell. Ruby will reset to my default when opening a new terminal shell. This is the best choice if I need to quickly work on a project for a short period of time without changing my default Ruby version.
Change the default version of Ruby (replace X.X.X with version number)
rvm --default use X.X.X
This will now be the default version of Ruby when I open a new terminal shell. Let’s assume this is a long term project, I’ll go with this one.
Delete a version of Ruby (replace X.X.X with version number)
rvm remove X.X.X
Ruby Gemsets
With each Ruby version comes it’s associated gems. Keep in mind when you install, change, or delete a version of Ruby, whatever gems have been installed/associated with that version go with it. Tracking individual gems locally are less critical when working on a project thanks to a Ruby Gem called Bundler.
Ruby Gems
What are Ruby Gems?
A gem is a software package that can be added to enhance functionalities of ruby programs. We already learned of one gem (bundler). I won’t go too deep into all the types of ruby gems, but they are a beautiful addition to making any Ruby project more capable.
Basic Ruby Gem Commands
Install a Ruby gem
gem install gem_name
List installed gems
gem list
Pretty simple stuff. Now, let’s look at a list of gems installed locally.
Truncated to only the first 7 gems and we can see…that’s a lot! Let’s do some uninstalling.
Note: each of these commands will prompt you if you are trying to remove a gem that is part of a dependency, how helpful!
Remove all old versions of all gems
gem cleanup
Remove all old versions of a specific gem
gem cleanup gem_name
Uninstall all versions of a gem
gem uninstall gem_name
Uninstall certain version of a gem (replace X.X.X with version number)
gem uninstall gem_name -v, X.X.X
Remove all versions less than a specified version (replace X.X.X with version number)
gem uninstall gem_name -v, '<X.X.X'
Much better! Our gems are clean as soap.
It should be noted that older versions of gems can certainly be useful due to compatibilities. However, if using Bundler, this won’t be a problem as it will simply reinstall any missing gems as necessary. Let’s learn a little more about this.
Bundler
What is Bundler?
Bundler was released in 2009 (sorry coding vets) as a Ruby gem management system for Ruby projects. It has multiple benefits:
- It allows the user to specify Ruby gems (in a gemfile) that must be installed locally before working on a project
- It allows the user to specify specific versions of Ruby gems
- It automatically installs the specified Ruby gems and any gem dependencies
- It creates a gemfile lock that notes the installed gems
Bundler Magic
To check for and install missing gems required for a project
bundle install
One command to rule them all!
So what is bundler doing behind the scenes with this single command?
- Reads the gemfile lock (if it exists)
- Reads the gemfile
- Queries RubyGems.org for a list of every version of each specified gem
- Finds gem versions allowed by the gemfile that work together including any necessary dependency gems
- Writes down those versions in the gemfile lock
- Install all gems in the gemfile lock
Powerful stuff! Bundler is a necessity in any Ruby project to keep gems nice and organized.
Congrats! Consider your ruby wrangled!
Top comments (0)