0
0
Rubyprogramming~5 mins

Gem installation with gem install in Ruby

Choose your learning style9 modes available
Introduction

We use gem install to add new tools or libraries to Ruby. These tools help us write programs faster and easier.

You want to add a new feature to your Ruby program, like working with web pages or databases.
You need to use code someone else wrote to save time.
You want to try out a new Ruby library you found online.
You are setting up a new Ruby project and need extra tools.
You want to update or install a specific version of a Ruby gem.
Syntax
Ruby
gem install gem_name [options]

Replace gem_name with the name of the gem you want to install.

You can add options like --version to install a specific version.

Examples
Installs the latest version of the rails gem.
Ruby
gem install rails
Installs version 1.13.3 of the nokogiri gem.
Ruby
gem install nokogiri --version 1.13.3
Installs the bundler gem, which helps manage other gems.
Ruby
gem install bundler
Sample Program

This example shows how to install the colorize gem and then use it to print blue text in Ruby.

Ruby
# This is a command line example, not a Ruby script
# Open your terminal or command prompt and type:
gem install colorize

# After installation, you can use the gem in your Ruby code like this:
require 'colorize'

puts 'Hello, world!'.colorize(:blue)
OutputSuccess
Important Notes

You need an internet connection to download gems.

Sometimes you may need to run gem install with sudo on Linux or macOS to get permission.

After installing a gem, you can use require 'gem_name' in your Ruby code to use it.

Summary

gem install adds new Ruby libraries to your system.

You run it in the terminal, not inside Ruby code.

Use require in Ruby to start using the installed gem.