Ruby Gem install to User directory by default
Installing Ruby gems as root
using sudo
seemed like it was opening my system up far too much, to packages I had little control over.
Gem allows you to install packages to your own user directory using the --user-install
option but it doesn’t add the directory of these ‘user’ packages to your $PATH
. Meaning that its pretty much unusable.
So I wrote some bash to solve all of this. Hopefully it becomes default in 3.0 as suggested in this Github issue, but until then you can use this:
gem() {
if [[ $1 == "install" ]]; then
gemargs="$@"
command gem install --user-install ${gemargs:7}
else
command gem "$@"
fi
}
if hash ruby 2>/dev/null; then
PATH="$(ruby -rubygems -e 'puts Gem.user_dir')/bin:$PATH"
fi
Add it to the end of your .bashrc
or .bash_aliases
(These are in your home directory).
This has been tested on Linux and should work on OS X too
Note: I am not a Ruby developer, I just use it for things like Jekyll from time to time