Ruby setting 2

What is gem?

--The library for the Ruby language provided in a fixed format is called "gem". --For example, "gem" includes the following. --Rails (Web framework) --Sinatra (Web framework) --omniauth (authentication function) --unicorn (application server) --Rspec (test framework) ...etc --Corresponds to CPAN in Perl, pear in php, etc.

What is Rubygems

--A package management system dedicated to gems, similar to yum and apt. --The following site discloses and provides information on gems.

What is Bundler?

--Gem can be manually installed individually as follows. - gem install "sinatra" - gem install "unicorn" - gem install "omniauth" ――However, when using various gems (libraries) in combination, there may be a compatibility problem such as "gemA ver1 and gemB ver2 work well, but the latest versions do not work well." There is. --In addition, when developing with multiple people and multiple environments, it is necessary to match the name and version of the library used in each environment. ――In such a case, Bundler will install and manage each gem while maintaining compatibility between gems. --Write the gem you want to install in a file called Gemfile, and install using bundler based on it. --Basically, it is recommended to install gems other than bundler via bundler.

How to install Bundler

--Installing Bundler --Bundler is also one of the gems. Let's download manually - gem install bundler --Check that the version is displayed after installation. - bundler -v

How to use Bundler

  1. Creating a Gemfile
    • bundle init --When you execute the above, a template file called Gemfile will be created.
  2. Describe the gem you want to use in the Gemfile --Edit Gemfile - vi Gemfile --At the beginning of the Gemfile, declare that the installation source is rubygems. - source "rubygems" --Continue to describe the gem you want to install - gem "sinatra" - gem "unicorn" - gem "omniauth" --You can also specify the version when specifying the gem. - gem 'rails', "~> 4.2.4"
  3. Bulk installation of gems listed in Gemfile --Batch installation - bundle install --path vendor/bundle ---- You can specify the installation destination with path. After installing with --path specified even once, the same path will be selected even if bundle install is performed without specifying path from the next time. --At this time, a file called "gemfile.lock" is created that describes the name and version of the installed gem.
  4. Load the installed gem on the program source --First, require rubygems on the source. - require "rubygems" --If you write as follows, it will require from the installed contents (If you do not specify it, the latest system gem will be used) - require "bundler/setup"
  5. When running the program, first run bundle exec. (If not specified, the latest system gem will be used without considering compatibility)
    • bundle exec ruby foo.rb


--Recommendation of Rubygems | For beginners who are not familiar with Ruby --Bundler overview --What is Bundler?

Gem command and Bundler basics -BlueTechNote

Use Gem command

$ gem install
$ gem update
$ gem uninstall

bundler --Install Bundler --Write the Gem to install in the Gemfile --Install with the bundle install command --Upgrade the Gem with the bundle update command

-Install Bundler
(m ・_・ Bp) 17:50 ~/Desktop/vsnote2.0/ruby % gem install bundler
Fetching bundler-2.1.4.gem
Successfully installed bundler-2.1.4
Parsing documentation for bundler-2.1.4
Installing ri documentation for bundler-2.1.4
Done installing documentation for bundler after 2 seconds
1 gem installed

-Write the Gem to install in the Gemfile

(m ・_・ Bp) 17:51 ~/Desktop/vsnote2.0/ruby % bundle init
Writing new Gemfile to /Users/uekiyoshihiro/Desktop/vsnote2.0/ruby/Gemfile

-Install with the bundle install command

(m ・_・ Bp) 17:53 ~/Desktop/vsnote2.0/ruby % bundle install
Fetching gem metadata from
Resolving dependencies...
Using bundler 2.1.4
Fetching coderay 1.1.2
Installing coderay 1.1.2
Fetching method_source 1.0.0
Installing method_source 1.0.0
Fetching pry 0.13.1
Installing pry 0.13.1
Bundle complete! 1 Gemfile dependency, 4 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

-Upgrade Gem with the bundle update command

(m ・_・ Bp) 17:58 ~/Desktop/vsnote2.0/ruby % bundle update
Fetching gem metadata from
Resolving dependencies...
Using bundler 2.1.4
Using coderay 1.1.2
Using method_source 1.0.0
Using pry 0.13.1
Bundle updated!

Frequently used commands

$ bundle init #Generate Gemfile
$ bundle install	#Install the gem package written in the Gemfile
$ bundle exec	#Run the command using the gem package installed with Bundler
$ bundle list	#View a list of installed gem packages
$ bundle update	#Update installed gem package version

Recommended Posts

Ruby setting 2
Ruby setting 3 Rubocop
Ruby learning 4
[Ruby] Array
Ruby basics
Ruby learning 5
Ruby basics
Ruby Review 2
Ruby addition
Refactoring Ruby
Ruby learning 3
Ruby problem ⑦
Ruby learning 2
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module
Introduction to Ruby 2
Ruby input / output
ruby Uppercase letters
ruby search problem
Ruby Learning # 25 Comments
ruby constant variable
Ruby text conversion
Ruby basic terms
ruby exception handling
Ruby Learning # 13 Arrays
About Ruby symbols
Ruby Learning # 1 Introduction
[ruby] drill output
I started Ruby
[Ruby] Iterative processing
ruby API problem
Ruby vertical writing
About ruby ​​form
[ruby] drill output
About Ruby Hashes
Ruby Learning # 14 Hashes
[ruby] drill output
[Ruby] each nested
[Ruby] Arithmetic progression
[Ruby] FizzBuzz problem
Ruby Hash notes
[Ruby] Regular expression for secure password policy setting
Class in Ruby
[Ruby] slice method
Ruby standard output
[Ruby] end_with? method
Chart type Ruby
[Ruby] Hash retrieval
Basics of Ruby
[WIP] Ruby Tips
Ruby exception handling
About Ruby arrays