Skip to content

Latest commit

 

History

History
295 lines (238 loc) · 8.16 KB

bundler-and-gems.adoc

File metadata and controls

295 lines (238 loc) · 8.16 KB

Bundler and Gems

Gems constitute the package management in the world of Ruby.

Tip
If you do not have much time, you can skip this chapter for now and get back to it later if you have any specific questions.

If a Ruby developer wants to offer a specific feature or a certain program or collection of programs to other Ruby developers, he can create a "gem" from these. This gem can then be installed with the command gem install.

Tip
Have a look at https://www.ruby-toolbox.com to get an overview of the existing gems.

In any Rails project, different gems are used and a developer can also add further gems. The programm bundle helps the developer to install all these gems in the right version and to take into account dependencies.

The file Gemfile generated by rails new indicates which gems are to be installed by Bundler:

Gemfile
source 'https://rubygems.org'

git_source(:github) do |repo_name|
  repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
  "https://github.com/#{repo_name}.git"
end

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.0'
# 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', github: "rails/sass-rails"

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# 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'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# 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.0'
  gem 'selenium-webdriver'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  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'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

The format used is easy to explain: the word gem is followed by the name of the gem and then, if required, a specification of the version of the gem.

For example, the line gem 'rails', '5.1.0' stands for "install the gem with the name rails in the version 5.1.0".

With ~> before the version number you can determine that the newest version after this version number should be installed. As a result, the last digit is incremented, so for example gem 'rails', '~> 4.0.0' would correspondingly install a Rails 4.0.1, but not a 4.1 (for the latter, you would need to specify gem 'rails', '~> 4.1').

Note
You have the option of installing certain gems only in certain environments. To do so, you need to enclose the corresponding lines in a group :name do loop.

Apart from the file Gemfile there is also the file Gemfile.lock and the exact versions of the installed gems are listed there. In the above example, it looks like this:

Gemfile.lock
GIT
  remote: https://github.com/rails/sass-rails.git
  revision: dfbcc6a53653d8908007e26324a0f299f026ec4f
  specs:
    sass-rails (6.0.0.beta1)
      railties (>= 4.0.0, < 5.1)
      sass (~> 3.4)
      sprockets (~> 4.x)
      sprockets-rails (< 4.0)

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (5.1.0)
      actionpack (= 5.1.0)
      nio4r (~> 2.0)
      websocket-driver (~> 0.6.1)
      [...]

The advantage of Gemfile.lock is that it makes it possible for several developers to work on the same Rails project independently from one another and to still be sure that they are all working with the same gem versions. If a file is Gemfile.lock, this will be used by the Bundler. This is also useful for deploying the Rails project later on a web server.

Thanks to this mechanism you can use and develop several Rails projects with different gem version numbers in parallel.

bundle update

With bundle update you can update gems to new versions. As an example, I have a Rails project with the Rails version 4.2.1:

$ rails -v
Rails 4.2.1
$

In the file Gemfile, this version is listed:

Gemfile
source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'
[...]

And also in the Gemfile.lock:

$ grep 'rails' Gemfile.lock
  [...]
  rails (= 4.2.1)
  [...]
$

Assumed we are working with rails 4.2.0 and we want to update to rails 4.2.4. Then we have to change the Gemfile from this:

Gemfile
[...]
gem 'rails', '4.2.0'
[...]

to this:

Gemfile
[...]
gem 'rails', '4.2.4'
[...]

After this change, you can use bundle update rails to install the new Rails version (required dependencies are automatically taken into account by Bundler):

$ bundle update rails
  [...]
$ rails -v
Rails 4.2.4
$
Important
After every gem update, you should first run rake test to make sure that a new gem version does not add any unwanted side effects.

bundle outdated

If you want to know which of the gems used by your Rails project are now available in a new version, you can do this via the command bundle outdated. Example:

$ bundle outdated
Fetching gem metadata from https://rubygems.org/...........
Fetching version metadata from https://rubygems.org/...
Fetching dependency metadata from https://rubygems.org/..
Resolving dependencies....

Outdated gems included in the bundle:
  * hiredis (newest 0.6.1, installed 0.5.2)
  * mime-types (newest 3.0, installed 2.99)
  * mini_portile2 (newest 2.1.0, installed 2.0.0)

To update them you’ll have to change the version numbers in Gemfile and run a bundle update.

bundle exec

bundle exec is required whenever a program such as rake is used in a Rails project and is present in a different version than the rest of the system. The resulting error message is always easy to implement:

You have already activated rake 0.10, but your Gemfile requires rake 0.9.2.2.
Using bundle exec may solve this.

In this case, it helps to invoke the command with a preceding bundle exec:

$ bundle exec rake db:migrate

binstubs

In some environments, using bundle exec is too complicated. In that case, you can install programs with the correct version via bundle install --binstubs in the directory bin:

$ bundle install --binstubs
Using rake 10.4.2
Using i18n 0.7.0
[...]
Using web-console 2.1.2
Bundle complete! 12 Gemfile dependencies, 54 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.

Afterwards, you can always use these programs. Example:

$ bin/rake db:migrate
==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.0018s
==  CreateUsers: migrated (0.0019s) ===========================================

Further Information on Bundler

The topic Bundler is far more complex than can be described here. If you want to find out more on Bundler, please visit the following websites to find further information: