Why am I unable to switch my Ruby version using RVM? - ruby

I ran:
rvm install 2.1.0
Followed by:
rvm use 2.1.0
However, my Ruby version given by ruby -v yields 2.0.0 which is my system build.
Why is RVM not behaving as expected?

How are you invoking rvm and ruby? rvm is largely implemented as bash functions, so you need to execute all the commands in the same shell. Invoking rvm in a subshell or another process will discard all the environment variables and shell state that rvm sets up.
(rvm use 2.1.0) #subshell
ruby -v # still 2.0.0
./change_rvm_version.sh # execute shell script that calls rvm
ruby -v # still 2.0.0
rvm use 2.1.0
ruby -v # should be 2.1.0

Make sure you have
export PATH="$PATH:$HOME/.rvm/bin"
at the bottom of your .bashrc (or .bash_profile) file
and invoke it with . .bashrc ( or just do a new shell)

rvm use 2.1.0
Will only change you're ruby version locally, it's used when you're testing multiple versions of ruby. If you want to change you're default ruby version globally using rvm you need to use the default command.
Like this:
rvm --default use 2.1.0

Related

Error installing pg, requires Ruby version >= 2.2 even if ruby is >=2.2

$ ruby --version
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
$ which ruby
/usr/local/rvm/rubies/ruby-2.7.2/bin/ruby
$ which gem
/usr/local/rvm/rubies/ruby-2.7.2/bin/gem
$ sudo gem install pg
ERROR: Error installing pg:
pg requires Ruby version >= 2.2.
Error installing pg, requires Ruby version >= 2.2 even if ruby is >=2.2.
When using RVM, it is necessary for some environment variables to be loaded to RVM (and RVM-installed Rubies) to find its code.
When running your gem install command with sudo however, sudo will reset your $PATH and unset the required environment variables before executing the gem command with the sanitized $PATH. With that, you will likely use your system ruby rather than your RVM-installed ruby.
In general, you should not use sudo to install Rubies or any gems when using RVM. Instead, you generally can and should just use your normal user.
If you do have to use sudo with RVM for some reason, there is a special rvmsudo command which retains your RVM environment in the evaluated context. See https://rvm.io/integration/sudo for details. However, as written above, you should try to avoid using this!

Why isn't chruby saving my default Ruby?

I'm setting up a new machine and trying to install Ruby with chruby. I used ruby-install to install both ruby 2.3 and 2.1.2 because that's what everybody else on my team is running.
When I run chruby I get:
ruby-2.1.2
ruby-2.3.0
Then I run:
chruby ruby-2.1.2
and:
$ chruby
* ruby-2.1.2
ruby-2.3.0
$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin14.0]
The problem is that when I close terminal and open it again the default Ruby version goes back to ruby-2.3.0. How can I set a default version with chruby?
Invoke Chruby at Shell Initialization
Unlike RVM or other Ruby managers, chruby doesn't really have a concept of a "default" Ruby. You need to actually define one during each shell initialization. To do this, you need to:
Source the chruby.sh script.
Select the default Ruby to be exported to your environment.
As an example, you can add the following to your shell's ~/.bashrc or other interactive-shell startup file:
. /usr/local/share/chruby/chruby.sh
chruby ruby-2.3.0
so, the fix I've found so far is to put in a .ruby-version file in my home directory.
currently the .ruby-version file looks like:
2.1.2
... that's it, just the number of the ruby version I wish to use by default.
Use chruby to list Ruby versions. Then type chruby [version] (in my case, chruby ruby-2.6.5).
To set the chruby default ruby version, you need to use the "u" glob qualifier.
Example:
chruby u ruby-3.0.0
#=> * ruby-3.0.0

Installing Ruby 2.2.3 on Ubuntu

