RVM is not working over SSH

爱⌒轻易说出口 提交于 2019-11-28 19:20:16

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.

Actually, your ~/.bashrc will be executed. The problem is usually that one adds the

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

... snippet at the bottom of the file. However, the default .bashrc on ubuntu systems includes the following near the top

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

That call will stop executing the rest of the script and will therefore not set the proper paths. So you can either put the rvm call at the top of the file or remove the return call.

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!

“rvm” has two invocation bugs: the default installation drops the file /etc/profile.d/rvm.sh and believes any bash trick is now globally available. – This assumption is wrong.

Files in /etc/profile.d/ are “sourced” on login, but maybe not from bash, maybe not even from a shell. So the cd hook it installs is not there after the shell which runs these files exits. Actually, because of the buggy way “rvm” installs this hook, it is already gone once you run naked bash in a login-shell!

I don’t know if “rvm” supports an explicit invocation for virtual environments, without relying on cding into some directory (that I consider the second bug).

There is one sane workaround:

Make your shell source /etc/profile.d/rvm.sh from e.g. ~/.bashrc. .bashrc is executed from any non-login bash, and login-bash is usually setup to source .bashrc from those login-shell files like ~/.profile

For your ssh problem: should a proper ssh-shell not be login-shell anyway?

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"

Mentioned solutions work certainly fine, but mine was to run

source /usr/local/rvm/environments/<ruby version>@<gemset version>

at the start of the remote ssh call. Something like:

ssh -l <remote username> <server ip> "source /usr/local/rvm/environments/<ruby version>@<gemset version> ; <rest of the remote script>"
  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 

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.

Make sure that on the server you have done something like rvm --default 1.9.2 to set RVM's Ruby to be the default. Otherwise, it will always use the default system Ruby.

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!