Friday, February 5, 2016

Different bundles for different applications



Let's assume we created a Rails application named our_app using

$ rails new our_app

This application will use, by default, the global bundle, a common repository where all your gems will be installed when you use bundle install inside each of them.

If we have many applications using the global bundle, their gems will be all together. This may be good if we have strictly the same set of gems in all our applications, but this is seldom what happens. Unless, of course, if we only have clone applications. But then, what kind of programmers are we?

Using the global bundle may be a bit confusing, specially if you have applications using different Ruby versions.

The ideal situation is having one set of gems for each of our applications, and this may be easily achieved.

On the console, let's navigate to our application directory and type the following commands:

$ touch .ruby-gemset
$ touch .ruby-version

Then, using our favorite editor (Mine is emacs! Death to vi, vim and all other flavors of vi existing and still to come!) we shall open .ruby-gemset and type just a name in the first line. This name will be used, as you probably have already noticed by now, to name the bundle where this application's gems will live from now on. By default we use the name of the application, but we may use any name in fact.

After saving this first file and closing it, we shall edit the second one and write just a line again, saying something like

ruby-2.2.3

Save it and close it. Now we shall use

$ cd ..

to navigate out of our application directory. But we will be out for just a moment. As soon we leave, we'll enter the application directory again, and them we'll receive a message saying something like

ruby-2.2.3 - #gemset created /home/edvaldo/.rvm/gems/ruby-2.2.3@our_app
ruby-2.2.3 - #generating our_app wrappers - please wait

This means we are in the right path. But we haven't finished yet. We still need to install our gems with bundle install.

But when we run this command we have a surprise:

zsh: bundle: Command not found...
Install package "rubygem-bundler" to provide command "bundle"? [N/y] n

But if you think a while, this is not a surprise at all. Our gem bundle is empty! Then the gem bundler is not there yet.

Before using the command bundle we must run 

$ bin/setup

and this will install the gem bundler for us and also run bundle install for us. Now we just need to wait until the installation is over and voilĂ ! We have a separate bundle for our_app!

Repeat this process for all your applications and you'll have all them separated.