Steps to Create a Gemfile in Ruby

To create a Gemfile for our Ruby project, follow these simple steps:

Step 1: Create a New Directory (Optional)

We will start by creating a new directory for our project using the `mkdir` command in our terminal:

mkdir my-project

cd my-project

We use the “mkdir”(make directory) command to create a new directory in a command line environment. Then “cd” command to “change directory” to the folder we created earlier on.

Creating a new folder and changing the directory.

Step 2: Initialize a New Ruby Project

Let’s now initialize our project using the following command if not initialized before:

bundle init

This command will create a new file named Gemfile in our project directory.

Writing new Gemfile to /home/gatwiri/my-project/Gemfile

The above output shows that our ‘Gemfile file’ has been created.

Step 3: Edit the Gemfile

We will now open the Gemfile using a text editor of our choice e.g VScode. And add gem dependencies to this file using the `gem` key-word. For instance, let’s add the ‘faker’ and `colorize` gems as dependencies in our Gemfile:

# frozen_string_literal: true
source “https://rubygems.org”

# gem “rails”
gem ‘faker’
gem ‘colorize’

Our gemfile will look as shown above.

The “faker” gem is used to generate fake data for testing or seeding a database, while “colorize” is useful for adding color to text output in the terminal.

Step 4: Specify Gem Versions (Optional)

We can specify the version of the gem we want to use by adding it after the gem name. For example:

gem ‘faker’, ‘3.3.1’

This ensures that when we run bundle install, it will install version 3.3.1 specifically, and our project will use that version. This is a common practice to avoid potential issues with breaking changes in newer major versions.

Faker version 3.3.1

Step 5: Install Gems

Once we’ve added all the gems we need to our Gemfile, let’s save the file and run the following command in our terminal to install them:

bundle install

This command downloads and installs all the gem given in our Gemfile, as well as its dependencies, and creates a `Gemfile.lock` file to lock the version of the gem.

Installing dependencies.

Gemfile.lock file

Step 6: Using the Gems in Our Ruby Code

Let’s use the installed gems by requiring them:

require ‘faker’

require ‘colorize’

Example:

Lets create an `app.rb` file and add the following code:

Ruby
# app.rb
require 'faker'
require 'colorize'

puts "Welcome to the Fake Name Generator!".colorize(:green)

10.times do
  name = Faker::Name.name
  puts "- #{name}".colorize(:blue)
end

The code above utilizes the Faker gem to generate fake names and the Colorize gem to add color to the output in the terminal. When we run this Ruby script, it will print a welcome message in green and generate 10 fake names, each in blue color. This will add some visual appeal to our output.

To run our demo app we will use the following command:

ruby app.rb

Running ‘ruby app.rb’ executes the Ruby script `app.rb`.

Output:

Output

That’s it! We’ve created a Gemfile for our Ruby project and installed our gem dependencies using bundler.



How to Create Gemfile in Ruby?

A Gemfile is a configuration file that specifies the gem requirements needed to run a Ruby program. A Gemfile should always be located at the root of the project directory. To create a Gemfile we usually use a bundler which is a popular gem management tool. This article focuses on discussing steps to create a gemfile in Ruby.

Similar Reads

Steps to Create a Gemfile in Ruby

To create a Gemfile for our Ruby project, follow these simple steps:...

Contact Us