"RVM is not a function" error - ruby

RVM is installed on my machine (running Mac OSX 10.6.8), correctly and it runs fine. The odd thing is that to run it, I have to use source ~/.rvm/scripts/rvm for every new session. I tried making a symlink from it to /opt/local/bin/rvm, but when it runs it does nothing. I also tried creating a symlink from ~/.rvm/bin/rvm to /opt/local/bin/rvm, and when I run rvm in the Terminal it displays the help page, as expected. But when I try rvm use some_ruby_version it always displays "RVM is not a function, selecting rubies with 'rvm use ...' will not work.". How can I fix this?
My goal is to get it to the the point that I don't have to type the source command every session, and for some reason ~/.profile does not execute.

You have to source the RVM script into the current session because it makes changes to the shell environment - and it is absolutely impossible for that to be done from a child process. Your efforts at running RVM as an external command cannot succeed.
To actually fix this you have two choices:
Configure your terminal emulator to start a login shell, rather than a non-login shell, so that your .profile is loaded.
Modify .bashrc to source RVM instead, which works for non-login shells as well.
To do the second you can just add to ~/.bashrc:
if test -f ~/.rvm/scripts/rvm; then
[ "$(type -t rvm)" = "function" ] || source ~/.rvm/scripts/rvm
fi

If you are using zsh as shell instead bash, you have to:
1.
vi ~/.zshrc
2.
Like Matt said, add:
if test -f ~/.rvm/scripts/rvm; then
[ "$(type -t rvm)" = "function" ] || source ~/.rvm/scripts/rvm
fi
3. Restart Terminall
4. Done!
rvm use 1.9.3
Wil work

I didn't understand what ~/.profile does correctly; I needed to change ~/.bash_profile instead. Problem solved!

Well, with mountain lion (10.8.3) what worked for me was editing /etc/profile
and adding the line mentioned before at the bottom of the file:
if test -f ~/.rvm/scripts/rvm; then
[ "$(type -t rvm)" = "function" ] || source ~/.rvm/scripts/rvm
fi

I had the same issue. I found the .profile file was not getting updated, so i added the same command that was added into .bash_profile:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
I don't know if this is the right way, but it worked...

You shouldn't need to edit anything as others suggest. Just go into your terminal's settings and select the "Run command as login shell". This will cause .profile to run on the next terminal instance. Reopen your terminal and you should be able to use rvm use 1.9.3 (or whatever version you installed).
More info found on rvm.io (which is also a great place for answers)
https://rvm.io/integration/gnome-terminal

You have to make some settings.
Open terminal and run this command.
source ~/.rvm/scripts/rvm
and then go to edit > Title and command and check Run command as login shell
and you are done. Now you don't need to specify source everytime.

What was screwing me up was assuming my path was correct since I was using one I can run manually.
Apparently there are different executables or scripts that can be used and are located in different places.
I thought that the path Mina should use was this:
/usr/local/rvm/bin/rvm
When in reality it was this:
/usr/local/rvm/scripts/rvm

I had this issue when I became root. I tried many of the solutions above. What finally worked was exiting from root and being a regular user. Which is what I needed anyway.

None of these solutions seemed to redeem my problem which was on Ubuntu 12.04 LTS.
What I did is the following:
rvm get stable --auto-dotfiles as outlined in the RVM documentation here
Added source ~/.profile as the first line of: ~/.bash_profile
I will not all of these steps were documented as errors from the RVM command line:
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.
and
WARNING: You have '~/.profile' file, you might want to load it,
to do that add the following line to '/home/user_name/.bash_profile':
source ~/.profile

Related

RVM is not a function, selecting rubies with 'rvm use ...' will not work

