Setting the environment variable for Ruby

Hello!

I have some Ruby scripts that I’m executing from Keyboard Maestro through “Execute Shell Script” action pointing to a .rb file. Up until some time I was using OS X’s default Ruby installation and everything worked perfectly well but I started to have some problems with Ruby gems installation and updating so, following the recommendations found online, I’ve switched to RVM.

Outside of KM, everything works OK but now my scripts (when executed from KM) don’t work properly. I’ve researched this topic a little and found this KM’s Wiki article. My knowledge of Ruby is mostly about scripting and I’m not a developer per se therefore I wasn’t sure how can I follow those instructions to fix the scripts in my KM installation.

Some technical details:

  • I’m using KM 7.0.2
  • I’m using it on OS X 10.11.2
  • My RVM installation uses ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin12.0]

Executing a .rb script file from KM containing:

#!/usr/bin/env ruby
require 'thor'
puts "This worked out OK!"

results in error:

/Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- thor (LoadError)
	from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	from /Users/pglaz/Documents/IT/Ruby/test.rb:3:in `<main>'

If I run gem list from KM I get:

bigdecimal (1.2.0)
CFPropertyList (2.2.8)
io-console (0.4.2)
json (1.7.7)
libxml-ruby (2.6.0)
minitest (4.3.2)
nokogiri (1.5.6)
psych (2.0.0)
rake (0.9.6)
rdoc (4.0.0)
sqlite3 (1.3.7)
test-unit (2.0.0.0)

(complete list)

While running the same from OS X Terminal gives me:

*** LOCAL GEMS ***

activesupport (4.2.5, 4.1.8)
(...)
rubygems-bundler (1.4.3)
rvm (1.11.3.9)
safe_yaml (1.0.4)
sass (3.4.19, 3.4.9)
terminal-table (1.4.5)
test-unit (2.1.1.0)
thor (0.19.1)
(...)

(list shortened for readability)

So it seems obvious that I need to use the ENV_PATH variable in KM but I’m totally lost as I’m not entirely sure how to name it (should it be ENV_RUBYLIB?) as well as what contents it should have.

I have tried adding the following: a variable with name of ENV_RUBYLIB containing this:

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/pglaz/.rvm/gems/ruby-2.1.1/bin:/Users/pglaz/.rvm/gems/ruby-2.1.1@global/bin:/Users/pglaz/.rvm/rubies/ruby-2.1.1/bin/:/Users/pglaz/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

If I run env from KM, I get:

SHELL=/bin/bash
TMPDIR=/var/folders/4m/8536ytj54rd_q_r9vb6mtld00000gn/T/
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.l3AvNJhZsn/Render
KMVAR_SilverWiz_Work_Choice=Start
KMVAR_SilverWiz_Work_State=working
USER=pglaz
COMMAND_MODE=unix2003
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.Clh5RRXie5/Listeners
KMVAR_SilverWiz_Work=0
__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0
PATH=/usr/bin:/bin:/usr/sbin:/sbin
_=/usr/bin/env
PWD=/
KMVAR_Result_Button=Yes
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
KMVAR_GLAZ_WD_Backup_State=1
HOME=/Users/pglaz
SHLVL=2
LOGNAME=pglaz
RUBYLIB=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/pglaz/.rvm/gems/ruby-2.1.1/bin:/Users/pglaz/.rvm/gems/ruby-2.1.1@global/bin:/Users/pglaz/.rvm/rubies/ruby-2.1.1/bin/:/Users/pglaz/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
SECURITYSESSIONID=186a7

Thank you for giving it a thought!

I’ve found a fix. I was aware that I need to enter ENV_PATH in Keyboard Maestro variables but I didn’t know I also need to enter other variables. To use RVM Ruby installation in Keyboard Maestro you need to add the following variables:

  • ENV_GEM_HOME
    $PATH:/Users/username/.rvm/gems/ruby-2.1.1:/Users/username/.rvm/gems/ruby-2.1.1@global

  • ENV_GEM_PATH
    $PATH:/Users/username/.rvm/gems/ruby-2.1.1:/Users/username/.rvm/gems/ruby-2.1.1@global

  • ENV_PATH
    $PATH:/Users/username/.rvm/gems/ruby-2.1.1/bin:/Users/username/.rvm/gems/ruby-2.1.1@global/bin:/Users/username/.rvm/rubies/ruby-2.1.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/username/.rvm/bin

  • ENV_RUBYLIB
    $PATH:/Users/username/.rvm

Hey Pawel,

That's hardly shocking. Any software installation that plays with the environment will have similar problems, because Keyboard Maestro runs a bare shell without any of the modifications that might be added to your ~/.profile or other customization files.

You can manually load your .profile though:

source ~/.profile;

-Chris

1 Like