How to set a gem binary in path? - ruby

I have a gem I have built with an executable. The executable is under the bin directory of my repo and it is defined in the gemspec file:
spec.executables << 'my_gem'
After installing the gem the executable is also installed correctly:
$ ls /Users/myuser/.gems/gems/my_gem-1.0.0/
bin/ lib/
$ ls /Users/myuser/.gems/gems/my_gem-1.0.0/bin/
my_gem*
However I want to add this executable in the path so I can run something along the lines of my_gem or bundle exec my_gem to run the executable from the cli.
What do I need to add to my gemspec file to do this?
Here is my ruby version and ruby gems versions:
$ ruby -v
ruby 2.1.1p76
$ gem -v
2.2.2
Here is my gem env:
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.2.2
- RUBY VERSION: 2.1.1 (2014-02-24 patchlevel 76) [x86_64-darwin15.0]
- INSTALLATION DIRECTORY: /Users/myuser/.gems
- RUBY EXECUTABLE: /Users/myuser/.rbenv/versions/2.1.1/bin/ruby
- EXECUTABLE DIRECTORY: /Users/myuser/.gems/bin
- SPEC CACHE DIRECTORY: /Users/myuser/.gem/specs
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-15
- GEM PATHS:
- /Users/myuser/.gems
- /Users/myuser/.gem/ruby/2.1.0
- /Users/myuser/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- :sources => ["https://rubygems.org/", "https://my.org.url/artifactory/api/gems/gem-public/"]
- REMOTE SOURCES:
- https://rubygems.org/
- https://my.org.url/artifactory/api/gems/gem-public/
- SHELL PATH:
- /Users/myuser/.rbenv/versions/2.1.1/bin
- /usr/local/Cellar/rbenv/1.0.0/libexec
- /Users/myuser/.rbenv/shims
- /Users/myuser/.rbenv/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
The weird thing is that I already attempted this with a different gem I created using the gem tutorial and that works just fine.
Update: When debugging this for some reason it's running the lib/my_gem.rb file instead of the bin/my_gem file.

The gem has been installed properly. The issue is actually with the executable.
The executable I created ended with this common if statement:
if $PROGRAM_NAME == __FILE__
main
end
To fix it I just had to change it to this:
main

Related

Can't install gems using asdf

So I'm switching to a project that is using ruby. They use asdf as a package manager
I’m having trouble installing the gems, I’m using zsh and I’m thinking there might be an issue with the paths
Running gem install bundler outputs: You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.
ruby -v
outputs: ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
which ruby
output: /usr/bin/ruby
And I tried setting a global version for ruby with asdf global ruby 2.5.3 but that doesn’t seem to change the version
This is my gem environment:
RubyGems Environment:
- RUBYGEMS VERSION: 2.5.2.3
- RUBY VERSION: 2.3.7 (2018-03-28 patchlevel 456) [universal.x86_64-darwin18]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.3.0
- USER INSTALLATION DIRECTORY: /Users/carlos.grijalva/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/bin
- SPEC CACHE DIRECTORY: /Users/carlos.grijalva/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Library/Ruby/Site
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-18
- GEM PATHS:
- /Library/Ruby/Gems/2.3.0
- /Users/carlos.grijalva/.gem/ruby/2.3.0
- /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/gems/2.3.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/carlos.grijalva/.asdf/shims
- /Users/carlos.grijalva/.asdf/bin
- /Users/carlos.grijalva/bin
- /usr/local/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
It sounds like you may need to add the relevant lines to your .zshrc so asdf shims get recognized. You can find the instructions here
For macOS & zsh the correct lines to append to .zshrc are:
echo -e '\n. $HOME/.asdf/asdf.sh' >> ~/.zshrc
echo -e '\n. $HOME/.asdf/completions/asdf.bash' >> ~/.zshrc
After doing so, either source ~/.zshrc or restart your terminal

gem locations in macOS with chruby and TextMate preferences

