1
1

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 5 years have passed since last update.

SinatraにBundler導入〜起動まで

Posted at

SinatraにBundlerを導入し、起動までやってみたので、
備忘録として残します。

はじめに

Gemとは
Rubyで用意されている機能で、誰かが作ってくれたソースコードのパッケージを自分のプログラムにうまく取り込むための仕組み(ライブラリ)。
gemを利用することで0から開発をしなくても手軽に便利な機能を実装することが可能です。

Gemfileとは
必要なgemとそれらのバージョンの一覧が記載されたファイル。

Gemfile.lockとは
Gemfile.lockはGemfileを元に実際に取得してきた各gemとそれらのバージョンが記載されているファイル。

Bundlerとは
Rubyで用意されている機能で、Gemを管理する仕組み。
たくさんのGemをGemfileを元にワンアクションでインストールしてくれる。

Bundlerのインストール

まずは以下のコマンドでBundlerをインストールします。

$ gem install bundler

Gemfile作成

以下のコードを実行し、Gemfileを作成します。

$ bundle init

Gemfileに以下のコードを追加します。

Gemfile
gem 'sinatra'
gem 'sinatra-reloader'

コード追加後のGemfileは以下のようになりました。

Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

gem 'sinatra'
gem 'sinatra-reloader'

Rubyファイルにコードを追加

BundlerをSinatraで使うために、Rubyファイル(app.rbなど)に以下のコードを追加します。
参照:How to use Bundler with Sinatra

app.rb
require 'rubygems'
require 'bundler'

Bundler.require

Gemのインストール

$ bundle install

# 特定のフォルダにひとまとめにインストールする場合
$ bundle install --path vendor/bundle

bundle installを実行すると、Gemfile.lockを元にgemをインストールします。
ただし、Gemfile.lockが存在しない場合、Gemfileを元にgemをinstallした後、Gemfile.lockを作成します。

サーバー起動

$ bundle exec ruby app.rb

参照

1
1
1

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?