This problem probably has an obvious answer to anyone who has used Ruby on linux for a long time, but I can't really get a good answer via Google.
I'm trying to check out the Rails 5 beta, which requires Ruby 2.2.2 or greater. I've installed the RVM (Ruby Version Manager) and set my ruby version to 2.2.3. Running rvm list in the terminal results in the following output:
rvm rubies
=* ruby-2.2.3 [ x86_64 ]
# => - current
# =* - current && default
# * - default
Okay, great. I have Ruby 2.2.3 installed. Let's try bundle update:
Installing rack 2.0.0.alpha (was 1.6.4)
Gem::InstallError: rack requires Ruby version >= 2.2.2.
Okay, that's weird. What version of ruby do I have? Running ruby -v gives me this:
ruby 2.1.5p273 (2014-11-13) [x86_64-linux-gnu]
So... do I have 2.2.3 or not? What gives?
Since you are using bundle, I would suggest you to check your Gemfile for ruby version. You can also test it with bundle exec ruby -v
Also try:
rvm use ruby-2.2.3
bundle exec ruby -v
Maybe by setting the default Ruby version on rvm to the one you have (ruby-2.2.3) solves the problem, by doing rvm --default use ruby-2.2.3. Then reload the bash and execute ruby -v to check if the default version of Ruby was changed properly and then try again to execute the bundle install command.
If you don't know zsh and oh-my-zsh, I highly recommend for this kind of things. You could configure the terminal in order to show you the current version of Ruby and Rails that you're using every moment. Check it here: http://ohmyz.sh/
In addition to doing rvm use --default 2.2.3 or rvm use 2.2.3 make sure that you don't have the ruby package from apt (or whatever package manager) still installed. For some reason, that ruby package could take precedence over an RVM Ruby.
Also, try doing rvm uninstall 2.1.5 to force your system to default to 2.2.3... You can always reinstall 2.1.5 later and, if this issue persists, 2.2.3 should stick as your default.

Can't find Ruby 1.9.2 after installing RVM

I'm on Linux Ubuntu 12.04, and I first installed Ruby 1.9.2.
After I tried installing rvm to use Octopress, ruby --version in bash results in ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
How can I use RVM with ruby 1.9.2?
I don't really understand what RVM is all about...
The 1.9.2 version you installed first is not accessible from rvm.
While working with rvm, only the list of rubies installed through rvm is served. The previously installed versions of ruby - while still present in the filesystem - are not included.
With the steps you have done so far, you have a 1.9.2 version installed in one location, and a 1.8.7 version installed as part of rvm.
Install 1.9,2 version by running the rvm install 1.9.2 command. After that, when you run the rvm list command, you should see an output similar to the following:
$ rvm list
rvm rubies
ruby-1.8.7-p358 [ i686 ]
=* ruby-1.9.2-p320 [ x86_64 ]
# => - current
# =* - current && default
# * - default
You can switch between different versions of ruby managed by rvm with the rvm use 1.8.7 / rvm use 1.9.2 commands.
Here's a short blogpost with a good intro to rvm: Get started right with RVM
RVM is a version manager which enables you to decide which of multiple installed Ruby versions you want to use in your current shell session. RVM does this by providing a shell function named rvm which can be used to switch between versions in the current session. This changes environment variables, especially the GEM_HOME and PATH, to match the currently selected Ruby installation.
Every installed Ruby version can be selected by a specific identifier string. If you had a system-wide Ruby installation already before you installed RVM, that one should be referenced by the string system. The newly installed version in your case should be called 1.9.2.
To make RVM work as intended, it is necessary to load the rvm shell function into your shell. How to do this is described in the RVM Installation Documentation in section 2 - "Load RVM into your shell sessions as a function".
You can see if the shell function is correctly loaded when the command type rvm | head -n1 responds with:
rvm is a shell function
If not correctly loaded it will tell you something like this:
rvm is /home/someone/.rvm/bin/rvm
If you finally have it working you can switch your active Ruby version with commands like rvm 1.9.2 or rvm system. You can get a list of all Ruby versions and their identifier strings recognized by RVM by the command rvm list.
You can also specify which Ruby version shall be enabled in all new shell sessions from the beginning by issuing the following command once:
rvm --default 1.9.2
Perhaps you have not yet switched to the ruby version with:
rvm use 1.9.2 (assuming you did rvm install 1.9.2)
for a more general explanation I gave an answer here which might help.

How do I update my Ruby version if I have multiple versions installed?

I managed to use rvm (Ruby Version Manageer) to update to the latest Ruby version (ruby-1.9.2-p180). To do this I ran the following commands:
rvm install ruby-1.9.2-p180
rvm ruby-1.9.2-p180
ruby -v
The output of that last command indicates that it is successfully installed:
ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.0]
Running a "which ruby" command indicates that I'm using the correct Ruby version as well:
/Users/Bijan/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
So in the current Terminal session, the new Ruby is installed properly. However, whenever I open a new Terminal session, it defaults me back to the original version I was running:
ruby -v
ruby 1.9.2p136 (2010-12-25 revision 30365) [x86_64-darwin10.6.0]
which ruby
/usr/local/bin/ruby
So, in other words, I seem to have to different versions properly installed, but the default is going to the /usr/local/bin instead of the RVM installation. How do I go about making sure that the default Ruby version that I am using is the most recent?
You can set your default ruby version to be the latest with the following:
rvm --default use <ruby version here>
By default, the system ruby is your default in RVM
create a .rvmrc file under the directory (~/ in your case) which includes the following:
rvm use ruby-1.9.2-p180
save it and you are good to go.

Resources