Is there something similar to the Python utility virtualenv?
Basically it allows you to install Python packages into a sandboxed environment, so easy_install d
Mineshaft is a project that I've been working on for some time and am continuing development work on.
It offers the ability to both create virtual environments akin to how virtualenv works and can also install Ruby globally as well.
If you only need to install gems as non-root, try setting the GEM_HOME
environment variable. Then just run gem
.
For example:
$ export GEM_HOME=$HOME/local/gems
$ gem install rhc
I think you'll like sandbox.
No one seems to have mentioned rbenv.
I'll mention the way I do this with Bundler (which I use with RVM - RVM to manage the rubies and a default set of global gems, Bundler to handle project specific gems)
bundler install --binstubs --path vendor
Running this command in the root of a project will install the gems listed from your Gemfile, put the libs in ./vendor
, and any executables in ./bin
and all require
s (if you use bundle console
or the Bundler requires) will reference these exes and libs.
Works for me.
I recommend direnv. It is an environment switcher for the shell.
Before each prompt it checks for the existence of an ".envrc" file in the current and parent directories. If the file exists (and authorized), it is loaded into a bash sub-shell and all exported variables are then captured by direnv and then made available the current shell.
Here is how to use direnv with ruby-install
Add this to the ~/.direnvrc
use_ruby() {
local ruby_root=$HOME/.rubies/$1
load_prefix "$ruby_root"
layout_ruby
}
Install ruby-install (brew install ruby-install
) and install a bunch of rubies.
ruby-install ruby 1.9.3
ruby-install ruby 2.0.0
ruby-install ruby 2.2.0
And then make a couple of symlinks for convenience:
ln -s .rubies/1.9 ruby-1.9.3-p*
ln -s .rubies/2.0 ruby-2.0.0
ln -s .rubies/2.2 ruby-2.2.0
And finally in any project's .envrc
:
use ruby 2.0
This will put all gems under the project's .direnv/ruby
directory (makes opening gems easier). bundler will put wrapper binaries in .direnv/bin
(no more bundle exec
!).
It's also possible to use rbenv by adding the use rbenv
command in any .envrc
file. This will activate rbenv which in turn will put the ruby wrappers in the PATH.
Note that it's not necessary to install rbenv in the .bashrc or .zshrc for this to work.
Here is the most complicated .envrc that I use on ruby projects:
rvm use 1.8.7
layout ruby
PATH_add .direnv/bundler-bin
rvm is used to select the right ruby version for you
layout commands automatically set some of the usual environment variables. For now only the ruby layout exists. What it does is set the GEM_HOME environment variable and it's bin directory to your path. Because it depends on the ruby version, make sure to call it after "rvm". Since each ruby layout directories have their own GEM_HOME, you don't need to use rvm's gemsets.
PATH_add prepends and expands the given relative path. In that case, I use this to segregate the bundler binstubs from my own bin scripts with bundle install --binstubs .direnv/bundler-bin
If you want to find out what those commands exactly do, for now: cat direnv stdlib
| less