Install a gem from a downloaded tar or zip - ruby-on-rails

First let me say I cannot do gem install, I don't know why. Probably because I live in China and the firewall random things.
So I have to locally install gems. For example, i want to install this gem riddle. But this gem downloads as a tar or zip and when i open it it is a folder not a .gem file.
So what to do?

You can do gem build whatever.gemspec inside of the directory that you untar/unzip -- that will produce a .gem file, then do gem install whatever.gem.
You need to be at the directory where you unzip the gem file for example
C:\railsinstaller\ruby2.2.0\lib\ruby\gems\2.2.0\gems> gem install rails-5.0.0.1.gem
and that's it - you are done downloading and installing Rails.

To avoid the gem build step, and to always run the actual code, bundler can install from a local path:
gem 'pry', path: './pry'
in a Gemfile.
... where ./pry would be the clone of your repository.
Simply run bundle install once, and any changes in the gem sources you make are immediately reflected. With gem build pry / gem install pry/pry.gem, the sources are still moved into GEM_PATH and you'll always have to run both gem build pry and gem update again if you make changes.

Related

When installing a gem in a local project directory, how do I figure out what the proper gem file path should be?

I'm using Ruby 2.6 and Rail 5. I want to install the following gem
$ gem install ffi -v '1.9.18' -- --with-cflags="-Wno-error=implicit-function-declaration"
Fetching ffi-1.9.18.gem
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.
but the above attempts to install it in a global location, and I would prefer to have this in my project directory, which I configured using
bundle config set --local path 'vendor/bundle'
Per the documentation, I can add this to my "gem install" command
--local path_to_gem/filename.gem
but my question is what would "path_to_gem/filename.gem" need to be to properly install this ffi gem, or can I just make something up?
Look into using RVM and taking advantage of its gemset feature. Rails projects are not like Node projects where you would have a node_modules directory where all of your dependencies live. Instead, RVM would manage your dependencies locally and keep them separate using gemsets.

Install Ruby Gem + dependencies on specific folder and run that installation

I am currently attempting to install on a specific directory a set of gem files and run that installation (trying to avoid installing to default Ruby locations).
e.g. I have a folder with gems I plan to install locally, thus I run the following command:
gem install --force --install-dir .\install_3 --conservative --bindir .\install_3\bin --no-document --local *.gem
This then installs all gems available in that folder in my install_3 directory.
However, I am having trouble running the gems I installed in that directory.
My question is, how can I install Ruby gems in a specific folder and make sure I run the gem and its dependencies from my specified folder in --install-dir option?
When I try to run with the following script:
#setlocal
#set DIRNAME=%~dp0
cd %DIRNAME%
#set PATH=%PATH%;%DIRNAME%install_3\bin
#set TEMP=C:\TEMP
fluentd -c fluentd.conf
I get the following error:
Traceback (most recent call last):
2: from C:/Users/yytcadm/Desktop/fluentd_1.8.0_gems/install_3/bin/fluentd:23:in `<main>'
1: from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems.rb:303:in `activate_bin_path'
C:/Ruby25-x64/lib/ruby/2.5.0/rubygems.rb:284:in `find_spec_for_exe': can't find gem fluentd (>= 0.a) with executable fluentd (Gem::GemNotFoundException)
Instead of fetching gems from my specified installation folder, it attempts to search for them in the default ruby gem folder.
Edit: It seems to throw an error on this line from my fluentd file:
load Gem.activate_bin_path('fluentd', 'fluentd', version)
I'm guessing I need to modify this to the custom path decided on my --install-dir option but my attempts have not been fruitful so far.
When you require a gem, Rubygems searchs for it inside the default gems folder. You can prevent Rubygems to do that by adding the paths of your gems to the $LOAD_PATH variable.
For example, if you have one gem at /home/user/custom_dir/my_gem, you can do something like this before requiring the gem:
$LOAD_PATH << "/home/user/custom_dir/my_gem/lib"
Another option is to use require_relative instead of require. If you app root is /home/user/my_app and inside that dir you have the gems like /home/user/my_app/gems, you can do somthing like:
require_relative 'gems/my_gem'
Check this talk https://www.youtube.com/watch?v=I0a5zv7uBHw it's really enlightening on how to require things.
Providing answer to those that might need it:
In order to correctly automate the installation of a specific gem + their dependencies:
bundle install
bundle package --all-platforms --all
This will fetch all dependent gems and place them on vendor/cache
If you install your gem + dependencies with the following command:
gem install --no-user-install --install-dir Install_Location --conservative --bindir Install_Location\bin --no-document --local YOUR_GEM_TO_INSTALL
You can then run your gem commands by setting the following:
Add Install_Location/bin to PATH
Append Install_Location to GEM_HOME
Append Install_Location to GEM_PATH
run the gem commands needed
This allowed me to have run gem commands without these appearing/affecting gems available in gem list.

Update local gem source code

I'm testing a gem in a Rails project.
The current Gemfile:
gem 'mygemname', path: '/path/to/my/gem'
When I edit a gem locally I can build the gem, remove the gem from Gemfile, run bundle install, add the gem back to the Gemfile and run bundle install again. Is there an easier way to do this locally?
If you use bundle config local.GEM_NAME /path/to/local/git/repository from the command line then every time you reload your application it will load the latest source from your file system.
To remove the changes (when you have pushed your code to GitHub or RubyGems), you need to run bundle config --delete local.GEM_NAME
Source: http://bundler.io/v1.10/git.html
You can bump your Gem's version. Next time you run bundle your gem will be updated to the next iteration.
VERSION = "1.0.0"
Bump the patch version to
VERSION = "1.0.1"
Now just run bundle. Bundler will notice it and log 1.0.1 (was 1.0.0) in the output.

Install gems via ftp

Can you install your ruby gems via ftp? I mean just copy your local gem directory /var/lib/gems/1.9.1/gems and put it online with filezilla in the ruby>gems>gems directory.
The reason I want to do this is because with cPanel it gives me errors when trying to install some gems (like permission errors, some require ruby >=1.9.2 but I already have ruby 1.9.3). So is there a simple way?
Thanks!
Instead of copying your system's gem which may be ruby version specific, you could place all the required gems sources in your application's lib directory and reference them in your Gemfile. Not that you cannot place these sources in other directories.
Place gem source in your local application e.g. #{Rails.root}/lib/my_gem and update your Gemfile to reference the gem using:
gem "my_gem", path: "lib/my_gem"
Then run bundle install to install the sourced gem in your application.
and you can run
gem server
then add the source
http://some.ip:8808
there you'll have shared the gem installed in that system

What is the most easy way to install gem?

I need to uninstall a gem "rubyzip 1.1.0" and install the older version "rubyzip 0.9.9"
I need to enter in a specific folder in ssh to do this?
I need upload some file to sftp or I can do a command with some url?
If there is a very simple way even I am very grateful.
Take a look at your Gemfile and look for the line gem 'rubyzip'.
Change this line to: gem 'rubyzip', '0.9.9'
Now run bundle install from your application root directory to fetch and install the downgraded gem (and all of its dependencies).
Done.

Resources