I having trouble getting Ruby gems to work right with TextMate (TM) and chruby on macOS.
Clean install of Mojave (now v10.14.4) on a 2017 iMac about six months ago.
Trying to use Ruby-2.6.2, although earlier versions have been installed. System Ruby is v2.3.7
I have ended up with gems in at least three locations:
~/.rubies/ruby-2.6.2/lib/ruby/gems/2.6.0/gems/
~/.gem/ruby/2.6.2/gems/
/Library/Ruby/Gems/2.3.0/gems/
TextMate relies on its Preferences for gem and Ruby locations. As a start I have the following:
GEM_HOME is unset because I don't see why it's needed, although I've frequently read to set it as shown or similar. GEM_HOME and GEM_PATH are defined as
GEM_PATH provides the locations (there may be several) where gems can be found.
GEM_HOME is where gems will be installed (by default).
The problem is that when I run this script (after the error not included) in TM
#!/usr/bin/env ruby
system ('gem env') # for debugging
puts "\nGem.path: #{Gem.path}"
puts "\ngem list:"
system ('gem list') # for debugging
require 'fileutils'
include FileUtils
require 'find'
require 'yaml'
require "time"
require 'irb'
require 'mini_exiftool'
The result is:
Running “PhotoName-GPScoord.rb”…
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin18]
Theme:
RubyGems Environment:
- RUBYGEMS VERSION: 3.0.3
- RUBY VERSION: 2.3.7 (2018-03-28 patchlevel 456) [universal.x86_64-darwin18]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.3.0
- USER INSTALLATION DIRECTORY: /Users/gscar/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby
- GIT EXECUTABLE: /usr/bin/git
- EXECUTABLE DIRECTORY: /usr/bin
- SPEC CACHE DIRECTORY: /Users/gscar/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Library/Ruby/Site
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-18
- GEM PATHS:
- /Library/Ruby/Gems/2.3.0
- /Users/gscar
- /.rubies/ruby-2.6.2/lib/ruby/gems/2.6.0
- /.gem/ruby/2.6.2/gems/
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- "gem" => "--no-document"
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /Users/gscar/Library/Application Support/TextMate/Managed/Bundles/Bundle Support.tmbundle/Support/shared/bin
- /usr/local/bin
- /usr/texbin
- /opt/local/bin
Gem.path: ["/Users/gscar", "/.rubies/ruby-2.6.2/lib/ruby/gems/2.6.0", "/.gem/ruby/2.6.2/gems/", "/Users/gscar/.rubies/ruby-2.6.2/lib/ruby/gems/2.6.0"]
gem list:
<about 100 gems>
mimemagic (0.3.3)
mini_exiftool (2.9.0)
mini_magick (4.6.1)
<about 20 gems>
LoadError: cannot load such file — mini_exiftool
method require in kernel_require.rb at line 54
method require in kernel_require.rb at line 54
method <main> in PhotoName-GPScoord.rb at line 19
copy output
TextMate puts this in the top right of the output
Line 19 is require 'mini_exiftool'
mini_exiftool is in the gem list, but why the error?
If I run the script using the system Ruby (taking defaults, nothing set in TM)
The script runs with the following output:
Running “PhotoName-GPScoord.rb”…
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
Theme:
RubyGems Environment:
- RUBYGEMS VERSION: 3.0.3
- RUBY VERSION: 2.3.7 (2018-03-28 patchlevel 456) [universal.x86_64-darwin18]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.3.0
- USER INSTALLATION DIRECTORY: /Users/gscar/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby
- GIT EXECUTABLE: /usr/bin/git
- EXECUTABLE DIRECTORY: /usr/bin
- SPEC CACHE DIRECTORY: /Users/gscar/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Library/Ruby/Site
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-18
- GEM PATHS:
- /Library/Ruby/Gems/2.3.0
- /Users/gscar/.gem/ruby/2.3.0
- /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/gems/2.3.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- "gem" => "--no-document"
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /Users/gscar/Library/Application Support/TextMate/Managed/Bundles/Bundle Support.tmbundle/Support/shared/bin
Gem.path: ["/Users/gscar/.gem/ruby/2.3.0", "/Library/Ruby/Gems/2.3.0", "/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/gems/2.3.0"]
gem list:
<long list of gems including mini_exiftool (2.9.0)>
This particular script is OK with 2.3.7, but others may not be.
Thanks for any help.
This posting is a simplification of an earlier posting from me. I think I've boiled the problem down.
Changed GEM_HOME and GEM_PATH to $HOME/.gem/ruby/2.6.2:$HOME/.rubies/ruby-2.6.2/lib/ruby/gems/2.6.0 based on copying the those variables from what showing up when running from the terminal. Lu's comment made me look a little harder at what was being reported. Basically I was missing $HOME/ in the second path (after the colon). I have been fiddling with this for months