List the ruby versions
console:~$ rvm list
rvm rubies
ruby-2.0.0-p481 [ i686 ]
# => - current
# =* - current && default
# * - default
Try to use a specific version of ruby
console:~$ rvm use 2.0.0
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.
Your console is not running as a login shell and hence have no access to rvm function. If you are running Ubuntu, you can:
Open console
Select Edit -> Profile Preferences
Select tab: Title and Command
Check box 'Run command as a login shell'
Restart terminal
Type bash --login from your terminal. And then give rvm use 2.0.0
It seems like your rvm does not load ".bash_profile" properly. I have done to fix it in MAC OS X or Ubuntu 14.04 by opening terminal and write:
source ~/.rvm/scripts/rvm
My env is OSX Yosemite. Had the same issue .... solved by adding the following
1) edit and add the following line to .bash_profile file.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
2) Restart terminal and try RVM command again
If you don't want that every time you open a terminal, do the suggestion above again, just add
source ~/.rvm/scripts/rvm
at the end of ~/.bashrc
The error is due to rvm is not running as in login shell. Hence try the below command:
/bin/bash --login
You will able run rvm commands instantly as login shell in terminal.
Same principle as other answers, just thought it was quicker than re-opening terminals :)
bash -l -c "rvm use 2.0.0"
You need to add source ~/.rvm/scripts/rvm to ~/.your_shellrc file. From now when you load a shell, rvm will be loaded.
For example:
if you are using zsh shell, need to add in ~/.zshrc file
if you are using bash shell, need to add in ~/.bashrc file
And then open a new tab to see the effect.
For me in Ubuntu(18.08), I have added below line in .bashrc and it works.
source /home/username/.rvm/scripts/rvm
Please add this line.
I think this more generic solution
echo 'source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
Following work for me in ubuntu 19.1
source ~/.rvm/scripts/rvm
From a new Ubuntu 16.04 Installation
1) Terminal => Edit => Profile Preferences
2) Command Tab => Check Run command as a login shell
3) Close, and reopen terminal
rvm --default use 2.2.4
If RVM was installed with dedicated ubuntu RVM installer https://github.com/rvm/ubuntu_rvm the path to RVM scripts will be different /usr/share/rvm/scripts/rvm. So to add it to your .bashrc run the following command:
echo 'source "/usr/share/rvm/scripts/rvm"' >> ~/.bashrc
FWIW- I just ran across this as well, it was in the context of a cancelled selenium run. Perhaps there was a sub-shell being instantiated and left in place. Closing that terminal window and opening a new one was all I needed to do. (macOS Sierra)
Usually this is caused by shell initialization files. Search for PATH=... entries.
You can also re-add RVM to your profile by running: rvm get stable --auto-dotfiles
To fix it temporarily in this shell session run: rvm use ruby-2.6.5
To ignore this error add "rvm_silence_path_mismatch_check_flag=1" to your "~/.rvmrc" file.

the correct Ruby version when opening a terminal

Each time I open a terminal my Ruby version is wrong. I am obliged to execute by hand the following command:
source /usr/local/rvm/scripts/rvm
I read that RVM doesn't modify .bashrc or .bash_profile but I note that my .bashrc has at the end:
PATH=$PATH:$HOME/.rvm/bin
Not only I didn't insert this myself but the path to rvm is wrong !
I changed by:
PATH=$PATH:/usr/local/rvm/bin
I reopened a terminal but the Ruby version is always wrong !
rvm does modify your .bash_profile / .bashrc -- that's one of its weaknesses. In particular, it adds (is supposed to add) this line to load rvm as a shell function:
# Load RVM into a shell session as a function
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
I had a similar issue with the wrong ruby version running even after I changed the global default in rvm--ensuring this line was at the end of my .bash_profile fixed it. In any case, that line's supposed to be there for rvm to work properly.
You can make an .rvmrc file in the root directory of your application.
It runs whatever is inside on opening the root direct, so I think you can just copy this
PATH=$PATH:/usr/local/rvm/bin
and things should be good. You'll get a warning, but it's safe to ignore (or has been in my experience). I think you might be better off going with this, which I've found helpful to keep rubies/gemsets straight across applications.
#.rvmrc
rvm [ruby]#[gemset] #for example rvm 2.0.0-p247#my_gemset
Why gemsets/rubies get switched/reset in the first place, I really don't know and it is annoying, but this seems to fix it.
read more here
Depending on your setup it could not be loading for several reasons.
For instance, when you open a shell (depending on the OS distribution) bash files are read in various orders, although here is what the standard looks like: ( I believe Ubuntu is slightly different so you might want to check if thats your distro).
# for interactive login shells (when you login to a terminal)
/etc/profile
/home/<user>/.bash_profile
/home/<user>/.bash_login # if there is no .bash_profile
/home/<user>/.profile # if there is no .bash_login
# for interactive non-login shells (i.e. opening up a new tab)
/etc/bash.bashrc
/home/<user>/.bashrc
What happens when you directly source your .bashrc? Does it then find rvm? If so, then its just a matter of making sure your .bash_profile sources .bashrc aka:
# in .bash_profile
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
If this isn't the cause you could dive into the rvm documentation or switch to rbenv which IMHO is a much simpler solution.
RVM should handle this automatically but this code is wrong.
PATH=$PATH:/usr/local/rvm/bin
RVM's path should be first. Your bash is acting as you wish actually. It uses the 'system wide' ruby first because you tell your shell to do so. Change this to:
PATH=/usr/local/rvm/bin:$PATH
then source ~/.bashrc and it will work as you wish, calling rvm ruby when you login.

RVM not being found (Mac 10.7.3)

