Wrangling RVM and Ruby Gems
Tags: ruby, rails, bash
Published: 2019-04-08 16:59:48

<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VkIYx3y_Bafqh-QY" /><figcaption>Photo by <a href="https://unsplash.com/@zhenhu2424?utm_source=medium&amp;utm_medium=referral">Zhen Hu</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>Note: All referenced commands are using a Bash shell on a Mac OS X Terminal.</em></p><h3>RVM (Ruby Version Manager)</h3><h4>What is RVM?</h4><p>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.</p><p>Let’s jump right into to some uses of RVM once it has been installed.</p><h4>RVM Commands</h4><p>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.</p><p><strong><em>Install the latest version of Ruby</em></strong></p><pre>rvm install ruby</pre><p><strong><em>Install a specific version of Ruby (replace X.X.X with version number)</em></strong></p><pre>rvm install ruby-X.X.X</pre><p>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).</p><p><strong><em>Return a list of currently installed versions</em></strong></p><pre>rvm list</pre><p>Here is what this looks like when I run it on my machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/239/1*URYXuqZB8n7dFhrx9I7LUQ.png" /></figure><p>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.</p><p><strong><em>Change the current version of Ruby (replace X.X.X with version number)</em></strong></p><pre>rvm use X.X.X</pre><p>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.</p><p><strong><em>Change the default version of Ruby (replace X.X.X with version number)</em></strong></p><pre>rvm --default use X.X.X</pre><p>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.</p><p><strong><em>Delete a version of Ruby (replace X.X.X with version number)</em></strong></p><pre>rvm remove X.X.X</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/240/1*D276St4de9CPMkUNdsa1Nw.png" /></figure><h4>Ruby Gemsets</h4><p>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.</p><h3>Ruby Gems</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nSa_YkdQNOs9X3PN" /><figcaption>Photo by <a href="https://unsplash.com/@mahkeo?utm_source=medium&amp;utm_medium=referral">Mahkeo</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>What are Ruby Gems?</h4><p>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.</p><h4>Basic Ruby Gem Commands</h4><p><strong><em>Install a Ruby gem</em></strong></p><pre>gem install gem_name</pre><p><strong><em>List installed gems</em></strong></p><pre>gem list</pre><p>Pretty simple stuff. Now, let’s look at a list of gems installed locally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*leerpGmtr6bdCuCtRfJmxg.png" /></figure><p>Truncated to only the first 7 gems and we can see…that’s a lot! Let’s do some uninstalling.</p><p><em>Note: each of these commands will prompt you if you are trying to remove a gem that is part of a dependency, how helpful!</em></p><p><strong><em>Remove all old versions of all gems</em></strong></p><pre>gem cleanup</pre><p><strong><em>Remove all old versions of a specific gem</em></strong></p><pre>gem cleanup gem_name</pre><p><strong><em>Uninstall all versions of a gem</em></strong></p><pre>gem uninstall gem_name</pre><p><strong><em>Uninstall certain version of a gem (replace X.X.X with version number)</em></strong></p><pre>gem uninstall gem_name -v, X.X.X</pre><p><strong><em>Remove all versions less than a specified version (replace X.X.X with version number)</em></strong></p><pre>gem uninstall gem_name -v, &#39;&lt;X.X.X&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/242/1*MzlYOVRuEp9aXiCpJgKSiQ.png" /></figure><p>Much better! Our gems are clean as soap.</p><p>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.</p><h3>Bundler</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HYry8r1dNptuBz59" /><figcaption>Photo by <a href="https://unsplash.com/@rgrzybowski?utm_source=medium&amp;utm_medium=referral">Radek Grzybowski</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>What is Bundler?</h4><p>Bundler was released in 2009 (sorry coding vets) as a Ruby gem management system for Ruby projects. It has multiple benefits:</p><ul><li>It allows the user to specify Ruby gems (in a gemfile) that must be installed locally before working on a project</li><li>It allows the user to specify specific versions of Ruby gems</li><li>It automatically installs the specified Ruby gems and any gem dependencies</li><li>It creates a gemfile lock that notes the installed gems</li></ul><h4>Bundler Magic</h4><p><strong><em>To check for and install missing gems required for a project</em></strong></p><pre>bundle install</pre><p>One command to rule them all!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xtINib8YN-757RZj" /><figcaption>Photo by <a href="https://unsplash.com/@andresiga?utm_source=medium&amp;utm_medium=referral">Andres Iga</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>So what is bundler doing behind the scenes with this single command?</p><ol><li>Reads the gemfile lock (if it exists)</li><li>Reads the gemfile</li><li>Queries RubyGems.org for a list of every version of each specified gem</li><li>Finds gem versions allowed by the gemfile that work together including any necessary dependency gems</li><li>Writes down those versions in the gemfile lock</li><li>Install all gems in the gemfile lock</li></ol><p>Powerful stuff! Bundler is a necessity in any Ruby project to keep gems nice and organized.</p><p><strong>Congrats! Consider your ruby wrangled!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=950b1c57b140" width="1" height="1">