I'm new to Ruby and Rails, and my rails installation cannot run in the CLI because it is looking for Ruby in the wrong place.
TLDR question - How can I point rails to my ruby installation?
Here is the command I ran and it's output. It is looking for ruby in the wrong directory.
$ rails -v
/c/Users/<username>/AppData/Local/Microsoft/WindowsApps/rails: line 6: /c/Users/<username>/AppData/Local/Microsoft/WindowsApps/ruby: No such file or directory
I'm using an installation of ruby from the rubyinstaller-devkit-3.2.2-1-x64
. Once that was done I installed rails using the command gem install rails
. I followed the installation instructions from the Rails community, right up until it came time to check the version of my installation.
I've done some searching and learned that the .bat
files can define where rails looks for ruby or other packages, and that they can be run independently.
I can run rails.bat
like so and get the following result:
$ ~/AppData/Local/Microsoft/WindowsApps/rails.bat -v
Rails 7.0.7.2
So I opened rails.bat
and it contains this
@ECHO OFF
@ruby.exe "%~dpn0" %*
I read up on batch files and their syntax, and I edited rails.bat
so that I could see what command was being run. Here is the output from that experiment:
$ ~/AppData/Local/Microsoft/WindowsApps/rails.bat -v
C:\Users\<username>\workspace\ruby-work>ruby.exe "C:\Users\<username>\AppData\Local\Microsoft\WindowsApps\rails" -v
Rails 7.0.7.2
While searching I saw a great deal of consternation from Rails users when some installer had an incorrect path hardcoded, but I don't see that hardcoding here. Further, they seemed to be solving their problem by correcting the relevant .bat
files (this was for an older version of Rails and Ruby). But when I edit my .bat
files, and re-run rails -v
I get the same output - it always looks in the same directory, no matter what path I define in the .bat
files.
How can I point Rails to the directory containing my ruby installation?
Finally here is some version and location info which I hope is helpful. I am running on Windows 10.
$ ruby -v
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x64-mingw-ucrt]
$ gem -v
3.4.10
$ which ruby
/c/Ruby32-x64/bin/ruby
$ which rails
/c/Users/<username>/AppData/Local/Microsoft/WindowsApps/rails
$ ~/AppData/Local/Microsoft/WindowsApps/rails.bat -v
Rails 7.0.7.2
Edit: Noting here that the commands above were issued in a bash shell. Reviewing my PATH, the only ruby-related inclusion I see is this.
C:\Ruby32-x64\bin;
I don't have any further environment variables related to ruby development defined as of yet.
Following the suggestion by @Casper I opened the /c/Users/<username>/AppData/Local/Microsoft/WindowsApps/rails
file to see if editing it would be any help. Inside I found the following snippet that appears to target the ruby executable:
=begin
bindir="${0%/*}"
exec "$bindir/ruby" "-x" "$0" "$@"
=end
By changing the value of bindir
like so, I can run rails commands through the command line successfully:
bindir="C:/Ruby32-x64/bin"
The downside here is that I'll need to manually update this path any time I need to update or otherwise change the version of Ruby. This feels like more of a workaround, but it got me up and running.