[RUBY] Why can I use the rails command installed with gem? ??

Introduction * Under the local environment of windows. *

As the title suggests, I was curious, so I looked it up.

Run in bin / rails or rails? ?? First of all, as a prior knowledge, do you run with bin / rails or rails? ??

In conclusion, it doesn't matter which one you use, but the reference source is different.

Add bin to refer to the project directory. </ span> </ strong>

See local (global) rails without bin. </ span> </ strong>

Then the main subject. Why can I use the rails command installed with the

gem? ?? </ h2> The rails command should pass Path in a local environment variable. .. .. </ del>

→ 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 the rails command pass with rails installed with gem? ??

I looked it up.

The environment variables are:


If you check here, 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 seems to refer to this.

However, I can't confirm it, so I will investigate further.

The various libraries installed by gems are below.


Check the contents of rails 5.2.3.

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

that? ?? How do you refer to it? ??

Try using which command.

$ which rails

Solved ◎

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

By the way, why is it referred to? .. ..

Apparently the which command has the following rules.

which command searches in the order of directories set in the PATH environment variable and displays the first command found If the command is such a reference, so should the command! !!

Point ✌

Remarks: Specify the path when doing bundle install If you execute it without specifying it, it will be installed as follows as described above.
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, bundle install is also executed by default when rails new, but you can skip it by adding the B option.

rails new rails_app -B

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

Project /.bundle/config

BUNDLE_PATH: "vendor/bundle"

Reference Do Rails 4.1 and later console commands always have to add bin /?

How to find the location of the command

Specify the installation path when doing bundle install with rails

Conclusion I wanted to pay more attention to the handling of the local environment on a daily basis.

Recommended Posts