Author: Matthew Raymond Billock

How to: Using Bundler To Manage Ruby Gems

Introduction

As Ruby applications grow, the number of external libraries these applications use can grow at the same rate–if not faster, in some cases. This growth can pose problems when gems install their dependencies. If two gems rely upon different versions of the same library, then installing both gems runs the risk of creating an error scenario due to gem incompatibilities. Bundler, a Ruby gem, provides you with an easy way to manage these gem and avoid conflicts, allowing you to focus on your application’s code instead of troubleshooting issues with support libraries.
.

Prerequisites

  • Ruby version – 2.2.1 or newer

Note: this article uses vim for text editing. Replace “vim” with your editor of choice if you prefer to use a different text editing tool. (And if you accidentally find yourself stuck in vim, here are some tips to help you get unstuck.)

.

Managing Complex Interdependencies

As your application grows in complexity, you may see version conflicts in your application’s gemset. These conflicts may prevent your application from functioning correctly as the number of gems you use–and the number of gem interdependencies–grows. You might consider using RVM in a more restrictive fashion, but this solution only gives you the opportunity to control the current Ruby version and associated gemset–it does not handle gem dependency issues, which can occur among different gem versions using the same base version of the Ruby language. Going through the dependencies of all gems to find these conflicts and download compatible gem versions is a long and tedious process. Luckily, we can use the Bundler gem to manage our gemsets and take the guesswork out of using shared libraries.
.

Installing Bundler to Manage Gems

To get started with Bundler, we first need to install the gem:

gem install bundler

Note, some versions of Ruby may use the form gem2.2 install bundler.

This command installs Bundler into the current Ruby execution environment. Next, you need to create a Gemfile to manage your application’s gems. Add a new file in your application’s root directory and open it for editing:

vim Gemfile

Note: Bundler, by default, looks for the filename Gemfile, capitalized exactly as shown. It will fail, for example, if you instead named your file gemfile.

You can add your gems at this point, or leave the file empty if you have no external gem dependencies. Bundler uses this Gemfile when downloading and installing gems for your application.
.

Anatomy of a Gemfile

A Gemfile gives you many different ways to manage your application’s gem sets. You can, for example, specify a new gem source in your Gemfile, which will be searched when running the install:

source 'https://rubygems.org'

You can easily add new gem dependencies by putting them on individual lines, preceded by the word gem. For example, the following line specifies nokogiri as a gem dependency for your application:

gem 'nokogiri'

You can also specify specific gem versions by using the familiar RubyGems syntax. The following line adds the Rails gem, but restricts it to Rails version 3.0.0.beta3:

gem 'rails', '3.0.0.beta3'

You can also specify gem groups that only run in specific environments. For example, the following block installs rspec, guard, and factory_girl when the Rails environment is either test or development:

group :test, :development do
    gem 'rspec'
    gem 'guard'
    gem 'factory_girl'
end

These examples are only a beginning. There are a number of other useful features of a Gemfile at bundler.io.
.

Installing All Gems With Bundler

Once you’ve populated your Gemfile with gems to install, you can install all of your current environment’s gems with the following command:

bundle install

This command parses your Gemfile and tracks down all of the indicated gems from the sources available both on your system and in the Gemfile itself. It examines all gems for shared dependencies, and then generates a new file (Gemfile.lock) that contains all of the compatible gems and their versions. Finally, it moves through the created Gemfile.lock file, installing each relevant gem. Upon completion, your application’s gems will be available to use in your application code.
.

Other Useful Bundler Commands

Aside from taking all of the headaches out of managing gems with shared dependencies, Bundler also gives you several other tools with which you can manage your application’s gem set. Below are a few example commands that you may find useful during development:

To update all gems in the Gemfile to the latest compatible version:

bundle update

In production environments, instead of dynamically resolving gems from all available sources, you might want to install just the gems specified in Gemfile.lock.

bundle install --deployment

To run a command within the context of an application’s Gemfile:

bundle exec XXXX

Without the addition of bundle exec, the command XXXX would run in the system Ruby context, which may contain incompatible gems.

You can review other available commands, and their intended uses, here.
.

Conclusion

A common problem among programming languages is the concept of “dependency hell,” which arises when two or more third-party libraries rely upon different versions of the same shared library. Bundler gives Ruby developers an easy-to-use tool for both installing and managing complex gem sets. It helps to resolve all of the dependency issues and provides you with a compatibility record for all of your application’s dependencies. Through the use of Bundler, you can quickly resolve conflicts in your consumed gems, and get back to working on the meat of your application code.

Atlantic.Net

Atlantic.net offers managed hosting services which include a layer of business-essential managed services to your hosting packages. Contact us today for more information.

.
.


How to: Adding Ruby Gems and Gem Sources

Introduction

Programming often involves repetitive tasks. Most programming languages give you some way to package common code, making it reusable across multiple applications. In this article, we’ll look at how Ruby solves the problem of code reuse in a repeatable way with Ruby Gems. We’ll also take a look at managing gem sources, giving you more options to install additional functionality libraries from various third parties.

.

