RVM is not working over SSH

2020-02-19 03:23发布

RVM is not working over SSH.

At the command-line:

leifg@host:~$ which ruby
/usr/local/rvm/rubies/ruby-1.9.2-p290/bin/ruby

Connected over SSH:

local:~$ ssh leifg@server 'which ruby'
/usr/bin/ruby

I'm using Ubuntu 11.04.

How do I get SSH to use the same Ruby as it is on the system?

I already verified some prequisites:

  • Ruby was already installed using apt-get install ruby. Does that make any difference?
  • sshd_config has the option "PermitUserEnvironment yes", and I restarted the daemon.

The .bashrc on the server contains these lines, but I see the same behavior when I remove them:

if [ -s "$HOME/.rvm/scripts/rvm" ] ; then
  . "$HOME/.rvm/scripts/rvm"
elif [ -s "/usr/local/rvm/scripts/rvm" ] ; then
  . "/usr/local/rvm/scripts/rvm"
fi

标签: ruby ssh rvm
10条回答
你好瞎i
2楼-- · 2020-02-19 03:32
  1. (if using Capistrano) Don't use rvm1/capistrano3 or rvm/capistrano; don't set :pty.

  2. Change ~/.rvmrc for the runner user, on the server, to this — note that it has to come before the line where it kills itself when not running interactively:

# get rvm for non-interactive shells (eg capistrano) too
source /etc/profile.d/rvm.sh
export BASH_ENV=$HOME/.bashrc
export rvm_is_not_a_shell_function=0

# If not running interactively, don't do anything
[ -z "$PS1" ] && return 
查看更多
霸刀☆藐视天下
3楼-- · 2020-02-19 03:36

From the ssh man page:

If command is specified, it is executed on the remote host instead of a login shell.

This should mean that your .bashrc won't get sourced, so RVM doesn't get set up.

Solution

This did the trick in the end:

ssh <host> bash --login -c <command>

Start bash as a login shell through SSH and then start the RVM installed Ruby via SSH's -c option.

查看更多
beautiful°
4楼-- · 2020-02-19 03:37

Actually there's totally another, more safe and lightweight option.

You add "PermitUserEnvironment yes" somewhere to your sshd_config in /etc/(open)ssh

Now you are allowed to specify user environment in /home/user/.ssh/environment. So what do you put there ?

Just something like :

user# env | grep rvm > ~/.ssh/environment

so it looks like below :

user@app3:~$ cat ~/.ssh/environment 
rvm_bin_path=/usr/local/rvm/bin
GEM_HOME=/usr/local/rvm/gems/ree-1.8.7-2012.02
IRBRC=/usr/local/rvm/rubies/ree-1.8.7-2012.02/.irbrc
MY_RUBY_HOME=/usr/local/rvm/rubies/ree-1.8.7-2012.02
rvm_path=/usr/local/rvm
rvm_prefix=/usr/local
PATH=/usr/local/rvm/gems/ree-1.8.7-2012.02/bin:/usr/local/rvm/gems/ree-1.8.7-2012.02@global/bin:/usr/local/rvm/rubies/ree-1.8.7-2012.02/bin:/usr/local/rvm/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
rvm_version=1.14.5 (stable)
GEM_PATH=/usr/local/rvm/gems/ree-1.8.7-2012.02:/usr/local/rvm/gems/ree-1.8.7-2012.02@global

Note: this also works work user-install RVM (not only for the system wide)

Now your are able to use ruby in ssh non interactive sessions :

ssh user@app3 'ruby --version'
ruby 1.8.7 (2012-02-08 MBARI 8/0x6770 on patchlevel 358) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2012.02

Voila!

查看更多
Juvenile、少年°
5楼-- · 2020-02-19 03:43

I had the same problem. I realized, that I accidentally installed RVM for multiple users, too. After deleting the directory /usr/local/rvm and edit ~/.bashrc like zoonmix suggested, the problem was solved.

查看更多
一纸荒年 Trace。
6楼-- · 2020-02-19 03:45

I've just added at the top of ~/.bashrc (for git user) this string:

[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm"
查看更多
混吃等死
7楼-- · 2020-02-19 03:48

zoomix's is the best solution. But when you change with "ruby rvm use system" in terminal or what else you get an error : Warning! PATH is not properly set up, is not at first place.... To solve that put the snippet just before the return instead of at the top of the .bashrc file (Debian Jessie here)

case $- in
*i*) ;;
  *) 
  [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
  return;; esac
查看更多
登录 后发表回答