0

Error when doing rails new blah, after a project where I put rails back to an early version

rails was working fine, I could do rails new blah

I wanted to test changing rails versions for a project. My experience up until this point has been that changing the rails version of a project won't change rails globally! So my experience has been that when CDing out of a directory and doing rails -v I'm still on 5.2.2.1

So I made a project with the aim to push back to rails 0.8.0 (just as a test!)

~/rubymac/test16$ cat Gemfile | grep "^gem 'rails'"
gem 'rails', '0.8.0'
~/rubymac/test16$ 

and I did bundle update rails

and no error, and I saw in Gemfile.lock that rails 0.8.0 seems to be specified there so I guess that worked that far.

Then I did cd .. and did rails -v expecting to (still) see rails 5.2.2.1 which has been my experience in the past when I put a project back to a slightly earlier version of rails. But rails -v didn't work. And rails new blah123 didn't work either.

~/rubymac$ rails -v
(in /usr/local/lib/ruby/gems/2.5.0/gems/rails-0.8.0)
rake aborted!
LoadError: cannot load such file -- rake/rdoctask
/usr/local/lib/ruby/gems/2.5.0/gems/rails-0.8.0/Rakefile:3:in `<top (required)>'
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)
~/rubymac$ 

and

~/rubymac$ rails new blah2432
(in /usr/local/lib/ruby/gems/2.5.0/gems/rails-0.8.0)
rake aborted!
LoadError: cannot load such file -- rake/rdoctask
/usr/local/lib/ruby/gems/2.5.0/gems/rails-0.8.0/Rakefile:3:in `<top (required)>'
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)
~/rubymac$ 

So I searched and found this question new rails project error Rails 3. which suggested gem install rails -v3.0.9

So I did gem install rails -v5.2.2.1

and it gives no error

~/rubymac$ gem install rails -v5.2.2.1
Successfully installed rails-5.2.2.1
Parsing documentation for rails-5.2.2.1
Done installing documentation for rails after 0 seconds
1 gem installed
~/rubymac$ 

But rails -v still doesn't work

