0
0
RubyHow-ToBeginner · 3 min read

How to Use Bundle Install in Ruby: Simple Guide

Use bundle install in your Ruby project directory to install all gems listed in the Gemfile. This command reads the Gemfile.lock to ensure consistent gem versions and sets up your environment with required libraries.
📐

Syntax

The basic syntax for installing gems with Bundler is:

  • bundle install: Installs all gems specified in the Gemfile.
  • --path: Optionally specify a directory to install gems locally.
  • --without: Skip installing gems from specified groups.
bash
bundle install [--path=DIR] [--without GROUP1 GROUP2 ...]
💻

Example

This example shows how to use bundle install to install gems for a Ruby project with a Gemfile.

bash
mkdir my_project
cd my_project
echo "source 'https://rubygems.org'\ngem 'sinatra'" > Gemfile
bundle install
Output
Fetching gem metadata from https://rubygems.org/... Resolving dependencies... Using rack 2.2.4 Using mustermann 1.1.1 Using rack-protection 2.1.0 Using tilt 2.0.11 Using sinatra 2.1.0 Bundle complete! 1 Gemfile dependency, 5 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed.
⚠️

Common Pitfalls

Common mistakes when using bundle install include:

  • Running bundle install outside the project directory without a Gemfile.
  • Not committing Gemfile.lock to version control, causing inconsistent gem versions.
  • Ignoring errors about missing native extensions or system libraries.

Always ensure you are in the directory with the Gemfile and check error messages carefully.

bash
cd ~
bundle install
# Error: Could not locate Gemfile

cd my_project
bundle install
# Correct usage inside project directory
Output
Could not locate Gemfile Bundle complete! 1 Gemfile dependency, 5 gems now installed. Use `bundle info [gemname]` to see where a bundled gem is installed.
📊

Quick Reference

CommandDescription
bundle installInstall all gems from Gemfile
bundle install --path vendor/bundleInstall gems locally in vendor/bundle folder
bundle install --without development testSkip gems in development and test groups
bundle updateUpdate gems to latest allowed versions
bundle exec Run a command in the context of bundled gems

Key Takeaways

Run bundle install inside your project folder containing the Gemfile.
The command installs all gems listed in the Gemfile and locks versions in Gemfile.lock.
Keep Gemfile.lock in version control to ensure consistent environments.
Use options like --path or --without to customize gem installation.
Check error messages carefully if installation fails, especially for native extensions.