Skip to content

Instantly share code, notes, and snippets.

@juri
Created October 14, 2015 08:17
Show Gist options
  • Save juri/1bfa3880369c645c642b to your computer and use it in GitHub Desktop.
Save juri/1bfa3880369c645c642b to your computer and use it in GitHub Desktop.
Separate trees for CocoaPods or other executables installed with gem

Sandboxing gems

The problem: I need to install CocoaPods, but I don't want to litter my system with the various gems it requires.

I tried to use Homebrew Cask, but it broke with my setup where I have Homebrew installed in /opt/brew and no write access to /usr/local.

There's probably a solution that involves RVM or rbenv, but I couldn't don't know those tools and I couldn't find one that was neatly packaged and simply explained.

Solution:

  1. Start with a system with no gems installed in the user's home directory
  2. Install cocoapods with gem install --user-install cocoapods
  3. Create .localgems in your home directory: mkdir ~/.localgems
  4. Rename .gem: mv ~/.gem ~/.localgems/cocoapods
  5. Ensure ~/bin is in your $PATH
  6. For each executable in cocoapods (~/.localgems/cocoapods/ruby/2.0.0/bin/ on my system), add a shell script with the same name to ~/bin, with the following contents:
#!/bin/sh

# Runs executable with the same name from a locally installed gem copied to a new directory

INSTALL_DIR=$HOME/.localgems/cocoapods/ruby/2.0.0
export GEM_PATH=$GEM_PATH:$INSTALL_DIR
EXECUTABLE=$(basename $0)
$INSTALL_DIR/bin/$EXECUTABLE "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment