ayckoster
ayckoster

Reputation: 6847

How can I use the correct rbenv Ruby version with Emacs' shell-command?

Here is my current setup:

My ~/.zprofile (I am certain that the file gets executed):

# Customize to your needs...
export PATH=/Users/username/.rbenv/shims:/Users/username/.nvm/v0.9.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin

eval "$(rbenv init -)"

I want to use my globally defined ruby version with M-x shell-command (or M-!). I expect M-! ruby -v to return ruby 1.9.3p374 (2013-01-15 revision 38858) [x86_64-darwin12.3.0] but I get ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0].

If I run ruby -v in M-x shell the correct version is loaded. I suspect that M-! does not work as expected as it runs as a non interactive shell.

Is there a way to fix this, besides running something like: M-! eval "$(rbenv init -)" && ruby -v? This works, but I do not want to insert eval "$(rbenv init -)" && infront of everything I execute.

Upvotes: 4

Views: 943

Answers (1)

Chad WALSTROM
Chad WALSTROM

Reputation: 111

Your path from shell needs to be seen by Emacs, and to make that easier, use the following elisp package.

https://github.com/purcell/exec-path-from-shell

You can install this through el-get or manually by dropping it in your ~/.emacs.d/lisp directory and adding this to your ~/.emacs or ~/.emacs.d/init.el file.

(add-to-list 'load-path "~/.emacs.d/lisp")
(unless (require 'exec-path-from-shell nil 'noerror)
  (exec-path-from-shell-initialize))

The lisp file will take care of the rest.

Upvotes: 4

Related Questions