18

is it possible to load rvm in shell script . can any one give an example of it. when i try a shell script . i am using ubuntu system

#!/bin/bash
rvm use 1.9.3

it gives me error

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.
jbr
  • 6,198
  • 3
  • 30
  • 42
user1328342
  • 191
  • 1
  • 1
  • 7
  • possible duplicate of [rvm installation not working: "RVM is not a function"](http://stackoverflow.com/questions/9336596/rvm-installation-not-working-rvm-is-not-a-function) – SirDarius Nov 13 '13 at 12:50

3 Answers3

17

You can just include rvm in your script by doing:

source ~/.rvm/scripts/rvm

then rvm should be available to your script,

jbr
  • 6,198
  • 3
  • 30
  • 42
  • 2
    Make sure you are using bash to execute the script and not sh. – jbr Nov 13 '13 at 12:54
  • how to use bash . i created a file names qst.sh and written a code in it. – user1328342 Nov 13 '13 at 13:03
  • instead of "sh qst.sh" use "bash qst.sh" – Aparichith Oct 17 '14 at 05:08
  • This answer is great and helped me too, so thanks. I just want to clarify that this line should be added *in addition to* your line that calls rvm. At first I thought I was supposed to use this in my command instead of just `rvm`. In my case I wanted to do a `rvm use system`, and so I needed to add the `source ~/.rvm/scripts/rvm` but still call `rvm use system` on a different line. – Keith Bennett Apr 05 '21 at 20:27
5

for simply executing a command against a RVM managed Ruby (or system Ruby) do :

rvm 1.9.3 exec camper_van

this assumes rvm use 1.9.3 and gem install camper_van happened previously which provides the camper_van executable

NOTE: RVM is expected to be loaded already - which it usually is for scripts started under your user (the RVM is not a function, selecting rubies with 'rvm use ...' confirms it's there already).

kares
  • 7,076
  • 1
  • 28
  • 38
1
# Load RVM into a shell session *as a function*
# Loading RVM *as a function* is mandatory
# so that we can use 'rvm use <specific version>'
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
  # First try to load from a user install
  source "$HOME/.rvm/scripts/rvm"
  echo "using user install $HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
  # Then try to load from a root install
  source "/usr/local/rvm/scripts/rvm"
  echo "using root install /usr/local/rvm/scripts/rvm"
else
  echo "ERROR: An RVM installation was not found.\n"
fi
Amol Pujari
  • 2,280
  • 20
  • 42
  • whats the output of command `type -fP rvm`. The given snippet is part of shell script. – Amol Pujari Nov 13 '13 at 12:54
  • 1
    Amol's code requires bash, make sure you are not executing your script with plain sh. – jbr Nov 13 '13 at 12:57
  • when i type in terminal the output of type -fp is /home/gaurav/.rvm/bin/rvm. actually i am new to script. when i write echo type -fp rvm then the same command get printed – user1328342 Nov 13 '13 at 13:08
  • Adding this to by bash script, this snippet echos: "using user install $HOME/.rvm/scripts/rvm". Then, when the next line tries "rvm use..." right back to the old "rvm is not a function" error. How to we do something in our Bash script to "make rvm a function" so we can actually do something with it - like set the ruby version? – JosephK Feb 14 '17 at 04:18
  • Thanks a ton @jbr my issue was fixed after running it through bash. Cheers – Ozone17 Apr 27 '21 at 08:08