~/rubymac$ rails -v
(in /usr/local/lib/ruby/gems/2.5.0/gems/rails-0.8.0)
rake aborted!
LoadError: cannot load such file -- rake/rdoctask
/usr/local/lib/ruby/gems/2.5.0/gems/rails-0.8.0/Rakefile:3:in `<top (required)>'
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.2/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)
~/rubymac$ 

I do gem list and see what rails versions I have

~/rubymac$ gem list | grep "^rails ("
rails (5.2.2.1, 5.2.2, 5.2.1.1, 5.2.1, 5.2.0, 5.1.6, 5.1.4, 5.0.7.2, 5.0.7.1, 5.0.1, 5.0.0, 0.8.0)
~/rubymac$ 

I tried uninstalling rails 0.8.0. it also prompted me to remove the executable and I said yes

~/rubymac$ gem uninstall rails -v0.8.0
Remove executables:
    rails

in addition to the gem? [Yn]  y               
Removing rails
Successfully uninstalled rails-0.8.0
~/rubymac$ gem list | grep "^rails ("
rails (5.2.2.1, 5.2.2, 5.2.1.1, 5.2.1, 5.2.0, 5.1.6, 5.1.4, 5.0.7.2, 5.0.7.1, 5.0.1, 5.0.0)
~/rubymac$ rails -v
-bash: /usr/local/bin/rails: No such file or directory
~/rubymac$ gem install rails -v5.2.2.1
Successfully installed rails-5.2.2.1
Parsing documentation for rails-5.2.2.1
Done installing documentation for rails after 0 seconds
1 gem installed

but maybe I went a step too far in saying yes to removing the executable earlier having been prompted because now I get '..rails: no such file or directory'

~/rubymac$ rails -v
-bash: /usr/local/bin/rails: No such file or directory
~/rubymac$ 

even after trying to install 5.2.2.1

~/rubymac$ gem install rails -v5.2.2.1
Successfully installed rails-5.2.2.1
Parsing documentation for rails-5.2.2.1
Done installing documentation for rails after 0 seconds
1 gem installed
~/rubymac$ rails -v
-bash: /usr/local/bin/rails: No such file or directory
~/rubymac$ 

Added

I then tried gem uninstall rails, I uninstalled all versions. I then did sudo gem install rails. I started a new terminal window. rails -v

when I do which rails it says /usr/bin/rails which is an improvement.

But when I do rails -v it says

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.
~$ 


~$ gem list | grep rails
coffee-rails (4.2.2, 4.2.1)
jquery-rails (4.3.1, 4.1.1)
rails (5.2.2.1)
rails-controller-testing (0.1.1)
rails-dom-testing (2.0.3, 2.0.2)
rails-html-sanitizer (1.0.4, 1.0.3)
sass-rails (5.0.7, 5.0.6)
sprockets-rails (3.2.1, 3.2.0)

actually grep rail* is better 'cos includes railties as mentioned by Uninstall Rails completely

Also, I am not using any version control manager, no rvm or rbenv

barlop
  • 10,225
  • 7
  • 63
  • 94

2 Answers2

0

First, try closing out of your terminal and restarting. Sometimes the PATH doesn't get updated with the rails executable after installation.

If that fails...

In your terminal run:

$ gem env

This should tell your where gems have been installed. Rails is, after all, just a gem. I use .rbenv as a Rails environment manager. This means my EXECUTABLE DIRECTORY is at: /Users/me/.rbenv/versions/2.5.1/bin Inside that folder I can do:

$ ls | grep rails

and I get:

drwxr-xr-x   3 me  group   96 Mar 14 10:59 rails

Yay, Rails is installed and it does exist there. If you can find this, that means you have Rails installed, but your PATH cannot find it. PATH is an environment variable that usually contains a list of known directories that contain executable files. When I type rails -v, PATH gets checked. Lets make sure my PATH is working:

/Users/me/.rbenv/shims

Good. Don't worry about shims, all you need to know is that folder contains my rails executables. Yours should be the path to the EXECUTABLE DIRECTORY. Now add your executable dir to your PATH:

export PATH=PATH:/full/path/to/EXECUTABLE_DIRECTORY

That should be it. Try to run rails again. If that fails, try to manually run your rails executable. If that fails, your executable is broken or you're not looking at the right file (much more likely).

If you can't find the executable rails, then rails DNE somehow and you need to try to reinstall.

Veridian Dynamics
  • 1,269
  • 1
  • 7
  • 15
  • 1
    Okay... I'm not sure I can help you without more information, but your issue seems like it almost perfectly matches this one except you're not using RVM: https://stackoverflow.com/questions/7788946/rails-keeps-telling-me-that-its-not-currently-installed. If that doesn't solve it, just look into forcefully uninstalling everything and starting fresh with RVM or RBENV – Veridian Dynamics Mar 21 '19 at 18:28
  • I saw that one but almost all solutions there involve rbenv and I'm not using a version manager. The one that doesn't involve rbenv says to start a new terminal but that didn't help. Also tried shutting down and starting up again, didn't help – barlop Mar 21 '19 at 19:08
  • I get this error when trying to remove rails forcefully `sudo rm -rf /usr/bin/rails rm: /usr/bin/rails: Operation not permitted`. and even gem uninstall rails doesn't get rid of it! – barlop Mar 21 '19 at 19:09
  • maybe I will try removing all rails things listed by gem list | grep rails, similar to what this guy did https://stackoverflow.com/questions/16164697/uninstall-rails-completely – barlop Mar 21 '19 at 19:16
  • I think that worked, removing all gems with 'rail' in them. particularly railties, then installing rails and starting a new terminal worked. I'll post an answer later. – barlop Mar 21 '19 at 19:26
0

Okay, the solution was to uninstall rails completely, and then reinstall rails. But the problem I was running into was that it wasn't uninstalling rails completely.

In order to uninstall rails completely, it's mentioned here Uninstall Rails completely

You have to uninstall not just rails, but a gem called railties!

So, when you list rails gems, don't do | grep rails, do | grep rail (singular!). i.e. Do gem list | grep rail, then you get a list of rail related gems including railties.

I deleted/uninstalled all of those gems. (though possibly I only need to uninstall railties and rails).

So then I was in a position where the rails file(in osx that's /usr/bin/rails), was gone And, those gems were gone, which is fine.

(what I didn't want is either a)a position where the rails file is there and rails gives an error or b)the rails file is not there) So that was then fine in that it was uninstalled completely.

Then I did gem install rails, and it installed rails and related gems. And it was fine.

barlop
  • 10,225
  • 7
  • 63
  • 94