Главная страница » Как установить gem на ruby скачанный

Как установить gem на ruby скачанный

  • автор:

# Gem Usage

If you are working on a project with a list of gem dependencies, then these will be listed in a file named Gemfile . To install a new gem in the project, add the following line of code in the Gemfile :

This Gemfile is used by the Bundler gem

(opens new window) to install dependencies your project requires, this does however mean that you’ll have to install Bundler first by running (if you haven’t already):

Save the file, and then run the command:

# Specifying versions

The version number can be specified on the command live, with the -v flag, such as:

When specifying version numbers in a Gemfile , you have several options available:

  • No version specified ( gem ‘gemname’) — Will install the latest version which is compatible with other gems in the Gemfile .
  • Exact version specified ( gem ‘gemname’, ‘3.14’ ) — Will only attempt to install version 3.14 (and fail if this is incompatible with other gems in the Gemfile ).
  • Optimistic minimum version number ( gem ‘gemname’, ‘>=3.14’ ) — Will only attempt to install the latest version which is compatible with other gems in the Gemfile , and fails if no version greater than or equal to 3.14 is compatible. The operator > can also be used.
  • Pessimistic minimum version number ( gem ‘gemname’, ‘

As a best practice: You might want to use one of the Ruby version management libraries like rbenv

(opens new window) . Through these libraries, you can install different versions of Ruby runtimes and gems accordingly. So, when working in a project, this will be especially handy because most of the projects are coded against a known Ruby version.

# Gem installation from github/filesystem

You can install a gem from github or filesystem. If the gem has been checked out from git or somehow already on the file system, you could install it using

Installing gem from github. Download the sources from github

# Checking if a required gem is installed from within code

To check if a required gem is installed, from within your code, you can use the following (using nokogiri as an example):

However, this can be further extended to a function that can be used in setting up functionality within your code.

Now you can check if the required gem is installed, and print an error message.

# Using a Gemfile and Bundler

A Gemfile is the standard way to organize dependencies in your application. A basic Gemfile will look like this:

You can specify the versions of the gem you want as follows:

You can also pull gems straight from a git repo:

You can also group gems depending on what they are used for. For example:

You can specify which platform certain gems should run on if you application needs to be able to run on multiple platforms. For example:

To install all the gems from a Gemfile do:

# Bundler/inline (bundler v1.10 and later)

Sometimes you need to make a script for someone but you are not sure what he has on his machine. Is there everything that your script needs? Not to worry. Bundler has a great function called in line.

It provides a gemfile method and before the script is run it downloads and requires all the necessary gems. A little example:

Как установить gem на ruby скачанный

Use of common RubyGems commands

The gem command allows you to interact with RubyGems.

Ruby 1.9 and newer ships with RubyGems built-in but you may need to upgrade for bug fixes or new features. To upgrade RubyGems, visit the download page.

If you want to see how to require files from a gem, skip ahead to What is a gem

Finding Gems

The search command lets you find remote gems by name. You can use regular expression characters in your query:

If you see a gem you want more information on you can add the details option. You’ll want to do this with a small number of gems, though, as listing gems with details requires downloading more files:

You can also search for gems on rubygems.org such as this search for rake

Installing Gems

The install command downloads and installs the gem and any necessary dependencies then builds documentation for the installed gems.

Here the drip command depends upon the rbtree gem which has an extension. Ruby installs the dependency rbtree and builds its extension, installs the drip gem, then builds documentation for the installed gems.

You can disable documentation generation using the —no-document argument when installing gems.

Requiring code

RubyGems modifies your Ruby load path, which controls how your Ruby code is found by the require statement. When you require a gem, really you’re just placing that gem’s lib directory onto your $LOAD_PATH . Let’s try this out in irb .

By default you have just a few system directories on the load path and the Ruby standard libraries. To add the awesome_print directories to the load path, you can require one of its files:

Tip: Passing -r to irb will automatically require a library when irb is loaded.

Once you’ve required ap , RubyGems automatically places its lib directory on the $LOAD_PATH .

That’s basically it for what’s in a gem. Drop Ruby code into lib , name a Ruby file the same as your gem (for the gem “freewill” the file should be freewill.rb , see also name your gem) and it’s loadable by RubyGems.

The lib directory itself normally contains only one .rb file and a directory with the same name as the gem which contains the rest of the files.

Listing Installed Gems

The list command shows your locally installed gems:

The list includes defaults gems and bundled gems both of which were shipped with Ruby by default. In Ruby 3.1, the default gems are 70 gems in total including bigdecimal, bundler, csv, did_you_mean etc. and the bundled gems are debug, rake etc.

Uninstalling Gems

The uninstall command removes the gems you have installed.

If you uninstall a dependency of a gem RubyGems will ask you for confirmation.

Viewing Documentation

You can view the documentation for your installed gems with ri :

Fetching and Unpacking Gems

If you wish to audit a gem’s contents without installing it you can use the fetch command to download the .gem file then extract its contents with the unpack command.

You can also unpack a gem you have installed, modify a few files, then use the modified gem in place of the installed one:

The -I argument adds your unpacked rake to the ruby $LOAD_PATH which prevents RubyGems from loading the gem version (or the default version). The -S argument finds rake in the shell’s $PATH so you don’t have to type out the full path.

Further Reading

This guide only shows the basics of using the gem command. For information on what’s inside a gem and how to use one you’ve installed see the next section, What is a gem. For a complete reference of gem commands see the Command Reference.

How to include a Ruby Gem in a program or app.

When you first start programming in Ruby it can be confusing what a gem is and how to use them properly in your program. A gem is a packet of ruby code that serves a variety of different functions.

For example, the Faker gem can be used to quickly and easily generate random data. This data can be used anywhere in your Ruby program, you can have one line of code that will quickly generate random names, or numbers or quotes, or pretty much anything Faker can generate.

There are a multitude of different gems, all serving a ton of different uses. It can be confusing where to start. Gems are available for searching on the website, rubygems.org.

From there you can click on Install RubyGems and download the RubyGems folder.

Download the ZIP file and unzip it into a folder, but make sure you remember the location of the folder that you are unzipping it into.

Once the file is unzipped, go into your terminal and navigate to the unzipped file using cd ../locationoffile where locationoffile is the folder that you unzipped. Once in the correct location run the command ruby setup.rb and RubyGems will be installed.

This will allow you to install gems from the command line, as long as you know the name of the gem you will be able to install it into your ruby system.

Including A Gem Within a Ruby Program.

To install and make use of a gem, within a ruby program, the command is simple. gem install ‘NameOfGem’ I will use Faker as an example for the rest of this post. So to install the Faker gem I would run the command, gem install faker . RubyGems will then take over and install the Faker gem for your use within your Ruby environment.

But simply installing a gem is not enough to begin using it. The Faker documentation can be found on the GitHub page for the gem. If you scroll down the ReadMe page until you get to the name attribute:

you will see that you have a choice of different commands to use to generate some random names.

How to install a downloaded Ruby gem file?

Maybe I have not fully understood the question. But if you just want to install a gem that you have on your local machine, all you need to do from the console is go into the directory containing your gem and gem install —local your.gem .

Just some more clarification in case you need to build / install your own gem file in this example foo-bar.

I was researching how to do this and this post was first result 🙂

Haris Krajina's user avatar

The problem is that gem install is looking for gems to install in its default directory. You can find out where that is by running:

This will give you something like:

The GEM PATHS locations is where gem install is expecting to find gems to install. So, the solution to your problem would be to copy the gem from its current location to the expected directory, i.e.

If you then run gem install it will pick up your gem and install it. Make sure you run the copy command as superuser (sudo, if you’re running Ubuntu like me)

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *