How to Install Gems in Ruby on Rails

2:21

A Ruby gem is a library you can use in your Ruby applications. It's really easy to install a gem. You just need to know the name of the gem you want to install. In this Treehouse Quick Tip, Ruby teacher Jason shows you how to install the Bundler gem.

  • 0:00

    [treehouse presents]

  • 0:03

    [Quick Tips] [How to Install Gems in Ruby on Rails] [by Jason Seifer]

  • 0:06

    Hi, I'm Jason.

  • 0:07

    In this treehouse quick tip, we're going to talk about how to install Ruby gems.

  • 0:11

    A Ruby gem is a library that you can use in your Ruby applications.

  • 0:15

    It's really easy to install a gem.

  • 0:18

    You just need to know the name of the gem that you want to install.

  • 0:21

    In this example, I'm going to install the Bundler gem.

  • 0:25

    The way to install a gem is by typing the word "gem" followed by a space

  • 0:31

    followed by the word "install" and then the name of the gem that you want to install.

  • 0:36

    I'm going to install the Bundler gem.

  • 0:38

    After that, press "enter" and then Ruby gems will go out, download the proper gem,

  • 0:43

    and install it onto your system.

  • 0:45

    If you get a message about not having the proper permissions to install a gem,

  • 0:50

    you can use the "sudo" command to install the correct gem.

  • 0:54

    You would use the "sudo" command on systems where your version of Ruby is installed

  • 1:00

    globally rather than on a system like RVM or RBN where you can install gems locally

  • 1:06

    to your own home directory.

  • 1:08

    I'm going to install another gem called Money.

  • 1:14

    The gem install command will also install any needed dependencies of a gem.

  • 1:18

    For example, the Money gem requires the i18n gem and also the json gems.

  • 1:24

    Once you install a gem, it's really easy to use.

  • 1:27

    First, require Ruby gems, then you can also require the gem that you just installed.

  • 1:36

    And there you go.

  • 1:37

    There's one other way to install gems, and that would be from a gem file.

  • 1:42

    A gem file will be in a Ruby on Rails application or actually any Ruby application

  • 1:48

    that uses Bundler.

  • 1:49

    You specify the gem dependencies in the gem file, and then once you do that,

  • 1:54

    you can type the word "bundle install" and then Bundler will go through and install

  • 2:01

    all of these gems for you.

  • 2:09

    As you can see, it's very easy to install and use Ruby gems.

  • 2:12

    The hardest part is finding good ones to install.

  • 2:15

    Luckily, there are sites like RubyGems.org and GitHub for that.

  • 2:19

    Happy coding!

Show full transcript