For Developers

Complete Tutorial: Download and Install Ruby Gems from Gemfile

Download and Install Ruby Gems from Gemfile

Ruby on Rails is a famous framework that is used to develop web applications given to its intuitive syntax. Several well-known and high-traffic websites like Twitter, Shopify, and online news sites employ this popular web framework. It includes packages, which we refer to as gems or Ruby gems, that can be used and shared among developers easily. If you’re a developer curious to know how to download and install Ruby gems from Gemfile, this article will be of help as it discusses the process step-by-step.

What do Ruby gems do?

It can be said that Ruby gems resemble libraries in other languages. They allow developers to reuse codes from another project for a feature they want to have in any other app.

Ruby gems allow you to:

  • Build a web application.

  • Work with external services, such as APIs, with ease.

  • Add a login feature to a Ruby on Rails application.

The format and file structure make Ruby easy to understand. It also has a very different workflow.

List of Ruby gems

1. Asset pipeline

This is a plug-in for the Rails app. It manages assets like CSS, JavaScript, and images. It ensures that it copies all these assets as required when you run rake assets:precompile or deploy your application. Many popular websites like Google Analytics and Gmail use the asset pipeline.

2. Ransack

This is the best alternative to a full-text search library for Rails. It enables you to add custom logic for ranking and filtering. It also allows you to add additional functionality with just a few lines of code, such as pagination. You can expect sane default results.

3. Factory girl

This gem provides a factory object that makes writing factories a cakewalk. It comes in very useful when you want to use factories to generate test data. As it is based on ActiveRecord, it lets you define factories and Ruby instead of SQL queries or other custom languages.

4. Devise

Devise is a flexible authentication solution for Rails that is used by popular web apps like Groupon, Facebook, NumLooker, and others. It provides an array of out-of-the-box features. It’s small, easy to set up, and is useful when you need to implement authentication logic in Ruby.

Which Ruby version should you choose?

We now move on to the various versions of Ruby to help you make the right decision about Ruby gems installation.

To specify the version number on the command live, use the e -v flag:

gem install gemname -v 3.14

Code source

You will come across several options while specifying version numbers in a Gemfile.

  • When no version number is specified, such as (gem 'gemname'), it will install the latest version that runs well with other gems in the Gemfile.

  • If the exact version number is specified, such as (gem 'gemname', '3.14'), it will install the same. However, the installation will fail if it is incompatible with the other gems in the Gemfile.

  • The optimistic minimum version number, say (gem 'gemname', '>=3.14'), will install the latest version that is compatible with the other gems in the Gemfile. The installation will fail if no version greater than or equal to 3.14 is compatible with the gem list in the Gemfile.

  • The pessimistic minimum version number, say (gem 'gemname', '~>3.14'), is a functionality equivalent to gem 'gemname', '>=3.14', '<4'. It means that the number is permitted to increase only after the final number.

Best practice to follow

You may want to use a Ruby version management library like rvm or rbenv. They will help you install different versions of Ruby runtimes and gems. This comes in handy when you work on a project as most projects are coded against a known Ruby version.

Downloading Ruby gems from Gemfile

STEP 1: Start by searching for Ruby download in your browser.

Search for ruby download windows on your browser_5_11zon.webp

Image source

STEP 2: Click on the rubyinstaller for windows (Ruby installer) as shown in the picture.

Click rubyinstaller for windows in the search results._1_11zon.webp

Image source

STEP 3: Click on the download button and let the process start.

Homepage of rubyinstaller with the download button._2_11zon.webp

Image source

STEP 4: Download a suitable version according to your operating system from the window that appears.

List of operating system while you download ruby from rubyinstaller_4_11zon.webp

Image source

STEP 5: Click on the downloaded file, accept the license, click next, and let it install.

installation in process._3_11zon.webp

Image source

Installing Ruby gems from Gemfile

If you are not using Ruby < 1.9, install Ruby gems manually as it will not be included natively.

STEP 1: Enter the command below to install a Ruby gem.

gem install [gemname]

Code source

If you are working on a project that includes dependency on a list of gems, they will be listed in a file named “Gemfile”.

