Ruby bundler

From wikinotes

Commandline Tool to manage a set of ruby gem requirements using a ruby gemfile.
This is similar to python-pip's requirements.txt file.

Documentation

bundler quickstart https://bundler.io/
bundler config options https://bundler.io/v2.2/man/bundle-config.1.html#LIST-OF-AVAILABLE-KEYS
bundler creating gems https://bundler.io/v2.0/guides/creating_gem.html
rubygems creatin gems https://guides.rubygems.org/make-your-own-gem/

Locations

${project}/.bundle/config bundle config (Remembers things like gem path)
~/.bundle/config<?code> global config

Install

gem install bundler

Consuming Gems

Usage

Create Project

# generate gemfile using 'rubygems.org' as source
bundle init
bundle update --bundler                    # update bundler version in Gemfile.lock
bundle config set --local path '~/.bundle'  # use user bundle path

Project Management

# install all gemfile requirements
bundle list                  # list installed
bundle install
bundle install --path .gems  # install to {cwd}/.gems (and remember for future bundle commands)

bundle lock --add-platform x86_64-linux

# install gemfile requirements for specific groups 'cucumber', 'development'
bundle install --without cucumber development

Gem Build Arguments

You can configure compile-time arguments for use when a gem with C-extensions is built.
This can be stored in your project-local, or global bundle config.

# writes ${CWD}/.bundle/config
bundle config --local build.ffi --with-cflags="-Wno-error=implicit-function-declaration" --enable-system-libffi

# writes ~/.bundle/config
bundle config --global build.snappy --with-opt-dir="/usr/local/opt/snappy"

Updating Gems

bundle update rails                   # update rails to latest only
bundle update --conservative sidekiq  # update this gem only, ignoring shared dependencies
bundle lock --update=foo              # update 'foo' only to latest in Gemfile.lock
bundle update                         # try to update everything

Gemfiles

See ruby gemfile (outlines gem requirements for project).
Note that if you are writing a gem for rubygems, requirements should be defined in the gemspec file.

Personal/Dev Gemfiles

If using an LSP, your work Gemfile may not have all of the requirements that you need.
You can work around this by defining an alternate Gemfile that sources the real one and adds requirements.

# ${PROJECT}/Gemfile

source "https://rubygems.org"
gem "pry"
# ${PROJECT}/Gemfile.pers

gemfile = File.realpath("Gemfile")
eval(File.read(gemfile), nil, "Gemfile.pers")
gem "solargraph"
gem "solargraph-rails"

Set Gemfile for bundle install, and applications you'd like to use this Gemfile for.

# install gems
BUNDLE_GEMFILE=Gemfile.pers bundle install  # (may need to bundle update)

# runs nvim, instructing it to use the alternate gemfile
BUNDLE_GEMFILE=Gemfile.pers nvim

You probably want to git ignore this file so it is never committed.

# ~/.gitignore
**/Gemfile.pers
**/Gemfile.pers.lock

Publishing Gems

Create a ruby gemspec file, build a gem, and push it to rubygems.org

Build/Release

gem build foo.gemspec
gem push foo-0.0.0.gem