How to avoid entering password for new Rails app? - ruby-on-rails

After I run rails new myapp, it downloads gems and then at run bundle install, I always get prompted to enter in my system password.
Is there a way to set the password so I don't have to keep entering it in?

Background info
Why do you have to enter the password in the first place? Because by default bundler will install gems in a system wide location, such as /usr/local/lib/ruby/gems/2.2.0/gems/.... These gems can be used by every user on your pc.
Because it is a shared location, it is usually only writeable by root and therefore you have to enter your password to gain root privileges.
Where are your gems stored? Find out by using bundle show <gem> for a specific gem, for example rake, which in my case says:
bundle show rake
/usr/local/lib/ruby/gems/2.2.0/gems/rake-10.4.2
Solution
a) Change owner of directory
Either make this directory writeable for you. But this is no good practise, because system folders should not be writeable for a normal user. In case you are the only user it still might be OK - then you can simply change the owner of this directory to be you:
sudo chown -R `whoami` /usr/local/lib/ruby/gems/
Now you are the owner and can change files inside without the need of any password.
b) Install gems locally inside project dir
Or you can install all gems locally inside your project dir using the --path option of bundler. It is a convention that those gems are normally installed in vendor/bundle, so you use this command (you can use any path you want):
bundle install --path vendor/bundle
(When you do bundle update next time, you can omit the --path option, because it is saved in the .bundle folder of your project.)
You should add the vendor/bundle folder to your .gitignore (or whatever vcs you are using) to not have it checked in.
Since you write inside a directory you own, you do not have to enter a password. The downside of this is that gems will be duplicated in every project folder if you have multiple projects using the same gems. For a multi user system this is the correct way. But if you are the only one using the machine, variant a) definitely helps to save space and install time.
I'm using variant a) on my own development machine and variant b) on the deployment machine.

It seems like a permissions issue. Try chmod a+w /path/to/bundler

Related

Rails Bundle Options for Getting Gems back into your Local Application

I ran bundle --without development on my machine and now I want to include the dependencies in my development group in my local rails app.
I ran bundle thinking it would including everything again including development, but I receive this message Gems in the groups development were not installed still. Can you tell me the option I need to pass into the bundle command to get the development gems back into my application? I tried running bundle --include development with no luck.
Can you also tell me where you found the option (if thats the solution)? I can't seem to locate a list of bundle options.
If you run
bundle help install
then both the with and without options are documented. Note that without is listed as being a "remembered option" i.e. it is persisted for subsequent calls.
Remembered options are stored in .bundle/config at the root of the project. You can either edit this file directly or view them using
bundle config
Which will also take into account user level configuration or environment variables that affect bundler
I was surprised to confirm this behavior. It seems like the without --development flag is saved, so even if you rm Gemfile.lock, running bundle again will not install the development group. bundle update also doesn't work.
You were close, though, with bundle --include development.
What works is bundle --with development. You don't need to delete your Gemfile.lock before doing this.

Does 'bundle install' install all the required gems on my computer permanently?

I am new to rails and am learning about bundler. I understand that bundle install installs all the gems specified in the gemfile but where does it install them to?
Does it install them on my computer permanently so that they can be used by any future project?
If so doesnt this mean my computer gets filled with random gem versions and gem installs that I needed for one example project but may never use again?
By default, bundle install is going to install at the root level so all users on a computer could have access to the gems. So 'yes' it is permanent (at least not tied to your application, you can remove them whenever you want).
Take a look at the man pages for bundler. In here, you'll notice that you can specify to install to a local directory.
Install your dependencies, even gems that are already installed to your system gems, to a location other than your system's gem
repository. In this case, install them to vendor/bundle.
$ bundle install --path vendor/bundle
Further bundle commands or calls to Bundler.setup or Bundler.require
will remember this location.
This will let you install the gems to a location inside your application. So when you delete the example app, you also delete the associated gems.
Also, if you would like to see where a specific gem is installed (say you want to look at its source code), type bundle show <gemname>. This will spit out the path to that gem.
The short answer is 'yes'. The longer answer is that there are some technologies which will reduce or eliminate the problems associated with this effect.
If you install 'RVM':
https://rvm.io/
this will allow you to install multiple versions of Ruby and create individual 'gemsets'. As you enter the directory that contains your project, the ruby version and gemset settings are automatically picked up and the active Ruby version will change. This way you can keep gems separate between projects - and use several Ruby versions at once, including JRuby and other esoteric versions.
To find out where gems are stored, type:
gem environment
into your command line and look for the INSTALLATION_DIRECTORY entry in the response.

Stop asking for password when installing gems

