[Ruby] Why can I use rails command installed by gem? ?

2 minute read

Introduction

*Under windows local environment. *

As the title says, I was interested, so I searched.

bin/rails or rails? ?

First of all, as prior knowledge, whether to execute with bin/rails or with rails? ?

In conclusion, there is no basic problem regardless of which one is used, but the reference source is different.

Refer to the project directory with “bin”.

Refer to local (global) rails unless you add bin.

Then the main subject. Why can I use the rails command installed with <h2>gem? ? </h2> rails command should pass Path as a local environment variable. .. ..

→ This is a very shallow misunderstanding! !!

rails is installed with gem as follows.

$ gem install rails -v "5.2.3"
$ rails -v
Rails 5.2.3

So why does rails command pass with rails installed by gem? ?

I checked.

The environment variables are:

C:\Ruby26-x64\bin

Check here and it looks like this.

$ ls Ruby26-x64/bin
_guard-core* coderay.bat nokogiri* rails.bat ruby_builtin_dlls/ spring*
_guard-core.bat erb* nokogiri.bat rake* rubyw.exe* spring.bat
bundle.cmd erb.cmd pry* rake.bat sass* sprockets*
bundler.cmd gem* pry.bat rake.cmd sass.bat sprockets.bat
byebug* gem.cmd puma* rdoc* sass-convert* sqlite3.def
byebug.bat guard* puma.bat rdoc.cmd sass-convert.bat sqlite3.dll*
chromedriver-helper* guard.bat pumactl* ri* scss* thor*
chromedriver-helper.bat irb* pumactl.bat ri.cmd scss.bat thor.bat
chromedriver-update* irb.cmd rackup* ridk.cmd setrbvars.cmd tilt*
chromedriver-update.bat listen* rackup.bat ridk.ps1 slimrb* tilt.bat
coderay* listen.bat rails* ruby.exe* slimrb.bat x64-msvcrt-ruby260.dll*

There were rails. It looks like I’m referring to this.

But I can’t get confirmation, so I’ll investigate further.

The various libraries installed with gems are below.

C:\Ruby26-x64\lib\ruby\gems\2.6.0\gems

Check the contents of rails 5.2.3

$ ls Ruby26-x64/lib/ruby/gems/2.6.0/gems/rails-5.2.3
README.md

that? ? How are you referencing? ?

Try using the which command.

$ which rails
/c/Ruby26-x64/bin/rails

Solved ◎

After all rails was referenced in the bin directory directly under Ruby.

By the way, why is it referenced? .. ..

Apparently, the which command seems to have the following rules.

which command checks the order of the directories set in the environment variable PATH and displays the first found command

If the which command is such a reference method, so should the command! !!

Match point ✌

Remarks: Specify the path when performing bundle install

If you execute without specifying it, it will be installed in the following as described above.

C:\Ruby26-x64\lib\ruby\gems\2.6.0\gems

The method to specify the path is as follows

bundle install -- path vendor / bundle

Now you can install it in vendor/bundle directly under the project.

By the way, at the time of rails new, bundle install is also executed by default, but you can skip execution by adding the B option.

rails new rails_app -B

Once you install with options, the settings are saved below and you do not have to specify them.

Project/.bundle/config

---
BUNDLE_PATH: "vendor/bundle"

Reference

Does the console command of Rails 4.1 or later have to add bin/?

To find the location of the command

Specify the installation path when bundle installing with rails

In the end

I would like to focus more on the local environment on a daily basis.