I'm using ruby 1.8 and have gem https://github.com/delano/rye installed that need to remote ssh to amazon.
require 'rubygems'
require 'rye'
instance = Rye::Box.new('amazone_ip', :user => "ubuntu", :safe => false, :keys => "amazone_key")
I got errors by running those commands:
instance.execute 'rvm list' #bash: rvm: command not found
instance.execute 'ruby -v' #bash: ruby: command not found
It's ok if I run the command instance.execute ls -la.
I would think there is problem with $PATH load, that I have checked it
instance.execute 'echo $PATH'
#[/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games, , 0, ]
#But it's in server:
$echo $PATH
#/home/ubuntu/.rvm/gems/ruby-1.9.2-p290/bin:/home/ubuntu/.rvm/gems/ruby-1.9.2-p290#global/bin:/home/ubuntu/.rvm/rubies/ruby-1.9.2-p290/bin:/home/ubuntu/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
I'll wager you don't have rvm loading because its either not loading your local .bashrc or you don't have the RVM loading line (sourcing line) in your .bashrc, its in your .bash_profile. Just move the RVM line to your .bashrc and then source the .bashrc at the end of your .bash_profile.
If its calling bash in sh-compatible mode, then you can set BASH_ENV in your .profile and point it at your .bashrc which will then load RVM that way.
Try
instance.execute('source ~/.bash_profile && rvm list')
In case someone else finds it useful as this is an old question -
For a non-login shell the user's paths and environment variables are not loaded.
I needed to add this in my .bashrc file right at the very top.
[[ -s "/home/douser/.rvm/scripts/rvm" ]] && source "/home/douser/.rvm/scripts/rvm"
Related
I'm using Ubuntu 12.04.4 TLS
I wrote a simple shell script in /home/abdo/sample_serv.sh that executes a Ruby file:
#!/bin/bash
/home/abdo/.rvm/rubies/ruby-2.1.0/bin/ruby /home/abdo/sample_serv.rb
and /home/abdo/sample_serv.rb containing the code below:
puts $:
require 'sinatra'
set :port, 8084
get '/' do
%{ <html><body>Hello from Abdo</body></html> }
end
Executing rvmsudo ./home/abdo/sample_serv.sh works just fine but I would like to get sudo ./home/abdo/sample_serv.sh to work by passing the necessary environment variables because I am having issues with upstart.
The issue arises in my /etc/init/foo.conf
description "webserver test"
start on runlevel [23]
stop on shutdown
pre-start script
exec >> /var/log/unicorn_test.log 2>&1
echo starting
end script
script
exec >> /var/log/unicorn_test.log 2>&1
/bin/bash /home/abdo/.rvm/bin/rvmsudo /home/abdo/sample_serv.sh
echo started
end script
When the line /bin/bash /home/abdo/.rvm/bin/rvmsudo /home/abdo/sample_serv.sh is reached, I get
/home/abdo/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
`require': cannot load such file -- sinatra (LoadError)
I was able to get the same ruby -v as that of my user but it looks like the GEM_PATH (even if I set it inside the upstart config file), is not helping.
Basically, if I can understand how (and where -- gem env is not helping since gem is not a command when I do sudo) rubygems looks for a file being required, I should be able to move forward.
When you run under sudo you run in a different environment, where gem is not installed, the $PATH is different, so things may not run smoothly.
You may want to try this:
Changing sudo's strict defaults
There are 3 things needed to mitigate this situation if you encounter
it:
the user that is invoking sudo must have export rvmsudo_secure_path=0 set on his shell environment (think .bashrc,
.bash_profile or .zshrc)
comment out Defaults secure_path=... on /etc/sudoers
add Defaults env_keep +="rvm_bin_path GEM_HOME IRBRC MY_RUBY_HOME rvm_path rvm_prefix rvm_version GEM_PATH rvmsudo_secure_path
RUBY_VERSION rvm_ruby_string rvm_delete_flag" to /etc/sudoers in
rare cases it is required to add more variables - they should be
reported by first run of rvmsudo.
After these changes, you should be able to use rvmsudo preserving
the same password/no-password directives as "normal" sudo calls.
Edit
If you don't want to change the defaults, you can try to synchronize the values of the environment values stated above (rvm_bin_path GEM_HOME IRBRC MY_RUBY_HOME rvm_path rvm_prefix rvm_version GEM_PATH rvmsudo_secure_path RUBY_VERSION rvm_ruby_string rvm_delete_flag) to your root user.
If that doesn't work, you can try installing rvm as root, and using that environment (instead of /home/abdo/.rvm/bin/rvm) to run your code.
When I try to use rvm in fish shell, I get this message:
ciembor#ciembor ~> rvm use 1.9.2
RVM is not a function, selecting rubies with 'rvm use ...' will not
work.
You need to change your terminal emulator preferences to allow login
shell. Sometimes it is required to use /bin/bash --login as the
command. Please visit https://rvm.io/integration/gnome-terminal/ for a
example.
I get used to use /bin/bash --login, then rvm and then starting fish from bash. But isn't there more straightforward way? I use xfce4 terminal.
I had the same issue. Download the rvm fish function from GitHub:
curl --create-dirs -o ~/.config/fish/functions/rvm.fish https://raw.github.com/lunks/fish-nuggets/master/functions/rvm.fish
Reference: http://rvm.io/integration/fish
Download the fish functions from GitHub.
curl -L --create-dirs -o ~/.config/fish/functions/rvm.fish https://raw.github.com/lunks/fish-nuggets/master/functions/rvm.fish
And activate the default Ruby manually in your config.fish file:
echo "rvm default" >> ~/.config/fish/config.fish
And you're done
Try look at bash "initialization" files like ~/.bashrc ~/.bash_profile and session "initialization" files ~/.profile /etc/profile* and add rvm related code(something like
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
) to fish "initialization" file ~/.config/fish/config.fish
Initialize rbenv and run ruby script from shell script
I want svnserve to run pre-commit hook, written on ruby. As the svnserve is run as root user, it knows nothing about user rbenv installation.
I set a soft link /usr/bin/ruby -> /home/admin/.rbenv/shims/ruby .
As result, when i try
#!/usr/bin/ruby
puts "Pre-commit hook!"
It shows error:
Transmitting file data .svn: Commit failed (details follow):
svn: Commit blocked by pre-commit hook (exit code 255) with no output.
When i run manually on Server:
admin $ sudo ./pre-commit
/usr/bin/ruby: line 4: exec: rbenv: not found
So, i suppose, that rbenv initialization is needed, but how?
In hooks path:
pre-commit:
#!/bin/bash
export HOME=/home/user
if [ -d $HOME/.rbenv ]; then
export PATH="$HOME/.rbenv/shims:$PATH"
eval "$(rbenv init -)"
fi
$0.rb $*
pre-commit.rb:
#!/usr/bin/env ruby
ARGV.each_with_index { |arg, index| puts "Index #{index}: Argument #{arg}" }
you should initialize rbenv before using it.
/path/to/user/home/.rbenv/bin/rbenv init
then set ruby version globally:
rbenv global DESIRED-RUBY-VERSION
or localy:
rbenv local DESIRED-RUBY-VERSION
or per shell:
rbenv shell DESIRED-RUBY-VERSION
though not sure shell setting will work without a tty.
so you could create a shell script, pre-commit.sh and register it as a svn hook.
inside it initialize rbenv and call your pre-commit.rb file
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
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.
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 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>"
(if using Capistrano) Don't use rvm1/capistrano3 or rvm/capistrano; don't set :pty.
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
I'm having trouble getting the Ruby Version Manager rvm to source from my Ubuntu 10.04 .profile. The code:
[[ -s "$HOME/.rvm/scripts/rvm" ] && . "$HOME/.rvm/scripts/rvm"
...never does what I expect it to (i.e. give me the rvm program when I open a new shell or start a new session); but if I execute
source .profile
in a new shell after logging in, it works! Why will it work when I manually source it, but not automatically at login?
It would appear that Ubuntu handles it's logon scripts differently than most other linux distros
http://ubuntuforums.org/showpost.php?p=9127226&postcount=6
The above post has hints that GDM logins in Ubuntu don't process .bash_profile or .profile the way most other linux distros do. I have had to put the line loading RVM in the ~/.bashrc and that has not caused any problems yet.
Sourcing $HOME/.rvm assumes you have installed RVM a single user, specially, the user whose home directory is $HOME. Likely, on your Ubuntu system, RVM has been installed system wide, and thus you must source the RVM scripts as such:
In your .bashrc file add:
\# Set rvm path
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm"
before this line; this line will exit and not execute anything past it, which is fine for interactive logins, bit would be a problem is you are using non-interactive SSH logins for automation purposes.
\# If not running interactively, don't do anything
[ -z "$PS1" ] && return
The RVM installation page has a series of things to check to test the initialization of RVM. Read the "Troubleshooting your Install" section at the end of the RVM installation page.
Also, here's a description of how Bash reads its startup files which can help with this sort of problem.
I had a problem with Atom editor not picking up RVM environment and thus not finding rubocop command on Ubuntu 16.04. But the problem was not there when I started Atom from gnome terminal. What I've found was that RVM script ~/.rvm/scripts/rvm that you're supposed to be loading in your .profile has these lines at the beginning:
if
builtin test -n "${BASH_VERSION:-}" -o -n "${ZSH_VERSION:-}" -o -n "${KSH_VERSION:-}"
then
...
else
return 0
fi
Strangely, when executed at login, I've found $BASH_VERSION to be empty (while in gnome terminal it's sth like 4.3.46(1)-release), so the script would do early return leaving RVM not loaded properly. I tried to set BASH_VERSION to whatever and it worked fine.
Here is the complete code from my .profile that loads RVM:
local rvm_home="${HOME}/.rvm"
export PATH="$PATH:${rvm_home}/bin"
if [ -z "$BASH_VERSION" ]; then
export BASH_VERSION=4
fi
source "${rvm_home}/scripts/rvm"