Prerequisites

  • Ruby version – 2.1.2 or newer

Expanding Your App’s Capabilities

As your software code base grows more complex, you may find that you have to write the same code multiple times. This repetition is obviously inefficient since now any changes to that code need to occur in multiple places when you introduce a new feature or a bug fix. You can mitigate this issue by encapsulating the code into separate classes–which solves the reuse problem–but this solution only scales to a certain point. Once you start trying to add complex functionality–such as HTTP communication between different applications–you will often find yourself working in well-defined problem domains that have well-known solutions. So while it might be a good learning exercise to write your own solution to one of these problems, ultimately your application would be best served by using one of these pre-existing solutions.

Read More


What Is RVM (Ruby enVironment Manager or Ruby Version Manager)?

Matthew Raymond Billock September 15, 2015 by under Cloud Hosting 0 Comments
Target Audience:

This article is intended to be useful for anyone who has a need to deploy–or manage–multiple Ruby installations on a single machine. A reader should have a basic understanding of how Ruby and Ruby gems work within an operating system.

Introduction

Managing multiple Ruby environments can be a challenge. Ruby is an open-source language, so new releases can be frequent, and those releases can introduce drastic functionality changes. Couple this potential with the use of third-party Gems that may depend on functionality within specific Ruby versions, and your application can quickly find itself struggling to meet all of the dependencies it needs just to execute. RVM (alternately expanded as Ruby enVironment Manager or Ruby Version Manager) tackles this problem head-on by providing a set of command-line tools that allow you to actively control not only the version of Ruby your application uses but also the Gem sets and versions it uses. Below we’ll discuss what RVM is, touching on each of the benefits that it has to offer.
.

Managing Multiple Ruby Environments

RVM enables you to install multiple versions of the Ruby programming language on a single machine. A native install of the Ruby programming language will install the language’s binaries into a common bin directory on the operating system; thus any change to those installed binaries will affect all Ruby applications hosted on the machine. Even relatively minor version increases–such as from 2.1.4 to 2.2.1–can introduce changes to the language that break existing language behavior.
.
RVM allows you to isolate these language binaries into separately-managed environments, which can be used at-will on your system. It accomplishes this isolation by encapsulating each installation into a separate set of directories and dependencies and providing easy-to-use command line tools to quickly switch between versions. RVM handles swapping out all relevant execution paths, environment variables, and installed third-party libraries, allowing developers to focus on developing their applications instead of managing their dependencies.
.

Why Is RVM Important?

For a single small application, RVM may seem to be overkill. However, once a codebase reaches a certain level of complexity, it may be challenging to track exactly which dependencies the application relies on. Furthermore, as an application grows and leverages various Ruby Gems, these third-party libraries may introduce further dependencies, making a simple version change a complex event fraught with peril. For example, the release of version 2.1.0 introduced a breaking change in the REXML parsers that many Gems use to provide SOAP functionality. This consideration is also crucial for cloud hosted web servers, which may host multiple Ruby applications at once (such as a Resque-based delayed job server, a web server built on Rails, and a Sinatra-based admin console). Getting several different applications with several different sets of dependencies behaving properly can be a full-time job by itself. By encapsulating different Ruby language versions into separate environments, RVM allows the developer to test out new versions, deploy existing applications without worrying about breaking changes, and ensure that application infrastructure management takes as little time as possible.
.

Resolving Dependency Conflicts

The most obvious benefit gained through the use of RVM is dependency management. By encapsulating the installations of the Ruby programming language into separate command-line-driven environments, you can more easily control version dependencies. RVM, coupled with a Gem management framework like Bundler, can make language-based dependency conflicts a thing of the past–you simply need to verify that there is a version of the Gem you are working with that is compatible with your current installation. Furthermore, using RVM allows you to be confident that only the Gems you need will be installed. Each Ruby environment managed by RVM has its own set of Gems, and as such there is no cross-contamination between apps that may use Gems that affect the Ruby execution environment itself.
.

Simplifying Deployment

In addition to mitigating breaking changes and resolving dependency conflicts, RVM can ease the deployment process for Ruby applications. RVM offers features like “Named Gemsets”, which allow you to quickly and easily specify the dependencies for your Ruby application. Furthermore, Ruby uses a shared cache of gem versions, which reduces the overall disk space used by your Ruby deployment. Finally, RVM works to ensure that all of the specified dependencies are contained entirely within user space, reducing security risks and removing the need to run your application as the root user.
.

Conclusion

The above article only scratches the surface of the functionality offered by RVM. By using RVM to manage your application’s environment, you can ease deployment concerns, mitigate upgrade issues, and create a reliable set of dependencies for your Ruby application. Coupled with tools like Bundler, RVM makes installing, running, and managing a Ruby application a simple and streamlined process.

Atlantic.Net

We offer many one click cloud install applications which also offer a simple and streamlined process.   We have many popular applications like WordPress, cPanel Hosting and Docker Hosting.
.


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources

We use cookies for advertising, social media and analytics purposes. Read about how we use cookies in our updated Privacy Policy. If you continue to use this site, you consent to our use of cookies and our Privacy Policy.