Gems are little packaged Ruby applications that allow you to extend the ecosystem and reuse countless online sources.

If you don’t have Root on your Mac (work at a large company?) but you still want to use a Gem or two, then you will usually get the following error when attempting to install:

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

This is incredible annoying, but something you can’t avoid. So better find a workaround and move on!

Luckily there is a very easy workaround and it includes changing the global path for the Ruby Gems directory for your user.

Open ~/.bash_profile or ~/.bashrc (whichever one you use and prefer) and add in:

export GEM_HOME=~/rubygems/gems

This clearly means you will need to make sure that path exists, so:

mkdir ~/rubygems
mkdir ~/rubygems/gems

Now source your changed bash file like so:

source ~/.bash_profile

(..or ~/bashrc, whichever you originally used)

Sidenote: Did you know instead of using source, you can simply replace it with a dot?

. ~/.bash_profile

Now you can install Gems!

gem install xxx --verbose

In our example above, we appended the command with –verbose to see how it goes while it’s executing. This is commonly done whenever you’ve experienced an issue and would like to see more the next time around.

If you get SSL errors as follows:

ERROR:  Could not find a valid gem 'xxx' (>= 0), here is why:
          Unable to download data from https://rubygems.org/ - SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: tlsv1 alert protocol version (https://rubygems.org/latest_specs.4.8.gz)

An easy workaround to this is to appendĀ –source http://rubygems.org to the end of the gem command; this will tell it to go and grab the dependencies from the non SSL version of the site.

gem install xxx --source http://rubygems.org

Disabling SSL is not considered a “solution” but rather a workaround, but please make sure that you trust the source, otherwise it is a sure fine way to get malware and other unknown packages installed on your system.

Categories: Content