Installing Gems per Project Directory
When Bundler complains about “not being allowed to install to the system RubyGems” we have two solutions:
-
Configure Bundler to install gems in the user directory, where we have write permissions; or
-
Configure Bundler to install gems in the project directory, just like NPM installs modules locally.
Let’s explore solution 2 this time.
Inside a project directory, we can check if gems are being installed locally:
$ bundle config path
Settings for `path` in order of priority. The top value will be used
You have not configured a value for `path`
Here we can see that no path is configured. This means gems are installed in the default directory, according to $BUNDLE_PATH and $GEM_HOME.
Let’s configure this path then:
$ bundle config path 'vendor/bundle' --local
This creates a .bundle folder with a single file named config inside.
Its contents are the following:
---
BUNDLE_PATH: "vendor/bundle"
Now, when the run:
$ bundle config path
Settings for `path` in order of priority. The top value will be used
Set for your local app (~/Projects/guilhermesimoes.github.io/.bundle/config): "vendor/bundle"
We can see that a path is configured. This makes it so that bundle install installs gems in the vendor/bundle folder.
Let’s try it:
$ bundle install
...
Fetching jekyll 3.7.0
Installing jekyll 3.7.0
...
Bundle complete! 4 Gemfile dependencies, 28 gems now installed.
Bundled gems are installed into `./vendor/bundle`
That’s it! Done!
Points worthy of note:
-
I recommend adding whatever you configure as your
path(in this casevendor/bundle) to your.gitignoreso that your gems are untracked by Git. -
bundle installcan also receive apathflag but this is not remembered in subsequent commands. -
bundle installcan also receive adeploymentflag which does a bunch of funky things including changing where the gems are installed.