0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

.Gemfile .Gemfile.lock まとめ

0
Posted at

概要

  • railsアプリを作成するにあたり、最初に理解しておきたいgemfileとgemfile.lock
  • だいぶ昔に勉強したこともあり内容をぼんやりとしか覚えていなかった
  • まとめていた内容が稚拙で、見てもわからなかったので、再度まとめて将来の自分が一発で理解できるようにする

参考資料:Gemfile

参考資料2:Gemfile?bundler?Rails開発するなら理解しておきたいgem管理の基礎知識

参考資料3:Bundler, Gemfile, Gemfile.lock について

gemとは

以下参照

gemfileとは

  • gemの依存関係を記述するファイルのこと
  • gemfileに記載してある内容に従ってbundlerが供給元(gemfileのsourceに指定するリソース。ほとんどがrubygemsからgemを探してくる

gemfile.lockとは

  • bundler1によってインストールされた全てのgemとそのgemのバージョンが記載される
  • gemfileに記載されているgemと依存関係を持っているgemも含めてインストールし、それらすべての情報が記載される

gemfileに記載したgemは他と依存関係を持っているgemもたくさんあり、それらも含めてインストールされる必要がある

ルール

  • プロジェクトのルートディレクトリに配置する(Railsアプリであれば、そのルートディレクトリ)

記載する内容

1. gemの供給元

.Gemfile
source <供給元URL> #e.g. "https://rubygems.org"

2. rubyバージョン

  • アプリケーションで必要になるRubyのバージョン情報を記載
.Gemfile
ruby "2.6.6"

関連【Rubyエンジン】

  • Rubyのエンジン(処理系)2も指定できる
  • エンジンを指定するならそのバージョン情報も記載する必要がある
.Gemfile
ruby "1.8.7", :engine => "jruby", :engine_version => "1.6.7"

3. Gem

  • 必要なGemを記載する
  • バージョンを指定できる
Gemのバージョン指定方法

.Gemfile
gem 'rails', '5.0.0' #railsバージョン5.0.0固定
gem 'rails', '>=5.0.0' #railsバージョン5.0.0以上
gem 'rails', '>=5.0.0', '<6.0.0' #railsバージョン5.0.0以上6.0.0未満
gem 'rails', '~>6.0.0' #railsの6.0.x マイナーバージョンまで固定
gem 'rails', '~>6.0' #railsの6.x.y メジャーバージョンまで固定

4. 特定の環境のみでの利用

  • groupを指定する(:test,:development,:production)
.Gemfile
group :test do
  gem 'faker'
  gem 'rspec'
end
group :test, :development do
  gem 'capybara'
  gem 'rspec-rails'
end
  1. gem同士の依存関係を解決しながら管理できるライブラリ。gemで公開されている。参照

  2. Rubyにも処理系があり、デファクトスタンダードはCRuby(だよね?)であるが他にもJRubyなどがあるみたい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?