5

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?

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
Gambai
  • 651
  • 1
  • 7
  • 25
  • Sounds like the `chruby` stub isn't getting activated properly when you create a shell. Are you sure it installed successfully? – tadman Feb 23 '16 at 22:18
  • how would I check? when I run chruby, it find the command and runs it... sorry, I'm still new to mac. I installed it with homebrew – Gambai Feb 23 '16 at 22:19
  • I'm more familiar with `rvm` and `rbenv`, but they all work on the same principle. The [installation instructions](https://github.com/postmodern/chruby) includes a step where you have to add a line to `~/.bashrc`. Did you do that? It's easy to miss. – tadman Feb 23 '16 at 22:22
  • I tried it in .bashrc, then I moved it to ~/.bash_profile it's: source /usr/local/opt/chruby/share/chruby/chruby.sh source /usr/local/opt/chruby/share/chruby/auto.sh – Gambai Feb 23 '16 at 22:36
  • do I have to have a .ruby_version file? – Gambai Feb 23 '16 at 22:36
  • A `.ruby-version` file is to set the version for a user or a project. It's not strictly necessary but can be useful. If absent should go to the global default, if there is one. – tadman Feb 23 '16 at 22:40
  • Remember to check with `which ruby` as to the one you're using, and look at `echo $PATH` to see that the `chruby` stuff is in there properly. – tadman Feb 23 '16 at 22:41
  • so I added 2.1.2 to a .ruby-version file in my home directory, seems to be working now thanks everyone! – Gambai Feb 23 '16 at 23:00
  • If you solved it and understand how, add an answer here that explains it in a bit more detail. You're probably not the only one with this issue, and who knows, in the future you might forget and come back here to remind yourself. – tadman Feb 23 '16 at 23:05
  • well, I don't know that I solved it per se. but it's a fix for now I guess. and ok – Gambai Feb 24 '16 at 20:49

4 Answers4

5

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:

  1. Source the chruby.sh script.
  2. 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
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
4

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.

Gambai
  • 651
  • 1
  • 7
  • 25
0

Use chruby to list Ruby versions. Then type chruby [version] (in my case, chruby ruby-2.6.5).

Qasim
  • 1,554
  • 1
  • 13
  • 22
0

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

JosiahF
  • 1
  • 1