Whenever I bundle my rails 3.2 gems, it asks me for my password:
Enter your password to install the bundled RubyGems to your system
This gets really annoying, especially when bundling several times in one project. However, when I set the gem directory to world-readable, it always gives me a warning when executing any (!) rails command. This is even more annoying, of course.
How can I turn this off?
# install gem with the specified path
bundle install --path vendor/bundle
Absolutely has to do with the system ruby, not RVM, unless you installed RVM using the Multi-User installation type. If you did that and its still asking you for your password, then you installed as root, strictly against what the listed documentation states, and your general user was not added to the 'rvm' group the installer creates. (NOTE: This is based on the idea that you want a multi-user install, not a single user one. If you want the single user install, not not prefix with sudo when you run the installer.)
Rip out RVM, log out then back in (to ensure a completely fresh reinitialization of the environment), and then rerun the installer command as your regular user, not as root, prefixing with 'sudo' as the documentation instructs.
If you do not have RVM installed, then follow the documentation at https://rvm.io to install either as a single user install, or as a multi-user install. In this case, without RVM installed, what Billy Chan described above is your fix, though I would suggest tightening the rules a bit by figuring out which exact set of commands (gem bin names) you need to run on a regular basis and adding entries for those in the sudoers file (visudo).
Right now the problem reads you trying to use the system ruby which *RVM does NOT control (it simply allows you access to it by setting the proper GEM_PATH, RUBY_* environment variables etc), or your RVM multi-user install was done incorrectly.
This depends on whether you want to install gems under system or under your user. If under your current user, you can simply turn this message and asking off by adjusting bundler configuration.
In your project in file .bundle/config add line
BUNDLE_DISABLE_SHARED_GEMS: '1'
So whole config file can look like
---
BUNDLE_WITHOUT: development:test
BUNDLE_DISABLE_SHARED_GEMS: '1'
BUNDLE_PATH: /home/youruser/gems/
I had this problem with my rails installation. A quick workaround for this is to skip bundle install during project creation like so:
rails new webapp -B
And then you can do:
cd webapp/
mkdir -p vendor/bundle
bundle install --path vendor/bundle
Hope that helps future queries.
The best method: Use RVM. With RVM, you can just run gem <any command> without adding sudo
If you don't want to use RVM, you can still add sudo before the commands. And you can set to prevent sudo to asking password for all commands by:
$ sudo EDITOR=vim visudo
# or any editor in your system
Then, edit the doc by adding following line
username ALL=(ALL) NOPASSWD: ALL
# Where username must be replaced by your real username in system
I know it may not be that safe, but it's convenient to use on my own machine.
You can use the above two methods all together like me.

How to use multiple bundle paths in Ruby on Rails (bundler)

I'm using a few gems for interactivity with my database and to provide easy pagination. After deploying my application on my server, it'll automatically execute bundle install to install those gems if they aren't installed yet. This is helpful when I deploy a codebase update including a new gem.
However, there's one gem (will_paginate) I've made a little change to (so that it always shows on what page you are, no matter the total number of pages).
Therefore, I used bundle install --local --path vendor at my local machine (OS X Lion) so that I can easily edit them (at per-app base, instead of system-wide).
Because of dependency problems, I cannot 'just' copy the vendor-folder to my web server. Therefore, I decided to add another rule to my .gitignore-file:
vendor
Because this caused my customized will_paginate-gem not to get uploaded, I executed another command:
git add -f vendor/ruby/1.8/gems/will_paginate
Now, when I push, the only gem in the vendor folder at my web server is will_paginate, which is great.
However, it doesn't get loaded, since it isn't in the bundle path.
I already tried to create a file called .bundle/config and add the following in it:
---
BUNDLE_PATH: vendor
I also tried adding BUNDLE_DISABLE_SHARED_GEMS: "0", but using this, bundle check says it's missing the shared gems.
My question is, how can I add a bundle path vendor, and fallback on the system-wide gem-path.

Rails 3's "bundle install" and "bundle install --deployment" both work well except the 2nd one just uses more disk space?

It seems that on development machines (like on the Macbook), if we use bundle install --deployment, all the gems will be installed into the vendor/bundle folder and it just use more disk space if we have multiple Rails 3 projects (some project just for testing Rails 3). If it is not --deployment, then the gems will be in the "generic" folder instead of inside the project folder, and so can be shared across projects. Is this true?
Another thing is, do we need to add all files under vendor/bundle to our repository and push it? It seems that if we do it, we just jam up the repo, because if we don't, all the appropriate gems will be installed by bundle install using all the gems specified in Gemfile.lock anyway. (The Gemfile.lock is a small file in the repo). Is this true too?
Yes! True.
When you use the --deployment flag, Bundler makes sure that every gem you need is vendored i.e. they are copied to a predetermined place your application's folder structure (which happens to be vendor/bundle in Rails by convention) This is good for two things.
First if you have limited permissions that prevent you from installing gems in your deployment machine, then let you have all the gems needed within your app.
Second, if you want to hack away at the actual code in the gems, you can do so on your vendored copies without affecting the system gems. The changes you make will only affect the application you're working on.
This vendoring approach used to have another use, that is making sure that you are using the specific version of a gem, and your application would keep working even if the system gems were upgraded to a higher version that would break your app. However, Bundler itself made this use case mostly obsolete, as it automated the installation and referencing of specific versions of gems.
And yes, vendoring would bloat your app's code. Gemfile.lock is just a list of the required gems. If you vendor your gems, they get copied into your app with all their might.
So, I recommend you don't vendor your gems (that also means don't use the --deployment flag) unless you have one of the reasons above.
I think vendor/bundle, will not affect repo as long as the repo allows you to ignore files.
One can ignore it (adding the path to .gitignore if you are using git) and on server have a symlink to share the gems with multiple revisions.

Resources