How do I ensure ruby gems are installed in right place to be executed by bundler?

白昼怎懂夜的黑 提交于 2019-12-29 08:01:11

问题


I seem to be constantly having problems with getting the gems in the right place (and not needing to use sudo when installing them.)

To address the sudo issue, I installed rbenv and then use that to install ruby 2.1.0 so that I had a separate one from Mac OSX system.

Then I installed bundler. But when I install gems from a Gemfile with bundler, I cannot find them.

Between all the steps I have taken, such as adding lines to my .bash_profile and so forth, I have the following, but really just want a clear, straightforward way to manage gems and their local execution:

gem env :

Edit: I removed everything and reset PATH -- still didn't fix anything, but you can see that gems are installed in one directory but when I call the executable, the command cannot be found:

 RubyGems Environment:
  - RUBYGEMS VERSION: 2.0.14
  - RUBY VERSION: 2.0.0 (2014-02-24 patchlevel 451) [universal.x86_64-darwin13]
  - INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.0.0
  - RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - universal-darwin-13
  - GEM PATHS:
     - /Library/Ruby/Gems/2.0.0
     - /Users/fongster/.gem/ruby/2.0.0
     - /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0

Here is what happens when I want to run, say shotgun:

bash-3.2$ shotgun /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/dependency.rb:296:into_specs': Could not find 'shotgun' (>= 0) among 12 total gem(s) (Gem::LoadError)

Can someone point me to simple, straightforward directdions for ruby gem management on OSX so that:

  • I don't need to use sudo to install gems
  • I can use bundler to install from a Gemfile for a given project
  • Those gems and their executables (shotgun, restclient, sass) can just be executed
  • My local environment based on this set up 'just works' in heroku?
  • I don't need to type bundle exec when I need to do stuff

Also some clarity on whether I need to always go to a bash shell (currently, I have been using exec bash to get my shell working).

EDIT: Following the steps below and have this response upon bundle:

Errno::EEXIST: File exists @ dir_s_mkdir - /Users/fongster/.rbenv/shims/gem
An error occurred while installing backports (3.6.3), and Bundler cannot
continue.
Make sure that `gem install backports -v '3.6.3'` succeeds before bundling.

回答1:


From a clean start

You do not have to do anything apart from the plain steps stated on the Bundler home page:

  • Do NOT use sudo bundle ...
  • Do NOT change anything to your PATH
  • Do not touch any ~/.file

  • Copy require 'bundler/setup' at the top of your first application file

  • DO use bundle exec in front of all your Bundler-managed "binaries"

Everything just works using the nice defaults for a developer workstation. When you want to go further like running multiples Rubies side by side or deploying to prod or are fed up with typing bundle exec (which you should alias to bex using alias bex='bundle exec') you can then Read The Full Manual, type bundle install --binstubs and install one of the Rubymongers (RBEnv, RVM).

In your specific situation

I would restart from scratch:

  • uninstall RBEnv (rm -rf ~/.rbenv and delete rbenv mentions in grep rbenv ~/.bashrc ~/.bash_profile ~/.zshrc /etc/profile /etc/profile.d/*)
  • uninstall Bundler



回答2:


Use rbenv-gemset. It works great and meets all your requirements. I have been using it for 2 years and have not had any problems. No issues with Heroku.

Here's my process for setting up a new Ruby project:

1. Create directory (or Rails app in new directory)
2. cd directory
3. Set ruby version: Use 2.1.0 or another version 
   rbenv local 2.1.0
4. Set gemset: replace app-name with the name of the app
   echo app-name > .rbenv-gemsets
5. Check gemset. Should be app-name global:
   rbenv gemset active
6. rbenv rehash
7. Create Gemfile
8. gem install bundler
9. bundle

When it does not work as expected, do rbenv rehash.

You should never need to install gems using sudo.

Regarding your shell, that could be causing problems. If a regular terminal doesn't work then there is probably another issue.

Since the issue seems related to your shell and PATH, here's my .bash_profile

export PATH="$HOME/.rbenv/bin:$PATH"
export PATH=/usr/local/bin:$PATH
eval "$(rbenv init -)"

You may want to try replacing your .bash_profile with the above, or create a new user and see if the issue disappears.



来源:https://stackoverflow.com/questions/26575245/how-do-i-ensure-ruby-gems-are-installed-in-right-place-to-be-executed-by-bundler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!