I've done a lot of searching and there seem to be similar issues, but none that have helped me solve this problem. I'm fairly new to Ruby, Rails, and -ix systems so that doesn't help much, either. :(
I was able to get RVM up and running along with the latest versions (1.9.3-p125 of Ruby and 3.2 of Rails). However, when I exit terminal and come back in the system defaults are being used. When I tried to change versions I received a "command not found". I then tried just typing "rvm" and got:
-bash: rvm: command not found
I followed some other tutorial advice and modified my ~/.bashrc file to look like this:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
but I'm still getting the exact same "command not found" even after exiting Terminal.
Any thoughts on how to get RVM working properly? Everyone says use it, but it sure isn't simple...
Thanks a lot!
Greg
I followed some other tutorial advice and modified my ~/.bashrc file
Usually I modify .profile file on Mac OS X, and .bashrc on Linux. Try to modify .profile
Please check How to fix Terminal not loading ~/.bashrc on OS X Lion to know why Terminal doesn't load .bashrc file.
If you modify your .bashrc you either need to immediately source it or create a new shell to see the changes take effect.
The standard rvm procedure is to source the rvm script that is created. The installation documentation covers most use cases.
If your rvm command is missing, it's possible that your .bashrc is not working as you expect. Is your PATH being modified as you've requested? If not you might have another issue.

RVM and Jenkins setup

I am new to Jenkins CI. I'm install RVM in my remote Jenkins and when I execute below shell.
#!/bin/bash -x
source ~/.bashrc
rvm use 1.9.3#rails-3.2.3
I get following errors.
+ source /var/lib/jenkins/.bashrc
++ PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/var/lib/jenkins/.rvm/bin:/var/lib/jenkins/.rvm/bin
+ rvm use 1.9.3#rails-3.2.3
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal settings to allow shell login.
Please visit https://rvm.io/workflow/screen/ for example.
What does it mean? I don't have any idea. Please help me.
UPDATED: I'm tried below script but I still get errors:
#!/bin/bash -x
source /home/zeck/.bashrc
[[ -s ".rvmrc" ]] && source .rvmrc
export RAILS_ENV=test
bundle install
Errors:
/tmp/hudson457106939700368111.sh: line 5: bundle: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Jenkins build shell can't detect RVM, gemsets and gems. What should I do?
UPDATED 2: Therefore jenkins can't detect ruby.
+ ruby -v
/tmp/hudson2505951775163045158.sh: line 5: ruby: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILUR
I'm not using any jenkins plugn and I'm just run script from Build->Execute shell section.
As the error message suggests, RVM expects an login shell. Changing the hashbang line to #!/bin/bash -xl should resolve this.
try:
. $(/home/RVM_USER/.rvm/bin/rvm env 1.9.3#rails-3.2.3 --path)
make sure you run the stable RVM:
rvm get stable
NOTE:
Last Jenkins version does not always accept "source", but ".".
RVM_USER is the user that installed RVM.
Alternatively you can also export the RVM command in the main PATH.
Yes, apparently you miss the $HOME/.rvm/bin in your PATH. I am using rvm successfully from Hudson on Mac OS X. First thing to notice is that, unless you define BASH_ENV environment variable (ENV for sh), .bashrc is called automatically only with interactive non-login shell. Such a shell is started when you do - for example - the following from the command line:
$ /bin/bash
When you use #!/bin/bash in your script, .bashrc will not be called.
To make rvm work with Hudson, I have the following in my .bash_profile:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
export PATH
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
Thanks to [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" I have rvm enabled every time I start new terminal window (interactive, login shell).
I do not put anything in my .bashrc, especially I am not sourcing rvm scripts there. Nothing wrong with that, but if any other scripts makes something stupid like setting `export BASH_ENV=$HOME/.bashrc' and then invoke non-interactive shell, you see what may happen - it is actually easy to forget.
Therefore, instead of loading things to your .bashrc, it is better to keep your script independent from any shell startup file and make sure that the correct environment is set up within the script. I still keep $HOME/.rvm/bin in my .bash_profile, but then I include the following at the beginning of my script:
#!/bin/bash
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
rvm use 1.9.3-head#MyGemSet
set -ex
cucumber # just an example
Notice the -e option which forces the script to exit with error code if any command following set -ex fails. This is behavior you may want when using the script with Hudson.
It is incorrect to say that RVM expects a login shell. Although using #!/bin/bash -l in your script will work, it does not seem like the best approach.
Just add this code in your shell script, i think rvm is loading from your source so it should work else need to export PATH variable
#!/bin/bash -l
source ~/.bashrc
rvm use 1.9.3#rails-3.2.3
l is for login shel, if you include x then it would be for debugging too.
adding a shebang to the build commands in jenkins fixed this for me
#!/usr/bin/env bash
rvm use 2.0.0
bundle install
rake test
...
Jenkins nodes don't load paths the same way, so it's not using the proper path to find rvm's version of ruby. You can set the path for a given agent.
Find your current PATH by doing echo $PATH
Assuming you've set up rvm properly, find where rvm's version of ruby is located by running which ruby
There's a setting in the Configuration of your agent where you can set environmental variables. Set PATH to be 1 and 2 concatenated.

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