razzi.abuissa.net

Razzi's guide to bundler

bundler is a command line tool to install ruby gems.

It doesn’t install gems one-off; either installs from a Gemfile or writes its changes to Gemfile if you use bundle add.

In this way it’s more akin to Pipenv or Poetry from the python world than pip (that would be gem).

installing

$ sudo apt install ruby-bundler

brief digression on vendoring

Ok here’s the thing. People meme on node_modules mostly because it’s often a huge folder. But I like the approach.

Here are some dependency installation options:

  1. install dependencies system-wide, like apt does
  2. install to user’s home folder (--user-install)
  3. install to local folder (called vendoring; like node_modules)

The problems with (1), system-wide dependencies, are:

(2) is similar in that dependencies are not isolated per-project, however it has the advantage that there is no need for sudo.

But the winner here is (3), installing to a local folder. If you do the local folder approach, you get isolation, you don’t need escalated permissions, and best of all, when you want to delete your dependencies to install from a Gemfile, you just remove the vendor directory:

$ rm -r vendor/

If you are installing one application per system, for example using a container, you might find the system-wide approach works. Also, when you’re tinkering around, sometimes it’s nice to have a user-install environment and not have the boilerplate of a new project every time.

vendoring with bundler

Now that I’ve hopefully convinced you to use vendoring for your ruby projects, here’s how to configure bundler to install to a local folder:

$ bundle config path --local vendor/bundle

This will write to a file called .bundle/config. If you track this in version control you won’t have to run it if you clone the repository elsewhere.

bundler usage

Now you can install your dependencies like so:

$ bundle add sinatra

This will also install them. If you reset your vendor directory or move to a new machine, you can install dependencies from your Gemfile as follows:

$ bundle install

Then to actually use the dependencies, you’ll need to:

$ bundle exec ruby app.rb