文档章节

Rails Bundle Install and Gemfile

Kolosek
 Kolosek
发布于 2018/02/12 16:46
字数 839
阅读 4
收藏 0

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 with bundler:

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

Bundle Install

When you run bundle install for the first time, Gemfile.lock will 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 are multiple options you 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 <path_to_folder>: The specific locations where gems are going to be installed.
  • --standalone <gem_list>: Creates a bundle that can work without depending on Bundler at runtime.
  • --without <gem_list>: Groups of gems to skip during installation.
  • --with <gem_list>: 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'
end

Setting up the Gemfile

Using source will explain the Gemfile where to look for gems: source https://rubygems.org. There is no need for more than one source per project. git and path are 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 versionsgem 'gem_name', '~> 1.3'
    If you don’t set a gem version, bundler will set it for you. To avoid that, you can use operators (=Equal To , != Not Equal To, > Greater Than, < Less 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 requiredgem 'gem_name', require: false
    In order to require gems in your Gemfile, you will need to call Bundler.require in your application. You can prevent bundler from requiring the gem with require: false, but it will still install and maintain the gem.

  • Adding gem sourcegem 'gem_name', source: 'https://gem_site.com'
    You can add source to your gems using source: 'https://gem_site.com', a git repository git: '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 is branch: '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 gemsgem 'gem_name', group: :development
    Gems can belong to one or more groups. 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 platformgem 'gem_name', platform: :jruby
    You can specify in the Gemfile if a gem is used on a specific platform (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.

© 著作权归作者所有

Kolosek
粉丝 0
博文 29
码字总数 20461
作品 0
塞尔维亚
CEO
私信 提问
Rails 3 | Bundler浅尝

--转载请注明出处 Rails3里多了个Bundler工具。 它是一个Library管理工具,它提供了好的工具来管理你rails3项目依赖的gems。 $> bundle 这个命令会列出bundler提供给你的一些工具。 Tasks: b...

blackanger
2018/06/28
0
0
rails new APP出错的解决方法

运行的时候,经常会卡住,或者出现类似以下的提示 换上淘宝源还是不能解决,只好按照提示手动安装缺失的gem包。 这是因为rails创建的Gemfile里指定被墙了的Rubygems作为源。 解决方法:首先,...

昊翔
2015/09/15
192
0
解决bundle install太慢

在使用rails new myproject ,会在myproject建立Gemfile 等一些文件 然后会提示run bundle install 这个时间比较长,解决办法: 在创建新的project时候,使用rails new myproject --skip-bund...

孔俊
2013/07/09
393
0
Rails best practices

Rails best practices 主页:http://rails-bestpractices.com/ github:https://github.com/flyerhzm/railsbestpractices 安装: ruby1.9+rails31 1. gem install railsbestpractices 2. Gem......

阿昭
2011/10/21
102
0
rails foundation

我安装了 一个最简单的 rails app 我想用用 foundation 玩玩 但是有点毛病 求解决 我是这样做的rails new fdemo 根据官网的介绍 配置 gemfile bundle install rails g foundation:install ra...

jingdor
2012/12/06
594
3

没有更多内容

加载失败,请刷新页面

加载更多

SpringBoot 操作ActiveMQ

一、消息队列中间件介绍 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合、异步消息、流量削锋等问题,实现高性能、高可用、可伸缩和最终一致性架构,是大型分布式系统不可缺少的...

zw965
25分钟前
4
0
本地化存储Storage

为什么会引入Storage(sessionStorage,localStorage,globalStorage) Web storage的目的是克服由cookie带来的限制,当数据需要严格控制在客户端上时,无须持续将数据发回服务器。主要是提供一...

五公里
34分钟前
8
0
Qt编写自定义控件57-直方波形图

一、前言 直方波形图控件非原创控件,控件大全中大概有20-30个控件非自己原创,而是参考了网上开源的代码,自己加以整理和完善,新增了插件的代码使得可以直接集成到QtDesigner或者QtCreator...

飞扬青云
34分钟前
4
0
或许你不知道的10条SQL技巧

这几天在写索引,想到一些有意思的TIPS,希望大家有收获。 一、一些常见的SQL实践 (1)负向条件查询不能使用索引 select * from order where status!=0 and stauts!=1 not in/not exists都不...

mskk
41分钟前
7
0
IntelliJ IDEA (Mac) 运行速度优化(JVM+localhost)

1.1. JVM 参数配置 打开 idea,菜单 –> help –> edit custom vm options,调整参数,重启即可。 具体调整参数: -Xms2g -Xmx2g -XX:ReservedCodeCacheSize=1024m -XX:+UseCompressedOops 1......

指尖Coding
49分钟前
10
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部