cap deploy throws "Ruby version is 2.2.5, but your Gemfile specified 2.5.3", except my Ruby version is 2.5.3

Out of absolutely nowhere (not an exaggeration: I deployed two hours before this exception started happening), cap production deploy stopped working and is throwing this error:
Your Ruby version is 2.2.5, but your Gemfile specified 2.5.3
bundle stderr: Nothing written
Except I'm using 2.5.3, as specified in both gemfile and in .ruby-version.
This is what's in my gemfile:
source 'https://rubygems.org'
ruby '2.5.3'
I'm using rvm. ruby -v returns ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18], and ruby is set to global. I've tried running gem update bundler, gem install bundler followed by bundle install, bundle, and deleted gemfile.lock.
$ bundle env
Bundler 1.17.1
Platforms ruby, x86_64-darwin-18
Ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]
Full Path /Users/mirai/.rvm/rubies/ruby-2.5.3/bin/ruby
Config Dir /Users/mirai/.rvm/rubies/ruby-2.5.3/etc
RubyGems 2.7.8
Gem Home /Users/mirai/.rvm/gems/ruby-2.5.3#greentea-b2b
Gem Path /Users/mirai/.rvm/gems/ruby-2.5.3#greentea-
b2b:/Users/mirai/.rvm/gems/ruby-2.5.3#global
User Path /Users/mirai/.gem/ruby/2.5.0
Bin Dir /Users/mirai/.rvm/gems/ruby-2.5.3#greentea-b2b/bin
$ which -a bundle
/Users/mirai/.rvm/rubies/ruby-2.5.3/bin/bundle
/usr/local/bin/bundle
Edit: Output of $ gem env:
RubyGems Environment:
- RUBYGEMS VERSION: 2.7.8
- RUBY VERSION: 2.5.3 (2018-10-18 patchlevel 105) [x86_64-darwin18]
- INSTALLATION DIRECTORY: /Users/mirai/.rvm/gems/ruby-2.5.3#mirai-b2b
- USER INSTALLATION DIRECTORY: /Users/mirai/.gem/ruby/2.5.0
- RUBY EXECUTABLE: /Users/mirai/.rvm/rubies/ruby-2.5.3/bin/ruby
- EXECUTABLE DIRECTORY: /Users/mirai/.rvm/gems/ruby-2.5.3#greentea-b2b/bin
- SPEC CACHE DIRECTORY: /Users/mirai/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/mirai/.rvm/rubies/ruby-2.5.3/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-18
- GEM PATHS:
- /Users/mirai/.rvm/gems/ruby-2.5.3#greentea-b2b
- /Users/mirai/.rvm/gems/ruby-2.5.3#global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- :sources => ["http://gems.rubyforge.org/"]
- "update" => "--env-shebang"
- "benchmark" => false
- "install" => "--env-shebang"
- "gem" => "-n/usr/local/bin"
- REMOTE SOURCES:
- http://gems.rubyforge.org/
- SHELL PATH:
- /Users/mirai/.rvm/gems/ruby-2.5.3#greentea-b2b/bin
- /Users/mirai/.rvm/gems/ruby-2.5.3#global/bin
- /Users/mirai/.rvm/rubies/ruby-2.5.3/bin
- /Users/mirai/.rvm/bin
- /usr/local/opt/openssl/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /opt/local/bin
- /opt/local/sbin
- /usr/local/MacGPG2/bin
- /opt/X11/bin
I'm using AWS. bundle exec cap production deploy throws the same error.

Where is the "Gemfile" in Ruby?

