Rails Bundle Install and Gemfile

2018-02-24 10:10:25来源:oschina作者:Kolosek人点击

分享

Bundler makes sure that Ruby applications always use the exact gems and versions that you need while keeping a consistent environment and gem dependencies satisfied. This is done by ensuring that the gems you need are present in the development, staging and the production.


Setting up a bundler

With this three simple steps you will get your application up and running withbundler:

Install (or update) bundler with the following command:$gem install bundler
Specify your dependencies in a Gemfile: gem "carrierwave", "~> 1.0"
gem "devise"
gem "pundit"
Install all the gems Rails will need to get started:$bundle install Bundle Install

When you runbundle installfor the first time,Gemfile.lockwill be added to your repository. This ensures that other developers on your Ruby application run the same third-party code on every machine.


Options

There aremultiple optionsyou can use to change the way bundle install works. We will cover the most commonly used ones.


bundle install [--deployment][--force][--frozen][--path PATH]
[--standalone[=GROUP[ GROUP...]]]
[--without=GROUP[ GROUP...]][--with=GROUP[ GROUP...]]
--deployment: Runs bundle in the production environment. Activating deployment mode on development machines will cause an error when the Gemfile is modified.
--force: Downloads every gem, even if the required versions are already available.
--frozen: Gemfile.lock will not be updated after this install.
--path : The specific locations where gems are going to be installed.
--standalone : Creates a bundle that can work without depending on Bundler at runtime.
--without : Groups of gems to skip during installation.
--with : Groups of gems to install during installation. Gemfile

A Gemfile is a file we created which is used for describing gem dependencies for Ruby programs. The Gemfile is located in the root of the project directory.


source 'https://rubygems.org'
gem 'rails', '4.2.6'
gem 'sqlite3'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
group :development, :test do
gem 'byebug'
end
group :development do
gem 'web-console', '~> 2.0'
gem 'spring'
endSetting up the Gemfile

Usingsourcewill explain the Gemfile where to look for gems:source https://rubygems.org. There is no need for more than one source per project.gitandpathare also valid gem sources.


Bundler first looks if a source location is set up for the gem. If nothing has been explicitly set, it will look at the sources you have defined, starting at the first one.


If the application requires a specific Ruby version or engine we can set this in the Gemfile in the following way: ruby '2.0.0', :patchlevel => '353', :engine => 'jruby', :engine_version => '1.6.7'

:patchlevel: Patch level for Ruby.
:engine: Type of Ruby engine.
:engine_version: Version of the Ruby engine. Setting up the gems

Gems default syntax is:gem 'gem_name'. You can improve the default syntax in the following way:

Setting versions:gem 'gem_name', '~> 1.3' If you don’t set agem version, bundler will set it for you. To avoid that, you can use operators (=Equal To ,!=Not Equal To,>Greater Than,=Greater Than or Equal To,<=Less Than or Equal To,~>Pessimistically Greater Than or Equal To).

The~>operator ensures that your application will safely work with future versions of a gem. gem 'gem_name', '~> 1.0'is the same as:gem 'gem_name', '>= 1.0', '< 2.0'


Setting gem to required:gem 'gem_name', require: false In order torequire gemsin your Gemfile, you will need to callBundler.requirein your application. You can prevent bundler from requiring the gem withrequire: false, but it will still install and maintain the gem.


Adding gem source:gem 'gem_name', source: 'https://gem_site.com' You can addsourceto your gems usingsource: 'https://gem_site.com', a git repositorygit: 'ssh@githib.com/git_repository/gem_name', or a system path:path => '.../path_to_gem/gem_name'.

When installing gems using git, you can set either a branch, tag, or ref for the gem. The default branch isbranch: 'master'. gem 'gem_name', git: 'ssh@githib.com/git_repository/gem_name', branch: gem_branch

Another gem source can be:gist, it is used if the project is hosted on Github as a gist. Gist ID is used as the path:gem 'gem_name', :gist => '8eb650c7065', branch: 'gem_branch'


Grouping gems:gem 'gem_name', group: :development Gems can belong to one or moregroups. When it doesn’t belong to any groups it is located in the :default group.

group :development, :test do
gem 'gem_name'
gem 'gem_another_name'
end

You can specify certain groups to not bundle install, this can considerably speed up the installation time.bundle install --without development test


Setting a platform:gem 'gem_name', platform: :jruby You can specify in the Gemfile if a gem is used on a specificplatform(or set of platforms). When developers use different Operating Systems during development, they will need different versions of gems depending on what is supported by their system.

platforms [:mingw, :mswin, :x64_mingw, :jruby] do
gem "gem_name"
gem "gem_another_name"
end
```s

Hope this text helped you to gain a better understanding of gems and bundle install. Thank you for reading!


Originally published on kolosek.com.

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台