Store and install ruby gems locally for ruby project - ruby

I am currently working on sample ruby project (not rails).
In the rails project we can use bundle package command for storing installed ruby gems.
It will lock and cache gems from RubyGems into ./vendor/cache. folder.
Now I have to use same functionality. So how can I store gems on local machine and when we do bundle install it will fetch the required ruby gems from that source.

bundler is not part of rails, but it is an independent ruby gem itself. so given you have the bundle command available, you can just set up you Gemfile and use bundle install as you are used to.
This is also described on the bundler homepage

Use Bundler
install bundler from your server prompt (regardless of project folder) to ensure the bundle command is accessible.
gem install bundler
now that your server has bundler installed make a file under the root of your project called Gemfile and add the source and required gems something similar to:
source 'https://rubygems.org'
gem 'example_gem'
gem 'example_gem_with_version', ">=0.9.2"
...
Now your Gemfile is ready. From the root of your project run the bundle install command and specify your vendor directory
bundle install --path vendor
After it retrieves the source it will cache it under the vendor directory. To install locally without fetching from rubygems.org simply use
bundle install --local

Related

How to install a ruby gem from a git repository(using bundler) in the default gems folder rather than in a separate folder called bundler?

When I clone the repo of the gem and try installing using gem install <gem-name>, it gets installed to the default gems directory. But, when I try installing using bundler, it gets installed in a separate folder called 'bundler' which is not considered by GEM_PATH, as a result it is not usable in irb.
I want to install that gem like the other gems using bundler, but it should install in default gem folder, like the other gems. Any solution to this?
There's no direct way to install a gem(from git source) to the default location as bundler differentiates rubyorg gems and git source gems separately.
So I have managed to install that specific gem from Github using a third party gem called 'specific_install'. It fetches the source from the github repo itself and installs the gem.
To install specific_install:
gem install specific_install
gem 'specific_install' (in Gemfile)
Syntax:
gem specific_install <git repo SSH or HTTPS>

Bundler: install gems from git to gems folder instead of bundler folder

The inside of your "Gem Path" (see: $ bundle env) contains a gems and a bundler folder. The gems folder is populated by gems installed e.g. via gem install or the gem tag of a Gemfile. The bundler folder, on the other hand, holds for example gems installed via the git feature of a Gemfile:
gem 'my-gem', git: "https://github.com/x/y.git"
I assume this is so that custom installations don't conflict with installations from a gem server. Nevertheless, I'd like to treat a specific gem installed from git as if it's coming from a gem server.
Is it possible in this case to tell bundler to use the gems folder instead?
My current workaround is to clone the repo and then build and install the gem locally.
I've also looked at Gem::SpecificInstall which might solve this issue (although not through bundler) but it doesn't support authentication for private repos which is a dealbreaker for me.

Ruby Bundle Install in deployment mode

I'm installing a ruby git-repo on several servers which have no internet connectivity. I had cloned that repo to my local machine, and used $ bundle install --path vendor/bundle to package all dependencies. Then I copied the whole directory to the target server, and tried $ bundle install --deployment, which still attempting resources from rubygems.org, hence not solving any dependencies as expected:
Fetching source index for https://ruby.taobao.org/
Could not find ffi-1.9.3 in any of the sources
According to bundler.io:
The --deployment flag activates a number of deployment-friendly conventions:
Isolate all gems into vendor/bundle
which does not work in my case. Any thing am I doing wrong?
bundle install will grab the gems from rubygems when run.
bundle package grabs the gems and packages them in vendor/cache.
The package command will copy the .gem files for your gems in the bundle into ./vendor/cache. Afterward, when you run bundle install, Bundler will use the gems in the cache in preference to the ones on rubygems.org.
http://bundler.io/bundle_package.html

Bundler install custom path issue

gem env shows
GEM PATHS:
/usr/local/share/gems
I would like to use bundle install --deployment --path=/usr/local/share/gems to install my bundled gems.
The problem is that the bundle install creates a folder ruby and puts the gems folder inside the ruby folder.
When this happens my ruby code is unable to find the gems in its default path.
Looks like I am missing some configuration parameter. Help please.
Maurício Linhares comments in the question has resolved the issue.
When using bundler to install gems use bundle exec ruby. When the bundle install --deployment happens the path information goes into .bundle/config
bundle exec ruby path-to-ruby-script.rb
The above execution Will find the gems installed by the bundle command.
Alternatively, if you want to force installation to the system gem location, I believe bundle install --deployment --system will do what you want.

How to make gem install command only install when not installed or update needed

i've read the documentation from rubygems site, but i guess the "gem install" command always reinstall, recompile everything, even if the same version already installed..
how to make gem install command only install when needed?
It looks like the --conservative flag will make the gem command do what you want.
gem install rake --conservative
From the documentation gem install --help:
--conservative Don't attempt to upgrade gems already meeting version requirement
You may want to use something external like gembundler to handle project’s gem installation.
If you must use rubygems directly for this, a command like
ruby -e "puts `gem install GEMTOINSTALL` if(`gem list --no-versions | grep GEMTOINSTALL`) == ''"
would do the job.
That is why we use bundle install. This command will look into the gemfile for the gems.
This Gemfile says a few things. First, it says that bundler should look for gems declared in the Gemfile at http://rubygems.org. You can declare multiple Rubygems sources, and bundler will look for gems in the order you declared the sources.
Bundler will connect to rubygems.org (and any other sources that you declared), and find a list of all of the required gems that meet the requirements you specified. Because all of the gems in your Gemfile have dependencies of their own (and some of those have their own dependencies), running bundle install on the Gemfile will install quite a few gems.
If any of the needed gems are already installed, Bundler will use them. After installing any needed gems to your system, bundler writes a snapshot of all of the gems and versions that it installed to Gemfile.lock.
When you run bundle install, bundler will (by default), install your gems to your system repository of gems. This means that they will show up in gem list. Additionally, if you are developing a number of applications, you will not need to download and install gems in common for each application. This is nice for development, but somewhat problematic for deployment.
In a deployment scenario, the Unix user you deploy with may not have access to install gems to a system location. Even if the user does (or you use sudo), the user that boots the application may not have access to them.
As a result, bundler comes with a --deployment flag that encapsulates the best practices for using bundler in a deployment environment.
The --deployment flag requires an up-to-date Gemfile.lock to ensure that the testing you have done (in development and staging) actually reflects the code you put into production. You can run bundle check before deploying your application to make sure that your Gemfile.lock is up-to-date. Note that it will always be up-to-date if you have run bundle install, successfully booted your application (or run your tests) since the last time you changed your Gemfile.

Resources