Gems install under vendor/bundle in rails app local testing - ruby

I have a rails app that I'm running locally where the gems are installed in the vendor/bundle directory. I want to add some debugging statements to a gem and then test it locally. I'm running bundle exec rackup config.ru to run the server. I've tried rerunning bundle install before starting the application, but that still doesn't seem to pick up my changes. Any ideas?

Running bundle show --paths will print out exactly where Bundler is loading your gems from, so you can double-check that against the files you are editing.
As a shortcut, bundle open <gemname> will open that gem's directory in your editor of choice (whatever your EDITOR environment variable is set to). You can then edit it directly there.
There is normally no need to re-run bundle install or rebuild the gem when you edit files this way.

Related

How to install additional gems with bundler in production environment?

I have a staging server. And I've got some issue I'd like to investigate right there. But I forgot to add byebug to Gemfile. I can surely add it locally, run bundle, commit to repository, deploy. But isn't there an easier way?
When I try to change Gemfile remotely and run bundle I get:
You are trying to install in deployment mode after changing
your Gemfile. Run `bundle install` elsewhere and add the
updated Gemfile.lock to version control.
If this is a development machine, remove the /home/gccs/website-backend/releases/20161018143057/Gemfile freeze
by running `bundle install --no-deployment`.
You have added to the Gemfile:
* byebug
Gems are installed with capistrano, basically, like so:
bundle install --path /home/user/app/shared/bundle --without development test --deployment --quiet
Edit .bundle/config changing BUNDLE_FROZEN: '1' to '0' (or removing it) is enough in order to be allowed by Bundler to manage gems in a deployment environment. Then you can edit the Gemfile, run bundle, restart your application and the custom gems are ready to be used.
If you intend to use them outside of the application runtime (f.e. if you need pry in rails console) restarting the server is not needed.
The reason why you're unable to install additional gems is because the bundle is frozen. You can check it like so:
$ grep FROZEN .bundle/config
BUNDLE_FROZEN: '1'
If you remove the line (as suggested by mdesantis) or change "1" to "0", you'll be able to install whatever gems you like, as if it were developer machine. But generally it's best to restore the value, if no more needed. Not sure if bundler will do that automatically on next deploy.

How to install Serverspec in windows machine where internet is not there?

I was using serverspec in a VM where internet is available and it was so nice.
But when we need to give the script to testers they have to install it in a machine where internet is not available.
I was trying to install in a local folder in a VM where internet available then installed in test VM. But when i run Serverspec-init it says rspec is not found.
Seems some dependent gems also need to be installed before using it.
Is it not possible to install the whole bundle as one step? How to do that?
Managing gems on a server that doesn't have internet access is definitely a challenge.
One relatively-straightforward way to do what you need is to make use of the vendor/cache directory in your application and make bundle know about it when using bundle install by using the --local flag.
First, download the gem archive (.gem file extension) of the bundler gem by going to its Rubygems page and clicking the "download" link in the bottom right. You'll need to upload that file to your test server and run $ gem install bundler-1.12.15.gem from the command line.
Now you need to get the .gem archives for serverspec, its dependencies, and all of its dependencies' dependencies, and put them inside your application in the vendor/cache directory (create those with $ mkdir -p vendor/cache) if they don't exist.
Now when you deploy the application to the server, with these .gem files in vendor/cache, run bundle install --local. This will install the gems. You can see the official documentation of the --local option at the bundler docs.

How do I properly install and run Thin?

How does one install and run Thin? According to the official page, I can start it with bundle exec thin start, but no dice. Instead I get Could not locate Gemfile. If I restart Terminal and run gem install thin again, it seems to install it again as if it weren't properly installed the first time.
The guide assumes you're using it in a Ruby project. Using bundle ... requires there to be a Gemfile in the current directory. It'll work if you cd to a Ruby project and add gem thin to your Gemfile.
Here's some info on Gemfiles: http://bundler.io/v1.3/gemfile.html

Deploying a Ruby Command Line Application that Depends on Non-Rubygems-Hosted Gems

I am attempting to deploy a small command-line application written in Ruby. Many of the gems that the application depends on are hosted on my github account. I have specified their locations in the Gemfile appropriately. When I clone the repo on the deployment machine and run bundle install from the root, all goes smoothly. I can then run the command line app (named "hippo") with bin/hippo. I would like to install the app to the system so that I do not need to patch $PATH or specify the path to the executable in order to use it.
When I run gem install /path/to/my/.gem/file, installation fails and says that it cannot resolve the dependencies to my personal gems hosted on my Github account. I gather this is because the gem install command does not read the Gemfile, only the gemspec (why on earth did the bundler people name their file Gemfile instead of BundleFile or something?), and the locations of the gems are specified in the Gemfile (there is no way to point to a github gem in a gemspec, right?). I have the same issue when I use the rake install task that comes with a bundle-scaffolded gem. However, when I run bundle exec gem install /path/to/my/gemfile then it installs OK, I guess because it sees my gems on the bundler-altered load path and decides that they are already installed. But then, when I try to run the executable, it fails because it can't find the dependencies when they are required. (Note that the executable has require bundler/setup as its first line after the shebang).
So I ask: what is a good way to deploy, with bundler, a command line application that depends on non-rubygems-hosted gems?
bundle exec sets up your load path correctly so that the gems are visible when you require bundler/setup. One option would be to always run your binary with bundle exec bin/hippo.
Something more suited to your purposes might be to run bundle install --standalone, which generates a bundle/bundler/setup.rb file within your project's directory. That file sets up load paths correctly for you, so you'd just need to do something like require_relative '../bundle/bundler/setup' from your binary instead of require 'bundler/setup'.

Using Bundler in deployment

Pretty fundamental question but I'm trying to understand how best to use Bundler in a deployment situation.
I'm working on a Sinatra application that has about 20 dependent gems. During development, I'm using RVM with a custom gemset for the application, and I run bundle install to update the gemset in accordance with the gemfile.
When it comes to deployment (manually for now, so I can understand how it all works before using a tool like capistrano), I need to do bundle install --development right? This downloads the gems and places them in vendor/bundle.
My question is what else do I need to do? I'm using Unicorn on the server - do I just bundle exec unicorn ... and everything just works? (i.e. bundler finds the vendor directory and uses the gems from there?)
Should unicorn be a vendored gem in the application or a separate 'system' gem on the server that all applications share?
You need --deployment key, not --development: http://gembundler.com/man/bundle-install.1.html#DEPLOYMENT-MODE
On first run bundler creates config in .bundle directory. You can check it by running bundle config or just cat .bundle/config in project's directory. So bundle exec unicorn is enough since bundler knows where gems are installed. On development machine you can also install gems to arbitrary directory using --path key. For more details see manpage for bundle install (link above or bundle help install).

Resources