Bin folder
In a Rails application, the bin/
folder contains executable scripts that help manage the application. These scripts are generated when we create a new Rails project
and ensure that commands like rails
, rake
,
and bundle
use the correct environment
and gem versions defined in the Gemfile.lock
.
Example:
bin/rails
– Runs Rails commands.
bin/rake
– Runs Rake tasks.
bin/setup
– Prepares the app for development.
Basically, Rails encourages us to use bin/rails
rather than just rails
because that loads the rails executable from within the bin/
subfolder of our app, ensuring we get the correct version. If we run rails
all by itself, that could run any rails
executable anywhere on our hard drive.
Run bin/rails s
instead of rails s
to run the server.
bin/bundle ensures that the correct version of Bundler is used in our Rails application when running commands like bin/rails s
.
Before
Previously, when we created a new Rails app using:
rails new myapp
It generated a bin/bundle
file that activated the correct version of Bundler
and looked something like this:
#!/usr/bin/env ruby
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
# The application 'bundle' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require "rubygems"
m = Module.new do
module_function
def invoked_as_script?
File.expand_path($0) == File.expand_path(__FILE__)
end
.
..continues
If we wanted to prevent binstubs like bin/bundle
from being generated when running rails new, we could use the --skip-bundle
option:
rails new myapp --skip-bundle
It prevented Bundler from installing gems and skipped generating binstubs, including bin/bundle
.
To install gems and generate binstubs manually, we could run:
bundle install
bundle binstubs bundler
After
With the changes in PR#54687, Rails no longer generates bin/bundle
when creating a new application. This change is possible because of improvements in RubyGems, specifically RubyGems PR#8345, which ensures that RubyGems automatically activates the correct version of Bundler.
Now, when we create a new Rails application using:
rails new myapp
We will notice that bin/bundle
is no longer part of the project. RubyGems takes care of Bundler version management automatically, making the bin/bundle
unnecessary.