STEP 2: If you want to install a new gem in your project, add the given line of code in the Gemfile.

gem 'gemname'

Code source

Note: Bundler gem utilizes Gemfile to install dependencies required in a project. You will have to install Bundler first.

Here’s how.

gem install bundler

Code source

STEP 3: Save the file and run the following command.

bundle install

Code source

What to do if a Ruby application won’t start

If your Ruby application won’t start, it’s likely because of a missing gem. To rectify it, proceed with the Ruby gems installation locally using Bundler.

Bundler is a gem that manages dependencies. It offers an appropriate environment for Ruby projects. It tracks and installs the exact gems and versions that a project requires. By eliminating dependencies, it ensures that the gems you need are available in the development, staging, and production phases.

Here’s how you can set up and use Bundler for your RubyGem application.

Note: These steps are to be followed only after you have set up Ruby on Rails.

STEP 1: Run the following command on a terminal window.

[server]$ gem install bundler

Code source

STEP 2: Search and navigate to your project’s root directory.

STEP 3: Specify the dependencies in a Gemfile like below.


source 'https://rubygems.org'

gem 'multi_json'

gem 'rack'

gem 'rspec', '~>3.7.0'

Code source

STEP 4: Install the required gems from the specified sources with the following command.

[server]$ bundle install

Code source

Note: The second command adds the Gemfile.lock and Gemfile to your repository. With this, all developers working on your app can use the same third-party code.

STEP 5: Load a bundled environment inside your app with the following code.

require 'rubygems' require 'bundler/setup'

# require your gems as usual require 'multi_json'

Code source

Note: This step is necessary only if you are not running RVM.

STEP 6: Run an executable that comes along with a gem in your bundle by using the following command.

[server]$ bundle exec rspec spec/models

Code source

Note: If the executable is installed in your system, running it without the “bundle exec” command may work. However, though it won’t pull in any gems that conflict with your bundle, it’s unreliable and not recommended. It may look like it works for now, but it may not in the future or on any other machine.

STEP 7: You can also create a shortcut to gems in your bundle with the following command.

[server]$ bundle install --binstubs [server]$ bin/rspec spec/models

Code source

Additional info to get you started

How to create Ruby gems?

Bundle is an easy way to create new Ruby gems. You can create a new gem with README, .gemspec, Rakefile, directory structure, and others to test, describe, and publish gems.

Here’s the code for the same.


[server]$ bundle gem my_gem

create  my_gem/Gemfile
      create  my_gem/.gitignore
      create  my_gem/lib/my_gem.rb
      create  my_gem/lib/my_gem/version.rb
      create  my_gem/my_gem.gemspec
      create  my_gem/Rakefile
      create  my_gem/README.md
      create  my_gem/bin/console
      create  my_gem/bin/setup
      create  my_gem/CODE_OF_CONDUCT.md
      create  my_gem/LICENSE.txt
      create  my_gem/.travis.yml
      create  my_gem/test/test_helper.rb
      create  my_gem/test/my_gem_test.rb
Initializing git repo in ./my_gem

Code source

Follow the step-by-step process to download and install gems with the help of this easy-to-follow Ruby gems tutorial. The additional information provided will also speed up your workflow.

FAQs

1. How do I set gem version in Gemfile?

Go to rubygems.org and search for specific versions of your gem. Use version operator: gem "name-of-gem", ">1.0".

2. How do I know if a ruby gem is installed?

Use gem list to get details about whether your Ruby gem is installed in the correct version.

3. Where do RubyGems get installed?

Ruby gems are installed in a directory inside the home directory.

4. How do I install gem packages?

Step 1: Install Ruby.

Step 2: Install Gems.

Step 3: Use the gem system to install Rails.

Step 4: Install Sqlite3.

Press

Press

What's up with Turing? Get the latest news about us here.
Blog

Blog

Know more about remote work.
Checkout our blog here.
Contact

Contact

Have any questions?
We'd love to hear from you.

Hire and manage remote developers

Tell us the skills you need and we'll find the best developer for you in days, not weeks.

Hire Developers