I often see in documentation on the Internet, "put this in the Gemfile". I don't know where and what this "Gemfile" is. If I install a gem then I have installed it. Who need than a "Gemfile"? Where or what is the Gemfile, and why is it used?
The Gemfile is wherever you want it to be - usually in the main directory of your project and the name of the file is Gemfile.
It's convenient to have one because it allows you to use Bundler to manage which gems and which versions of each your project needs to run.
If you are not using Bundler (which you should!), then you can just install any gems you come across with gem install X and ignore instructions about adding a line to your Gemfile.
Read more about it here:
http://bundler.io/gemfile.html
http://bundler.io/man/gemfile.5.html
Gemfile is in Rails project, for Ruby run gem environment to find out about your gem environment:
RubyGems Environment:
- RUBYGEMS VERSION: 2.4.8
- RUBY VERSION: 2.2.1 (2015-02-26 patchlevel 85) [i686-linux]
- INSTALLATION DIRECTORY: /home/gagan/.rvm/gems/ruby-2.2.1
- RUBY EXECUTABLE: /home/gagan/.rvm/rubies/ruby-2.2.1/bin/ruby
- EXECUTABLE DIRECTORY: /home/gagan/.rvm/gems/ruby-2.2.1/bin
- SPEC CACHE DIRECTORY: /home/gagan/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /home/gagan/.rvm/rubies/ruby-2.2.1/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /home/gagan/.rvm/gems/ruby-2.2.1
- /home/gagan/.rvm/gems/ruby-2.2.1#global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /home/gagan/.rvm/gems/ruby-2.2.1/bin
- /home/gagan/.rvm/gems/ruby-2.2.1#global/bin
- /home/gagan/.rvm/rubies/ruby-2.2.1/bin
- /usr/local/heroku/bin
- /usr/lib/lightdm/lightdm
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
- /sbin
- /bin
- /usr/games
- /home/gagan/.rvm/bin
- /home/gagan/.rvm/bin
Notice the two sections for:
INSTALLATION DIRECTORY
GEM PATHS
Gemfile is a file which must be located in root of your rails project. It is used for describing gem dependencies for Ruby programs.
The first thing in your gemfile is a source in which you tell the Gemfile where to look for gems.
Source can be called as a block and you can have multiple sources in your gemfile.
source "https://my_awesome_source.com" do
gem "my_gem"
gem "my_other_gem"
end
Here is some documentation where you can read more about gemfile
http://bundler.io/gemfile.html
Just run gem update --system and you're good to go. It's that easy!!!

Bundler can't find gem bundler/bower

Environment:
Mac OS X 10.11.3 (El Capitan)
Homebrew 0.9.5
rvm 1.26.11 (same problem occurs with rbenv)
Bundler 1.11.2
Gemfile (excerpt):
source 'https://rubygems.org'
require 'bundler/bower'
asset "jquery", "~2.1.4"`
I get this error:
bundle install
[!] There was an error parsing `Gemfile`:
cannot load such file -- bundler/bower. Bundler cannot continue.
# from /Users/nobby/becompany/website/src/website-static/Gemfile:5
# -------------------------------------------
#
> require 'bundler/bower'
#
# -------------------------------------------
My RubyGems environment is:
- RUBYGEMS VERSION: 2.5.1
- RUBY VERSION: 2.3.0 (2015-12-25 patchlevel 0) [x86_64-darwin15]
- INSTALLATION DIRECTORY: /Users/nobby/.rvm/gems/ruby-2.3.0
- USER INSTALLATION DIRECTORY: /Users/nobby/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /Users/nobby/.rvm/rubies/ruby-2.3.0/bin/ruby
- EXECUTABLE DIRECTORY: /Users/nobby/.rvm/gems/ruby-2.3.0/bin
- SPEC CACHE DIRECTORY: /Users/nobby/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/nobby/.rvm/rubies/ruby-2.3.0/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-15
- GEM PATHS:
- /Users/nobby/.rvm/gems/ruby-2.3.0
- /Users/nobby/.rvm/gems/ruby-2.3.0#global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/nobby/.rvm/gems/ruby-2.3.0/bin
- /Users/nobby/.rvm/gems/ruby-2.3.0#global/bin
- /Users/nobby/.rvm/rubies/ruby-2.3.0/bin
- /Users/nobby/.rvm/bin
- /Users/nobby/src/apache/ant/apache-ant-1.9.6/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
Maybe bundler is looking in the wrong gem paths? Is there a way to see which paths it is using?
It works when I manually add the dependencies to LOAD_PATH in Gemfile; maybe this helps to point me to the cause of the problem:
[ 'bundler-bower-0.0.3', 'bower-rails-0.10.0'].each do |dep|
$LOAD_PATH.unshift "/Users/nobby/.rvm/gems/ruby-2.3.0/gems/#{dep}/lib"
end
https://github.com/LTe/bundler-bower
You need to
gem install 'bundler-bower'
before you can require modules from it. Potentially (hopefully) bundler is clever enough to resolve dependency order if you add
gem 'bundler-bower'
to your gemfile and then install it with
bundle install
instead

Resources