Ruby Workflow - Bundle Install - ruby-on-rails

Where does bundle install fit into the add-commit-push workflow? In other words, when is it necessary.

You will need to launch bundle install every time that you update your Gemfile.
Before you run it, make sure you have bundler gem installed:
$ gem install bundler
You can integrate this command and several others adding some git hooks. For example, I use overcommit gem to make sure that some actions are performed before commit, merge and push.

You would want to ensure that you have run bundle install before adding files to a git commit.
You should be in the habit of running bundle install every time your Gemfile is changed.
Please note: Git and Bundler are independent. The only reason you want to have run bundle install before adding to git is to ensure that your Gemfile.lock is updated to contain the latest information.

Related

Bundle optimization for ruby on Docker

I'm new to docker and was wondering how does ruby bundler behave when re-building docker images? The first time I build an image it takes forever to bundle install. What will happen when I add another gem into the Gemfile? Is there a way to ensure previous gems are cached somehow in the image and used for this new bundling?
Dockerfile:
FROM rails:onbuild
RUN apt-get install -y imagemagick
It depends on how you ADD your Gemfile.
ADD Gemfile /var/www/yourapp/
ADD Gemfile.lock /var/www/yourapp/
RUN bundle install
In this case bundle install would only run if either the Gemfile or the Gemfile.lock changed. Note that the placement of this instruction in your Dockerfile matters. As soon as a previous build-steps cache is invalidated all subsequent instructions are no longer cached. (e.g. you ADD a config file before the Gemfile, and that changed -> bundle install will run).
What will happen when I add another gem into the Gemfile?
If you just want to add a couple of gems without bundling everything you could also do something like:
ADD Gemfile /var/www/yourapp/
ADD Gemfile.lock /var/www/yourapp/
RUN bundle install
...
ADD Gemfile.tip /var/www/yourapp/
RUN bundle install
Here is a rails example project you can check out (well documented).

Can't run bundle update on Windows

Whenever I run bundle update or bundle install on Windows 8.1 I can't update/install gems from github. I can install other gems like uglifier, but it doesn't work for github gems specifically. For example, putting this in the Gemfile
group :development, :test do
gem 'rspec-rails', '2.13.1'
gem 'spork-rails', github: 'sporkrb/spork-rails'
end
results in the error:
Retrying source fetch due to error (2/3): You need to install git to be able to use gems from git repositories.
The problem is I definitely have git installed. I was running this from Git Bash and working in a project that I was cloning, pulling, and working with off of Git. So why does the bundle update/install keep insisting that I don't have git installed? How do I fix it and make bundle install work?
Are you running a pre-release version of Bundler? This commit might be related to your problem; it looks like earlier versions of Bundler scan your %PATH% for "git", but not "git.exe".
The easiest solution would be to backport bundler to 1.3.5:
gem uninstall bundler
gem install bundler
Don't forget to select radio "Use git from Windows Command Prompt" while installing the Git. Thats the key!
By default "Use git from Git Bash only" is set.
For me it was also the path, with space and accent, I guess that was the accent the issue. installed the bundler 1.6-pre, uninstalled git, reinstalled it in C:\Git, changed the path of git in my IDE (RubyMine) and finally installed every gems using bundle install.
Thanks a lot!
While on Windows, if you're still getting that error after installing git too, make sure to close the 'CMD' instance and open it again, the system paths vars have been set, but not instantiated in the active 'CMD' window.

install gem from command line vs changing gemfile

I'm trying to understand what exactly happen when I'm installing gems.
At first I thought that using the "gem install gem_name" command (and after it the "bundle install) will make sure I have the proper files and dependencies and then it will update the gemfile.
But I've noticed that whenever I add gem using the commend line it doesn't appears at the gemfile yet I'm still able to use its functionality.
so is there is any reason to use the comment "gem install gem_name" insted of just adding the gem name to the gemfile?
The reason to use a Gemfile is that you can install all required gems in a single go.
Imagine that you work in a development team and a new member starts contributing to your application.
Al he has to do is checkout the repository and run bundle install.
Only use the command gem install if you just want to install a gem that is not nessecarily relevant to your project.
If the project requires the gem; put it in the Gemfile.

Why is "install" run twice?

I'm going through Micharl Hartl's well known Rails tutorial, and this piece is confusing me. Every time a new app is set up, these commands are run:
$ bundle install --without production
$ bundle update
$ bundle install
I don't really get why install is being run twice. What is the effect of these three commands run in this sequence?
You should not have to run bundle install twice as bundle update will also install all of your gems (as well as updating them to their most current version). I have not read the tutorial you mentioned but perhaps the purpose of the second install is to install all of the gems, including those reserved for production.
Your second question, what is the effect of these three commands:
bundle install --without production
Inspect the gemfile, ignoring gems that are reserved for production
Resolve all dependencies
Install all gems and dependent gems
Save the exact version of each gem to Gemfile.lock
bundle update
Inspect the gemfile
Resolve all dependencies from scratch using the newest version of each gem and completely ignoring Gemfile.lock
Install all gems and dependent gems
Save the exact version of each gem to Gemfile.lock
bundle install
Because this is the first run of the production gems, inspect the gemfile and resolve dependencies of the production gems
Use Gemfile.lock for exact versions of all other gems to be installed
Install all gems and dependent gems
Save the exact version of each gem to Gemfile.lock
Hoped this helped, for more detailed info about the two commands check out this and this.
$ bundle install --without production prevents bundler from installing any of the production gems. It also gets saved in your local repository and you don't have to run it more than once. Any subsequent run of bundle install will include --without production.
bundle install installs only the missing gems from your Gemfile, while bundle update updates/installs every single gem to the latest version as specified in the GemFile..

How do I use bundler to get a gem from a git repository?

Authlogic has a couple unfortunate deprecation warnings that are fixed in a fork.
How do I use this forked version? I tried adding the following to my Gemfile:
gem 'authlogic', :git => 'git://github.com/railsware/authlogic.git'
And it didn't work quite that well. I started getting:
git://github.com/railsware/authlogic.git (at master) is not checked out. Please run bundle install
And
The git source git://github.com/railsware/authlogic.git is not yet checked out. Please run bundle install before trying to start your application
Assistance will be rewarded with virtual cookies.
Have you tried running bundle install after removing Gemfile.lock?
rm Gemfile.lock
bundle install
Your Gemfile configuration should work, I was able to use the same for and my bundle install command executes as does my bundle list command.
My other suggestion would be removing you ~/.bundler and .bundle directories and checking that you have properly configured git.
You need to run bundle install from the terminal after updating your Gemfile.

Resources