Last Updated: September 27, 2021
·
4.979K
· rstacruz

Lightweight gemsets with rbenv

Rbenv is a great lightweight alternative to RVM. If you're working on legacy pre-Bundler projects, though, you'll want gemset support, which rbenv doesn't have.

There's an rbenv-gemset plugin that works very well. Unfortunately, I don't like it because it adds around 250ms of execution time for all Ruby commands (on my machine, at least).

Gemsets via rbenv-vars

There's a plugin called rbenv-vars which lets you define environment variables for each project. It works by letting you have a .rbenv-vars file per project that may look like this:

# .rbenv-vars
PORT=8000

...and that environment variable will be available to all Ruby commands issued for that project.

Let's use it to define a custom Gem installation path for your project:

# .rbenv-vars
GEM_HOME=$HOME/.rbenv/gems/myproject
GEM_PATH=$HOME/.rbenv/gems/myproject

And of course, let's create that path first using:

$ mkdir -p ~/.rbenv/gems/myproject

Testing it

Now, Ruby commands (most notably gem, ruby) will be invoked with those gem paths. Let's try it:

$ gem list

**** LOCAL GEMS ****

Your gemset should now be pristine and empty.

$ gem environment

RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.23
  - GEM PATHS:
     - /Users/rsc/.rbenv/gems/myproject
....

Caveats

Executables (like rails) will need to be invoked manually, eg, ~/.rbenv/gems/myproject/bin/rails.

If this bothers you, possible ways around this are:

  • Sucking it up and just typing the long commands

  • Putting your gem path inside your project path, ie, GEM_HOME=./.gemset, and adding ./.gemset/bin to your $PATH

  • Making a small bash script that adds it to your path (export PATH=$HOME/.rbenv/gems/myproject/bin:$PATH) and invoking it whenever you're working on that project

2 Responses
Add your response

Nice post!

over 1 year ago ·

chgems (https://github.com/postmodern/chgems#readme) does light-weight gemsets much better. Worth checking out.

over 1 year ago ·