Skip to content

Automatic ruby switching #120

colszowka opened this Issue Mar 19, 2013 · 6 comments

2 participants


I have chruby installed system-wide, including the in my /etc/profile.d/ Now, I have a user called rubyuser. Consider his home dir structure:

- /home/rubyuser
  - .ruby-version # => 1.9.3
  - 187/
    - .ruby-version # => 1.8.7

When executing su - rubyuser -c "ruby -v ; cd 187 ; ruby -v" as root, I'd expect the following:

ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]
ruby 1.8.7 (2012-10-12 patchlevel 371) [x86_64-linux]

Instead, I get my system ruby, indicating that the auto-switch via PROMPT_COMMAND is not triggered.

ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]

When I execute su - rubyuser -c "chruby_auto ; ruby -v ; cd 187 ; chruby_auto ; ruby -v", I get the expected result. When I add chruby_auto to my /etc/profile.d/, at least the initial switch to 1.9.3 when switching the user is executed.

I'm not sure whether I'm maybe missing something here, but it would be great to have this work "out of the box" when auto mode is enabled. At least maybe could call chruby_auto on load to ensure that a top level .ruby-version is picked up?

This would be very useful for making sure the correct ruby is picked up when running services.


Could you run su - rubyuser -c "./scripts/" and post the diagnostic information.


It's just a pretty straight-forward vagrant box on Ubuntu 12.04.1. Here's the output of the bug_report script after running it via su

root@vagrant-ubuntu:~# su - rubyapp -c "./"

## System

    Linux vagrant-ubuntu 3.2.0-32-generic #51-Ubuntu SMP Wed Sep 26 21:33:09 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
    GNU bash, version 4.2.24(1)-release (x86_64-pc-linux-gnu) (/bin/bash)
    [exited] (/usr/bin/tmux)
    ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux] (/usr/bin/ruby)

## Environment

    RUBIES=(/opt/rubies/1.8.7-p371 /opt/rubies/1.9.3-p392 /opt/rubies/2.0.0-p0 /opt/rubies/rbx-2.0.0-rc1)

## .ruby-version


## Aliases

    alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
    alias egrep='egrep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias grep='grep --color=auto'
    alias l='ls -CF'
    alias la='ls -A'
    alias ll='ls -alF'
    alias ls='ls --color=auto'

When logged in as that user and then running bug report, there's some more environment variables related to the then auto-selected ruby 1.9.3, everything else is the same.


This is due to the fact that bash only executes PROMPT_COMMAND when in interactive mode. A workaround would be to override cd when not in interactive mode; but that would only switch Rubies after cd is invoked.


Yeah, I figured this must be due to bash not invoking that, I wonder though why not when I actually use a login shell. Probably it needs a proper terminal emulator to actually call that variable. I wonder if there is some other environment var that chruby_auto could be placed into which would make this work. Although the current way is definetely workable, I would love to have the absolutely braindead way I suggested without overwriting cd (which actually is a bit of a hack as well, since you need to actually cd somewhere for it to work, somewhat similar problem to prompt_command :), but not sure if it is even possible at all :)


This might be fixed in 0.3.5. I switched from using PROMPT_COMMAND (which only runs in interactive mode) to using trap DEBUG which runs in both interactive / non-interactive mode (but not apparently in functions).

@postmodern postmodern closed this May 28, 2013

Also, you should be using the -l option with su, so that chruby gets loaded:

su - hal -l -c "chruby; echo 'changing directory ...'; cd /vault/1/code/ronin/ronin; chruby"
changing directory ...
 * ruby-1.9.3-p392
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.