[RUBY] The process of understanding Gemfile by non-engineers

What is a Gemfile?

As you pointed out, the contents of Bundler have been revised.

When creating the portfolio, I was building various environments.

What is a Gemfile?

I did it without knowing it in the tutorial, but I would like to summarize it here.

What is Gemfile? \ _ Hehe (∀ ` *) Katata

"Gemfile" is a file that manages the list of gems used in Rails application. Quote


gem is a ruby ​​library. Quote

···Library? ??

A file that collects a lot of useful program parts and puts them together. Quote

・ ・ ・ ・ Words that I don't understand appear in the Imozuru formula.

First to understand the outline

Related keywords: Gemfile, Gemfile.lock, bundler, library, gem

Let's summarize the above keywords into a story.

I have a man. A family of four with a wife and two children. His wife is tired of being busy with housework every day. Dad and children plan to "help mom and make it easier."

The purpose was to "make mom easier" (application)

There are various ways to do this, you can think for yourself, or you can get various books (gems) from bookstores (sources in Gemfile) and get inspiration.

Dad makes an action plan on how to achieve his goals. (Gemfile)


[https://rubygems.org]"How to help washing ver2.0", "How to make delicious food ver3.5" from the bookstore
Get "Assertive Relationships with Your Wife ver4.0" and "5 Ways to Get Your Mother's Mood Ver1.1"

Based on the blueprint, my dad gets a book at a bookstore (bundle install), Here the librarian checks the list (Gemfile.lock) of the books and versions that Dad already has. After that, he looks at the "Dad's Action Plan" and gives you a book that you don't have and a book that has a dependency on it. Finally, it updates the list of books owned by Dad (Gemfile.lock).

Three months later, the children will go get the book with their father's action plan and list.

Dad and children can accomplish the goal of "easing mom" with exactly the same plan.

Keyword details

Ruby library "gem"

A library is a collection of common processes packed into program parts.

This is an example book. "Make delicious food" "How to help with laundry" These are libraries. In other words, it is called "gem".

If the wisdom of these ancestors is not in the form of a book, you will have to create a "method for making delicious food" yourself, and you will have to devise a "washing method" yourself. But if it's a library, you can use these technologies just by bringing it as it is.

For a real Rails application "Connect to the database" "Test" "Adjust the appearance" "Implementation of login function" "Add pagination function"

Such "gem" is prepared.


There are dependencies in gems. For example, the book "Making delicious food" is supposed to refer to the book "How to use kitchen knives 2.0". (Depends on)

In this way, there are cases where gems use other gems.

RubyGems bundle installYou can get the gem even if you don't. gem's package manager "RubyGems" The way to get a book is to go to a bookstore (`https://rubygems.org```), but when you try to get "make delicious food", gem install make delicious food" The `` command alone will give you" How to use a kitchen knife 2.0 ".

Then what is the significance of Bundler? It becomes a story.

Without Bundler, applications suddenly stop working, or in another person's environment .

why? The gem will be updated to make it better. When it is updated, it will not be compatible with the gems that have been dependent on it until now, and the above problems will appear.

Suppose your dad gets "how to cook delicious food" and its dependency "how to use a kitchen knife 2.0" with the gem command. Three months later, when the children also got it with the gem command, "How to use the kitchen knife" was updated. I got "How to make delicious food" and "How to use a kitchen knife 2.5", and the dependency is broken.

Bundler With the advent of Bundler, everyone who uses the application can run the app with the same gem version, including the dependent gems.

In the previous example, even if the author of "How to make delicious food" says "How to use a kitchen knife 2.0 or later", the father can control "How to use a kitchen knife 2.0!" Bundler ". This allows children (other developers) to prepare "How to use kitchen knives 2.0".

"Gemfile" and "Gemfile.lock" are required to manage the version with this Bundler.

Gemfile The place to manage gems installed by this bundler is "Gemfile"

A file that manages the list of "gems" used in Rails applications.


source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.6'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme
# for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster.
# Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5.x'
# Build JSON APIs with ease.
# Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '~> 2.13'
  gem 'selenium-webdriver'

group :development do
  # Access an IRB console on exception pages or by using
  # <%= console %> anywhere in the code.
  gem 'web-console'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running
  # in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'

#Tzinfo in Windows environment-Must include a gem called data
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

The source at the top is the code that specifies the bookstore, which comes out a little from the previous time.

I fetch various books "gem" from the bookstore. Rails, sqlite2, puma, etc. You can also specify the version with the second argument of the gem, as you can check the function of each gem.

1 2
No arguments Insert the latest ver
~>1.1 1.1 or later ~ 2.Limited to ver before 0
>=1.0 1.Limited to 0 or more vers
  • The group that appears on the way can specify the gem installation location. This group can also be specified to be used only in the development environment and test environment.

Gemfile.lock This file shows a list of "gems" installed by bundler. Dependent gems are also displayed here.

For example, there is no "action cable" in "Gemfile", but since this is a gem that Rails depends on, `bundle install` will show it in Gemfile.lock. (It is also installed with `` `gem install rails```)

If gem rails is mentioned in the Gemfile, rails and the action cable that rails depends on will be installed. Quote

+ Α bundle exec command

This command "uses the gem written in Gemfile.lock".

Dad and children finally do "make mom easy". However, in fact, there were books in this house, "How to use a kitchen knife 1.0", "How to use a kitchen knife 1.67", and "How to use a kitchen knife 2.0". What I want to use is "How to use a kitchen knife 2.0", which is also written in "Gemlock.file".

If you don't use bundle exec, children will say "How to use kitchen knives 1".It is possible that "0" is seen and executed, and "make mom comfortable" fails.

 Therefore, for example, in the case of ``` rails s```, it is necessary to use `` `bundle exec rails s```.

# Impressions / Issues
 I tried to create an image to understand it within myself, but I cannot deny the feeling that it became redundant.
 Even "gems" like the basic knowledge of course are deep and I can't understand everything.
 (Whether the path option is needed during bundle install, etc.)

 I want to move forward little by little.

 You pointed out when you posted for the first time. I was completely misunderstanding about Bundler.
 Thank you for the opportunity to learn and modify.

# Citation / Reference







Recommended Posts