Skip to content

Instantly share code, notes, and snippets.

@smanolloff
Last active July 4, 2018 11:16
Show Gist options
  • Save smanolloff/54a787f661a3b8b5af10541f5cc5fa2c to your computer and use it in GitHub Desktop.
Save smanolloff/54a787f661a3b8b5af10541f5cc5fa2c to your computer and use it in GitHub Desktop.
MacOS: optimize RVM ruby in Sublime Text 3
#!/bin/sh
rvmdir="$HOME/.rvm"
rubyversion=$(${rvmdir}/bin/rvm-auto-ruby -e 'puts ENV.fetch("RUBY_VERSION")')
export PATH="${rvmdir}/gems/${rubyversion}/bin"\
":${rvmdir}/gems/${rubyversion}@global/bin"\
":${rvmdir}/rubies/${rubyversion}/bin"\
":${rvmdir}/bin"\
":$PATH"
export GEM_HOME="${rvmdir}/gems/${rubyversion}"
export GEM_PATH="${rvmdir}/gems/${rubyversion}:${rvmdir}/gems/${rubyversion}@global"
export MY_RUBY_HOME="${rvmdir}/rubies/${rubyversion}"
export RUBY_VERSION="${rubyversion}"
export SUBLIME=true
exec "${0%/*}/Sublime Text" $@
@smanolloff
Copy link
Author

smanolloff commented Mar 11, 2017

Using rvm-auto-ruby in sublime build systems is inefficient as it initializes the RVM environment on each invocation and has a noticeable performance impact.

My solution for Sublime Text 3 on MacOS, based on http://apple.stackexchange.com/a/51737 (commands require superuser privileges):

  1. Create file /Applications/Sublime Text.app/Contents/MacOS/setenv.sh with the contents of this gist.

  2. chmod +x "/Applications/Sublime Text.app/Contents/MacOS/setenv.sh"

  3. Edit /Applications/Sublime Text.app/Contents/Info.plist, changing the CFBundleExecutable value to setenv.sh

  4. Force update the LaunchService database in the Terminal by using the lsregister command:

    /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -v -f '/Applications/Sublime Text.app'
  5. (Re-)launch Sublime Text. Observe how the correct ruby (as set by rvm) invocations are almost instant within sublime :)

Bonus hint:

Invocations of various ruby executables (bundle, rspec, etc.) are much faster when done via ruby -S <executable> (at least on my MacOS 10.12 with updated rubygems and rvm). Haven't really investigated why, but it sometimes saves up to one second...

Try it yourself:

command time ruby -S bundle exec rake --help >/dev/null
#        1.06 real         0.59 user         0.09 sys
command time bundle exec rake --help >/dev/null
#        2.14 real         1.15 user         0.09 sys

@Startouf
Copy link

Startouf commented May 10, 2018

Thanks for the tip, my sublime text ruby config seems to have broken with the High Sierra update (it went back to using the System Ruby I guess), but with this it works again :D

@Startouf
Copy link

@smanolloff your solutions seems to break everytime there's a sublime text update and I have to reapply your patch :S Do you think there would be something less intrusive that could keep working even after an app update ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment