2

I am trying to install Ruby on rails on a Mac Mojave:

$ brew install ruby
Warning: ruby 2.7.1 is already installed and up-to-date
To reinstall 2.7.1, run `brew reinstall ruby`

This shows me that I am running Ruby 2.7.1.

But when I check the version I get:

$ ruby --version
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]

This tells me I am using Ruby 2.3.7.

I don't understand.

To run Rails I need >= 2.4.4 apparently:

$ sudo gem install rails
.....................
ERROR:  Error installing rails:
    zeitwerk requires Ruby version >= 2.4.4.

and

$ which ruby
/usr/local/opt/ruby/bin/ruby

So until I get ruby version to at least 2.4.4 I can't run rails.

Update

Install RVM on MAC as follows

$ brew install gpg
$ curl -L https://get.rvm.io | bash -s stable --autolibs=enabled --ruby
$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin18]

Got these instructions from: https://null-byte.wonderhowto.com/how-to/mac-for-hackers-install-rvm-maintain-ruby-environments-macos-0174401/

But then I install Rails:

$ sudo gem install rails
Successfully installed rails-6.0.2.2
Parsing documentation for rails-6.0.2.2
Done installing documentation for rails after 0 seconds
1 gem installed

and so I tried to crate a Rails project:

$ rails new blah
Rails is not currently installed on this system. To get the latest version, simply type:
$ sudo gem install rails
You can then rerun your "rails" command.

What is going on there? I seem to have successfully installed Rails but cannot create a Rails project.

halfer
  • 19,824
  • 17
  • 99
  • 186
Shane G
  • 3,129
  • 10
  • 43
  • 85
  • Can you tell us the output of `which ruby` ? – Y4glory Apr 04 '20 at 11:08
  • Ok that is added thanks – Shane G Apr 04 '20 at 11:10
  • 1
    What I suspect is the ruby that has been added to your path/bashrc file is the 2.3.7 ruby. And the other installation which is not in path/bashrc file is the up to date 2.7.1 version. – Y4glory Apr 04 '20 at 11:11
  • 1
    I would suggest running `brew uninstall ruby` and then installing it again. Also while its being installed pay attention to where it's being installed. – Y4glory Apr 04 '20 at 11:15
  • 1
    Once that is done I can walk you through adding the new ruby installation to the `~/bashrc` file – Y4glory Apr 04 '20 at 11:16
  • Ok great thanks, I will do that now – Shane G Apr 04 '20 at 11:17
  • $ brew uninstall ruby done and now $brew install ruby again? – Shane G Apr 04 '20 at 11:20
  • 1
    I did some digging around and it turns out apple installs ruby in usr/bin/ruby which they use presumably for internal purposes in the OS. Refer to this answer as well to get a better understanding of whats happening https://stackoverflow.com/a/5214030/13206920 – Y4glory Apr 04 '20 at 11:20
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210929/discussion-between-y4glory-and-shane-g). – Y4glory Apr 04 '20 at 11:22
  • After $ which ruby I get /usr/bin/ruby so the system version of ruby must still be there – Shane G Apr 04 '20 at 11:23
  • yes you can try ruby -v – Y4glory Apr 04 '20 at 11:28
  • As you can see in the updated post i can now install rails successfully but can't create a rails project – Shane G Apr 04 '20 at 13:14
  • Hey has this been resolved? – Y4glory Apr 04 '20 at 15:26
  • basically rails has not been added to path (I know this seems so redundant), so I recommend running `which rails` and adding the following line to your `~/.bashrc` using `export PATH=<"output of which rails>"' >> ~/.bash_profile` – Y4glory Apr 04 '20 at 15:32
  • I notice the end of my ~/.bash_profile file has # MacPorts Installer addition on 2019-02-01_at_16:54:10: adding an appropriate PATH variable for use with MacPorts. export PATH="/opt/local/bin:/opt/local/sbin:$PATH" # Finished adapting your PATH environment variable for use with MacPorts. export PATH="/usr/local/opt/ruby/bin:$PATH" export PATH="/usr/local/opt/ruby/bin:$PATH" export PATH="/usr/local/opt/ruby/bin:$PATH" export PATH="/usr/local/opt/ruby/bin:$PATH" – Shane G Apr 04 '20 at 16:03

1 Answers1

2

The output from this command brew install ruby is not telling you that you're running ruby 2.7.1. That just says you already have ruby 2.7.1 installed for brew. But you could have other ruby versions installed in other ways. When you do

ruby -v

ruby executable is looked for in paths listed inside your environment variable PATH, in order. You can see those paths with

echo $PATH

So, managing different versions of ruby is hard and version managers exist for this reason. I suggest you to install rvm

Ursus
  • 29,643
  • 3
  • 33
  • 50
  • Thanks I just want one system wide version of Ruby, I am not trying to manage different versions – Shane G Apr 04 '20 at 11:24
  • 1
    Two ways then: specify the entire path for your ruby executable 2.7.1 or edit your path variable to have that version path before the system one – Ursus Apr 04 '20 at 11:25
  • 1
    I still suggest the version manager because even if you want just one version your system probably already have one you want it or not – Ursus Apr 04 '20 at 11:27
  • 1
    you're welcome. I'm sorry that's not exactly what you expected – Ursus Apr 04 '20 at 12:53