g2rkiw
Last Updated: July 12, 2017
·
2.855K
· rstacruz
F8ec7f90daf8b1defb8e318d663c0f17

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

Say Thanks
Respond

3 Responses
Add your response

1478
3bcfa1481ddfb92c8deeae3846826792

Nice post!

over 1 year ago ·
1499
66f5d5b64b951b3eeb8b6c34fcb69237

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

over 1 year ago ·
29085

It is all looking that we at here for the pokemon go cheats online as we all want to have the pokeballs and pokecoins.

5 months ago ·