Switching rubies in shell script - shell

When executing a bash shell script I am using ruby 1.9.3. Then, within the script, I want to switch to JRub (I'm using rvm). I tried switching to JRuby by doing rvm use jruby within the script, but this didn't work, it said:
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.
./run.sh: line 10: jruby: command not found
When I do type rvm | head -n1 at the command prompt, I get: rvm is a function. So I'm not sure of the problem. I thought it might be because I installed JRuby using sudo (sudo rvm install jruby). So I ran the shell script again using sudo. Again I received the error.
How can I switch rubies from within a bash shell script with rvm?
Thanks

I ended up adding this to the line before:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
as explained in this thread (sorry - though I was aware of this thread before, I didn't quite grasp it):
RVM doesn't switch Rubies

/complete/path/to/rvm your shell seems to have an 'rvm' builtin command defined.

Related

Emacs ansi-term as a login shell

I am using Emacs Live. How can I configure emacs ansi-term to allow login shell? When I try to switch rvm gemsets I get the following message:
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.
Thanks!
I found that if I add the following line, Emacs will find RVM as a function and stop complaining.
# Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
I tried this with Linux Mint 20.2, rvm 1.29.12, and GNU Emacs 27.2 and found if I added this to my bashrc:
[[ -s "$HOME/.rvm/environments/default" ]] && source "$HOME/.rvm/environments/default"
Then ansi-term played well with RVM.

"RVM is not a function" error when running in ruby script and irb

I get an error when I run %x{rvm use #myapp} in ruby and irb. The error is "RVM is not a function, selecting rubies with 'rvm use ...' will not work".
Here's what I've tried:
1. the "rvm use #myapp" command works in OSX command prompt (using OSX Mavericks)
2. made sure RVM is the latest version.
3. reloaded RVM check RVM is a function in the command prompt
4. (still fails in irb and ruby's %x{})
5. According to some SO posts, I changed OSX terminal preferences from login shell to /bin/bash and /bin/bash --login. Quit, opened new terminal windows but all efforts were in vain.
6. checked .bash_profile for [[-s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
Any ideas on how I can get %x{rvm use #myapp} to work in ruby and irb?
What happens here is that the shell you had started ruby or irb with had rvm defined both as a function and added to PATH the function takes precedence in shell and it all worked fine, but when you open ruby or irb it is a new process and it inherits only environment variables which includes PATH and does not inherit functions, additionally running %x{} from ruby creates another shell process which is neither a login or interactive shell, and they respectively would make shell load ~/.bash_profile and ~/.bashrc.
Depending on what do you want to do you have few options, to execute another ruby/gem you can use rvm ... do ... for from %x{} like this:
rvm #myapp do ruby -e '...'
OR:
rvm #myapp do gem install ...
OR:
rvm #myapp do bundle install
it allows single command to run in the context of given ruby
Try this trick:
%x{bash -c 'source "$HOME/.rvm/scripts/rvm"; rvm use #myapp'}
However, you really can use rvm as you've specified because even if you've set up the rvm you then lost your session because your terminal will be closed. Try to setup your environment with session gem, and control bash session with it.
require 'session'
#myapp = 'ruby-1.8.7-p374'
bash = Session::Bash.new
stdout, stderr = bash.execute 'source "$HOME/.rvm/scripts/rvm"'
stdout, stderr = bash.execute "rvm use #{#myapp}"
puts stdout
# => Using /home/malo/.rvm/gems/ruby-1.8.7-p374

How to permanently switch ruby -v 1.8.7 to 1.9.3

By using these commands
source ~/.rvm/scripts/rvm
rvm use 1.9.3 --default
The version in current session is 1.9.3 but when I close terminal and reopen ruby version comes back to 1.8.7.
Do I need to add something to the .bash_profile ?
Edit: I found the another way is when I reopen terminal everytime just type source .bash_profile. The version then is 1.9.3. Is there anyway to execute the .bash_profile permanently ?
Yes you need to add something to your bash profile. See here:
https://rvm.io/rvm/basics/
Quote:
The rvm installation documentation instructs you to put the following line at the very end of your bash profile:
# This loads RVM into a shell session
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
Create a file named .rvmrc with the text rvm use 1.9.3 --default.
We're treading into sysadmin waters here but one possible explanation might be because of how you're logging into your shell and your OS. See the discussion of what files are loaded by your shell here and what makes up a login vs. non-login shell here.

Ruby versions differ in Terminal & bash

In Terminal, ruby -v gives me:
ruby 1.8.7 (2011-12-28 patchlevel 357) [universal-darwin11.0]
But if I type /bin/bash then ruby -v I get:
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
I suspect this is something to do with my PATH config(s). My $PATH variable is different in both the above environments. There are other issues e.g. rvm won't run unless I go into bash mode.
For info, my ~/.bashrc contains:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
Looks like "login shell" is not enabled, you need to enable it in Terminal Emulator Preferences, sometimes it is needed to use /bin/bash --login.
There are also known issues with ZSH, but it seams to be unrelated.
Try which ruby from "terminal" and "/bin/bash". Your 1.9.3 is inside your ~/.rvm path.
Type rvm info. You should get a list of the settings for RVM.
In your ~/.bashrc OR ~/.bash_profile, you should have RVM's initialization code. If you don't you didn't install RVM completely and need to finish. Read all the instructions on the RVM installation page.
This was not due to a $PATH problem. What I've learned is that RVM cannot be run unless you change your default login shell to either Bash or ZSH. Just firing up Terminal in Mac won't work. You make the global change to using Bash like this:
chsh -s /bin/bash
(swap /bin/bash for whatever your bash path is, find out using which bash).
The RVM website does say that bash>=3.2.25 is a prerequisite, but doesn't say what that is or how to check whether you have it. It also advises you to run rvm requirements to check what you need - and you can't run this unless you change your shell (all quite confusing for somebody new to this).
Thanks to the replies above for getting me there in the end.
See also: Bad: modifier error when installing RVM

rvm installation not working: "RVM is not a function"

I just installed RVM, but can't make it work. I have such line at the end of my .profile file:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
I tried to run source .profile and restarting terminal, but still, when I run rvm use 1.9.2 I'm getting:
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
My system is Ubuntu 11.10.
You need to run the following
$ source ~/.rvm/scripts/rvm
then run this
$ type rvm | head -n 1
and if you get
rvm is a function
the problem is solved.
You also need to run user$ rvm requirements to see dependency requirements for your operating system
Source: https://rvm.io/rvm/install/
I forget mention that you need to put this code into you ~/.bashrc or ~/.zshrc file and you will not need to write this code again.
You are not using an login shell.
The process of enabling the login flag is described here, also some details on what a login shell is can be found here.
Thus, you need to check the option "Run as login shell" in the Gnome terminal's settings. It is required to open new terminal after this setting the flag.
Sometimes it is required to set the command to /bin/bash --login.
For remote connections it is important to understand the differene between running interactive ssh session and executing single commands.
While running ssh server and then working with the server interactively you are using login shell by default and it's all fine, but for ssh server "command" you are not using login shell and it would be required to run it with ssh server 'bash -lc "command"'.
Any remote invocation can have the same problem as executing single command with ssh.
To permanently resolve this just cut/paste following line:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
From: ~/.bash_profile file
To: ~/.bashrc file
Reason this works is that .bashrc is executed each time you enter the terminal, and .bash_profile each time you login. That is why solution /bin/bash --login works, but you have to do that each time you enter the terminal. This way you are set until your next format, and you will forget all this by than :)
I too faced this problem. Finally i executed this line on terminal.
source ~/.rvm/scripts/rvm
Problem is fixed. Because this line will make the RVM instance a function for a particular time.
The latest RVM (rvm 1.11.6 (stable)) stopped working on Ubuntu (10.10 - 64 bit - nerdy gnat or whatever) - I kept getting
"RVM is not a function, selecting rubies with 'rvm use ...' will not work."
Before, I got the message, but 'rvm 1.9.3-p0#rails321' would work. Now, it wouldn't work - you couldn't change gemsets at all.
Nothing worked, until I found this - make this the LAST line in /home/your-name/.bashrc
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
As you said, the error shown could be the following one.
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.
As said above, just type '/bin/bash --login' in your terminal (after restarting your terminal), then type the comand 'rvm use 1.9.3' (for e.g.) and it will start using the same version.
Just execute the command 'ruby -v' to confirm that the RVM is using the updated version of Ruby.
I had this problem too on a fresh rvm installation, and non of the answers here fixed it. Going into the official rvm site, on the basics section, they have this command:
# from http://rvm.io/rvm/basics
source $(rvm 1.9.3 do rvm env --path)
You should change 1.9.3 for the ruby version that you actually want, and it'll make rvm a function regardless of the shell type.
even though you accepted an answer, i'd like to suggest another way .. ~/.bashrc is loaded before any shell is opened. Add that line at the end of that, and you don't need any of that login shell thing
Maybe you can try belows:
Your Terminal ->
Edit ->
Profile Preferences ->
Title and Command ->
Check the "Run command as a login shell"
Done
Run bash --login and then run rvm use 2.0.0.
Open Up the Terminal and then Go to Edit > Profile Preferences and then go to the Tab "Title and Command" and Check "Run Command as Login Shell".
Boot Up a Bash and Now you can install Gems directly from the terminal without the use of sudo and the error "RVM is not a function, selecting rubies with 'rvm use ...' will not work." will be eliminated.
Cheers.
All the above answers are valid. But when i faced the same issue, the solution was the following:
Update ZSH. (Tried to update directly din't work for some reason. So uninstalled and reinstalled updated version from here)
Set default shell as zsh (i.e. if you prefer zsh) using sudo chsh -s $(which zsh) $USER
Ensure that the following code is at the bottom of your .zshrc after you have installed the latest RVM probably using CURL from official RVM site
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
source ~/.profile
MOST IMPORTANT POINT: Ensure that in your .zshrc file every export to PATH is appended with :$PATH. Which i believe was the root of my problems even after following the above steps.
post this all my problems of RVM Not being a function went away. If it still does not work, give some error trace over here. After a few hours of struggle to solve this issue, i'm sure i must have seen all related errors.
Hope it helps. Cheers!
Procedure for installing Ruby 1.9.3-p125 on Mac OSX 10.8 Mountain Lion
You've already installed the latest XCode (>= 4.3) and and the command line Objective-C
compiler "clang".
You must run the "bash" shell for this procedure to work.
Go to System Preferences
Click on "Users & Groups"
Click the lock on the bottom left of the panel and enter your password to unlock it.
"Ctrl-Click" on your user icon in the left pane of the panel and choose "Advanced Options..."
Change the Login Shell to "/bin/bash"
Close the preferences
Open a terminal window (press command+spacebar and type in "terminal")
Follow the instructions at:
http://www.frederico-araujo.com/2011/07/30/installing-rails-on-os-x-lion-with-homebrew-rvm-and-mysql/
Notes:
To install ruby, you may need to specify the clang compiler:
$ rvm install 1.9.3p125 --with-gcc=clang
If RVM gripes about /usr/local/rvm not found, you need to create a link:
$ ln -s /Users/[your user name]/.rvm /usr/local/rvm
source ~/.bash_profile
... should do the trick ..., probably need to logout and login again.
How to reload .bash_profile from the command line?
I'd got the same error because I'd ever installed the old rvm version ruby-rvm with the apt-get command.
I solved the problem by remove the script line to config the old rvm in .bashrc file.
Check the old rvm config script and then run source .profile
« Official » instructions are there: https://rvm.io/integration/gnome-terminal/
I fixed it by adding this line to .bash_profile:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
I had the same error, but none of the solutions on this page seemed to work. For me it was enough to add the rvm executable to my path:
PATH=$PATH:/usr/local/rvm/bin/
Et voila!
I had the same issue and I did this in my .bash_profile and it worked.
source "$HOME/.rvm/scripts/rvm".
For those who comes with same issue and they are using lubuntu like me I followed this link :
You start your terminal with
lxterminal -e "bash -il"
Thank to #mpapsis who pointed me to the right direction
My unclean way to change of ruby version is
rvm alias create default ruby-2.2.3 && source ~/.bashrc && rvm list
it works because I have the line bellow in my ~/.bashrc but strangely it don't do the job automatically.
[[ -s "/usr/local/rvm/bin/rvm" ]] && source "/usr/local/rvm/bin/rvm"
I tried to connect with --login to my docker container
docker run -it imagename `/bin/bash --login`
but in this case the container stay open in background and I can't enter commands.
I tried both zhc and terminal with the option "open with /bin/bash --login"
$ source ~/.rvm/scripts/rvm
if you don't want to do it again and again for every terminal tab enable the login shell by following these steps.
got to preferences
enter image description here
enable run command as a login shell
enter image description here

Resources