Ruby Gems returns "command not found" - ruby

Ubuntu 9.10
Just installed newgem
gem install newgem
and when i try
newgem new_project
I get
adam#adam-ubuntu:~$ newgem newproject
newgem: command not found
Ive checked my path via echo $PATH
adam#adam-ubuntu:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/adam/.gem
and my gem enviroment
adam#adam-ubuntu:~$ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.6
- RUBY VERSION: 1.8.7 (2009-06-12 patchlevel 174) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/bin/ruby1.8
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/lib/ruby/gems/1.8
- /home/adam/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
Im not hot with paths etc but all the gem directories listed above are on the path so how come it cant find the command?

Your $PATH variable needs to include the exact path to your Ruby's bin directory. Adding a directory to the PATH does not include it's subfolders. Try adding the bin directory via:
export PATH=$PATH:/home/adam/.gem/ruby/1.8/bin
or if you installed the gem using sudo:
export PATH=$PATH:/usr/lib/ruby/gems/1.8/bin
You might want to add this to your .bashrc file, so that you don't have to set this manually every time your open up a new bash.

(Just stealing #John Franklin's comment)
$ gem environment
Will tell you the EXECUTABLE DIRECTORY. Then put whatever that value is in your PATH like so (in your .bashrc or other shell config file).
export PATH="$PATH:/path/to/bin"
Reload your shell and you should then be able to use the installed gem.

If you use RVM (most do), then it will take care of this for you. In fact putting it in your path directly may conflict. You have to set a ruby to use though. Run one of these on the command line.
rvm use 1.9.3
or
rvm use --default 1.9.3

Related

Find where a specific Ruby version is installed

On Mac, I used to have rbenv as Ruby version manager but recently switched to asdf. I want to create a Ruby on Rails 7.0.4 but it uses an outdated version and I'd like to locate it.
$ rails new app
"Rails 7 requires Ruby 2.7.0 or newer.
You're running
ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.arm64e-darwin"
$ which ruby
/Users/kawsay/.asdf/shims/ruby
$ ruby -v
ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c5) [arm64-darwin21]
$ irb
irb(main):001:0> RUBY_VERSION
=> "3.0.5"
$ asdf list ruby
2.7.4
*3.0.5
$ brew info ruby
==> ruby: stable 3.2.0 (bottled), HEAD [keg-only]
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 3.2.33
- RUBY VERSION: 3.0.5 (2022-11-24 patchlevel 211) [arm64-darwin21]
- INSTALLATION DIRECTORY: /Users/kawsay/.gem
- USER INSTALLATION DIRECTORY: /Users/kawsay/.gem/ruby/3.0.0
- RUBY EXECUTABLE: /Users/kawsay/.asdf/installs/ruby/3.0.5/bin/ruby
- GIT EXECUTABLE: /usr/bin/git
- EXECUTABLE DIRECTORY: /Users/kawsay/.gem/bin
- SPEC CACHE DIRECTORY: /Users/kawsay/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/kawsay/.asdf/installs/ruby/3.0.5/etc
- RUBYGEMS PLATFORMS:
- ruby
- arm64-darwin-21
- GEM PATHS:
- /Users/kawsay/.gem
- /Users/kawsay/.gem/ruby/3.0.0
- /Users/kawsay/.asdf/installs/ruby/3.0.5/lib/ruby/gems/3.0.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/kawsay/.asdf/installs/ruby/3.0.5/bin
- /Users/kawsay/.asdf/shims
- /opt/homebrew/opt/asdf/libexec/bin
- /Users/kawsay/.cargo/bin
- /opt/homebrew/opt/openssl#2.8/bin
- /opt/homebrew/opt/python#3.8/bin/python3
- /opt/homebrew/bin
- /opt/homebrew/sbin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
$ which rbenv
rbenv not found
$ which rvm
rbenv not found
How can I find where ruby 2.6.8p205 is installed ?
You have an issue where you have system Ruby, brew Ruby, asdf Ruby, and rbenv Ruby. In short, your system is confused about where to find things.
The first step is to remove brew's Ruby installation:
brew uninstall ruby
The second step is to remove rbenv completely. (or use the official docs)
The third step is to make sure that system Ruby does not have the Rails gem installed. Start a shell without loading your profile so that you can bypass asdf and any other configuration:
env -i bash --norc --noprofile
Then confirm it's pointing at system Ruby: (your version will differ slightly depending on your version of macOS)
$ ruby -v
ruby 2.6.10p210 (2022-04-12 revision 67958) [universal.arm64e-darwin22]
Then uninstall the Rails gem:
gem uninstall rails
But while you're at it you might want to remove all the gems you installed using system Ruby: (ignore any errors here)
gem uninstall -aIx
Now close that shell session and switch back to your default shell. Make sure that your shell profile is cleared of anything related to rbenv and that you have no GEM_* environment variables being set. (assuming you use ZSH, check every ~/.zsh* file and ~/.gemrc) Then start a new shell session to load the cleaned profiles.
Then make sure asdf has a properly groomed environment:
asdf reshim
Now when you run gem env you should see output like this:
RubyGems Environment:
- RUBYGEMS VERSION: 3.4.1
- RUBY VERSION: 3.2.0 (2022-12-25 patchlevel 0) [arm64-darwin22]
- INSTALLATION DIRECTORY: /Users/foo/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0
- USER INSTALLATION DIRECTORY: /Users/foo/.gem/ruby/3.2.0
- RUBY EXECUTABLE: /Users/foo/.asdf/installs/ruby/3.2.0/bin/ruby
- GIT EXECUTABLE: /opt/homebrew/bin/git
- EXECUTABLE DIRECTORY: /Users/foo/.asdf/installs/ruby/3.2.0/bin
- SPEC CACHE DIRECTORY: /Users/foo/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/foo/.asdf/installs/ruby/3.2.0/etc
- RUBYGEMS PLATFORMS:
- ruby
- arm64-darwin-22
- GEM PATHS:
- /Users/foo/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0
- /Users/foo/.gem/ruby/3.2.0
Note the difference between INSTALLATION DIRECTORY and EXECUTABLE DIRECTORY in this output and yours. You need to have output that looks like this, otherwise that means that asdf is not installed properly and you should restart the installation of asdf from the beginning.
Afterwards, re-run gem install rails and you should be able to complete rails new app successfully.
If not, there is a shortcut to run commands with asdf for any given command:
asdf exec gem install rails
asdf exec rails new app
asdf exec rails server
This should bypass everything else and use asdf exclusively.

Managing Path With rbenv

I installed rbenv to manage my ruby installations, and then I subsequently ran gem install github-pages to get jekyll and other dependencies needed for developing a github pages site. However, now when I try to run the jekyll command I get
The program 'jekyll' is currently not installed. You can install it by typing:
sudo apt-get install jekyll
I assume this is a path issue, but I thought the path was supposed to be handled by rbenv. I also previously had the same issue when completing the same process using RVM instead of rbenv. How can I fix this?
I just tried running gem env and got the following:
RubyGems Environment:
- RUBYGEMS VERSION: 2.5.1
- RUBY VERSION: 2.3.0 (2015-12-25 patchlevel 0) [x86_64-linux]
- INSTALLATION DIRECTORY: /home/daniel/.rvm/gems/ruby-2.3.0
- USER INSTALLATION DIRECTORY: /home/daniel/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /home/daniel/.rbenv/versions/2.3.0/bin/ruby
- EXECUTABLE DIRECTORY: /home/daniel/.rvm/gems/ruby-2.3.0/bin
- SPEC CACHE DIRECTORY: /home/daniel/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /home/daniel/.rbenv/versions/2.3.0/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /home/daniel/.rvm/gems/ruby-2.3.0
- /home/daniel/.rvm/gems/ruby-2.3.0#global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- "gem" => "--no-document"
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /home/daniel/.rbenv/versions/2.3.0/bin
- /home/daniel/.rbenv/libexec
- /home/daniel/.rbenv/plugins/ruby-build/bin
- /home/daniel/.rbenv/plugins/ruby-build/bin
- /home/daniel/.rbenv/shims
- /home/daniel/.rbenv/bin
- /opt/jdk1.8.0_65/bin/home/daniel/.rvm/gems/ruby-2.3.0/bin
- /home/daniel/.rvm/gems/ruby-2.3.0#global/bin
- /home/daniel/.rvm/rubies/ruby-2.3.0/bin
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
- /sbin
- /bin
- /usr/games
- /usr/local/games
- /home/daniel/.rvm/bin
You have both rbenv and RVM installed and running at the same time. Don't do that.
Per the rbenv documentation:
Compatibility note: rbenv is incompatible with RVM. Please make sure to fully uninstall RVM and remove any references to it from your shell initialization files before installing rbenv.
The why to that delves into how the shell finds commands which would be off-topic. You can research that if you're so inclined.
Should I just copy /home/daniel/.rmv/gems to /home/daniel/.rbenv/bin?
No.
"just copy" would run the risk of havoc breaking loose farther down the road.
I'd recommend temporarily disabling rbenv by commenting-out its initialization in your ~/.bash_profile, then restart your terminal session resulting in only RVM being available. Then run
gem list --no-versions > ~/rvm-gems-list
Then reenable rbenv by removing the comment mark, and comment-out the RVM initialization and restart your terminal session to give rbenv control. Run
xargs gem install --conservative < ~/rvm-gems-list
which will spin through all the gems installed in your RVM instance and let gem install any that are missing. Once that finishes you can delete the rvm-gems-list file:
rm ~/rvm-gems-list
At this point you need to remove RVM, or toggle back and forth between rbenv and RVM by alternately enabling/disabling them as you did above. You can't have them both running at the same time, and trying to remember where gems are installed will be a pain so I'd suggest sticking with one or the other.
(I use them both, only on separate systems.)

Multi-User RVM - gem path different when using sudo to execute Ruby script

I have installed RVM in a Multi-User configuration. Everything seems to work, but the gem installation folder and path are different when I have to use sudo to execute a Ruby script. This ends up resulting in gems not being available to a script.
If I execute gem env I get the following:
RubyGems Environment:
- RUBYGEMS VERSION: 2.4.8
- RUBY VERSION: 2.2.2 (2015-04-13 patchlevel 95) [x86_64-darwin14]
- INSTALLATION DIRECTORY: /usr/local/rvm/gems/ruby-2.2.2
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-2.2.2/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/rvm/gems/ruby-2.2.2/bin
- SPEC CACHE DIRECTORY: /Users/patrickaikens/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-14
- GEM PATHS:
- /usr/local/rvm/gems/ruby-2.2.2
- /usr/local/rvm/gems/ruby-2.2.2#global
...
If I execute sudo gem env, I get this instead:
RubyGems Environment:
- RUBYGEMS VERSION: 2.4.8
- RUBY VERSION: 2.2.2 (2015-04-13 patchlevel 95) [x86_64-darwin14]
- INSTALLATION DIRECTORY: /usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0
- RUBY EXECUTABLE: /usr/local/rvm/rubies/ruby-2.2.2/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/rvm/rubies/ruby-2.2.2/bin
- SPEC CACHE DIRECTORY: /Users/patrickaikens/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-14
- GEM PATHS:
- /usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/gems/2.2.0
- /Users/patrickaikens/.gem/ruby/2.2.0
...
Notice that INSTALLATION_DIRECTORY and GEM PATHS are different, but both outputs indicate that RVM is "active". I don't have a .rvm folder in either my own home directory or in root's home directory. Why is RVM handling those paths differently, and can I make then the same? Using rvmsudo does seem to work, but I really don't want to have to remember which of my admin scripts will run Ruby at some point if I don't have to.
The reason you see different information appearing when running normally and when running as "sudo" is because using sudo invokes the system ruby, bypassing RVM, rather than the version selected in your RVM.
I'm having this issue myself.
The RVM troubleshooting page suggests using rvmsudo instead, but then also suggests you wont need to.

Problem running gems in OS X

I'm running Snow Leopard, and installed a custom built Ruby according to the guide here: http://hivelogic.com/articles/compiling-ruby-rubygems-and-rails-on-snow-leopard . My ruby binary lives in usr/local/bin/ruby and my gems are installed in /usr/local/bin/gem . My gem env looks like so:
RUBY VERSION: 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
I think I may have borked the install since all actions taked on gems give the error:
ERROR: While executing gem ... (Errno::EEXIST)
File exists - /usr/local/bin/ruby
How do you edit the environment variables for the gem environment?
And for those of you on OS X and using ruby AND gems, what did you use to get yourself up and running?
I'm thinking of just nuking everything and starting anew.
It looks like your installation of ruby into /usr/local/bin is not the ruby that you're getting, which probably means /usr/local/bin/ is not in your PATH. If you'd like to run that ruby, try editing your .bash_profile to add /usr/local/bin like this:
export PATH=/usr/local/bin:$PATH
Alternately, you can add the path to a file in /etc/paths.d, which is the preferred method for adding paths globally on an OS X system.
I've tried things about three different ways, each with varying results. I have systems with the default install (/usr/bin), hand-built /usr/local/bin and also Mac Ports in /opt/local/bin.
By far, the simplest is to just use the built-in binary, which on 10.6.2 is ruby 1.8.7. In that scheme, gems are installed in /Library/Ruby/Gems. Second simplest is MacPorts (sudo port install ruby), third is the totally from source method you're describing above. Certainly there are good reasons people install from source, but unless you're trying to run ruby 1.9 or another version, you're best off using the built-in ruby 1.8.7.
Does the command "gem env" not report any gem paths? Mine reports a number of gem paths along with teh version of rubygems etc. that yours does not appear to be doing.
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.5
- RUBY VERSION: 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-10
- GEM PATHS:
- /Library/Ruby/Gems/1.8
- /Users/steveweet/.gem/ruby/1.8
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://gems.rubyforge.org/
You can edit the environment variables associated with gem by specifying them in your shell startup files, (.bashrc for bash). The relevant variables are all listed in the environment page of the gem web site. You will want to pay particular attention to GEM_HOME and GEM_PATH. You can check the current settings of these by typing echo $GEM_HOME at a shell profile
I would suggest that your best action at this moment in time may be to go back to your default ruby installation as provided with OS/X and then download and install rvm (The Ruby version manager) and then watch Ryan Bates screencast

OS X gem execution environment

NOTE: user377519 (answer below) pointed me in the right direction. I've posted the 'fix' at the bottom of the questions, in case you found this and need more than a 'pointer'
I can't figure out why the second call to "gem env" doesn't have the same environment as the first?
Any pointers? This is driving me crazy!!!!
$ which gem
/usr/local/bin/gem
$ /usr/local/bin/gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.8.8 (2010-06-26 patchlevel 1) [i386-darwin9.8.0]
- INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/local/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-darwin-9
- GEM PATHS:
- /usr/local/lib/ruby/gems/1.8
- /Users/pauliprice/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- :sources => ["http://gems.rubyforge.org/", "http://gems.github.com", "http://gems.github.com"]
- REMOTE SOURCES:
- http://gems.rubyforge.org/
- http://gems.github.com
- http://gems.github.com
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.8.6 (2009-06-08 patchlevel 369) [universal-darwin9.0]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-9
- GEM PATHS:
- /Library/Ruby/Gems/1.8
- /Users/pauliprice/.gem/ruby/1.8
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- :sources => ["http://gems.rubyforge.org/", "http://gems.github.com", "http://gems.github.com"]
- REMOTE SOURCES:
- http://gems.rubyforge.org/
- http://gems.github.com
- http://gems.github.com
SOLUTION:
Given that you don't (as I do not) want to switch between different versions, merely install a new version, on you OSX 10.5 box, this is how to get RubyGems installed correctly.
Assuming you built and installed your new Ruby like this:
./configure --prefix=/usr/local --enable-pthread
sudo make
sudo make install
edit your ~/.bash_profile and add this line
export PATH=/usr/local/bin:$PATH
Save and refresh your terminal. Then you should install RubyGems like this:
Download and setup latest rubygems
wget http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz
tar -xzv http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz
cd rubygems-1.3.7
sudo su
# export PREFIX=/usr/local
# export GEM_HOME=$PREFIX/lib/ruby/gems/1.8
# export RUBYLIB=$PREFIX/lib/ruby:$PREFIX/lib/site_ruby/1.8
# ruby setup.rb all —prefix=$PREFIX
exit
edit ~/.bash_profile and add the following lines
export PREFIX=/usr/local
export GEM_HOME=$PREFIX/lib/ruby/gems/1.8
export RUBYLIB=$PREFIX/lib/ruby:$PREFIX/lib/site_ruby/1.8
And you're all good.
Refresh Terminal,
Ruby and its tools are "smart." They are aware of execution context and will look for configurations based on where they are started. The first execution is out of a system directory, while the later is out of your home directory.
Given that you have two different versions and you're on Mac, it means you had to have either installed Ruby manually or through RVM. Given that the newer version is out system directory, I guess that you attempted to install a newer version of Ruby manually and am now confused why it's not working for you. You installed it into system space, but did not update Ruby's configuration in your home directory. Unfortunately, I have no answer how to get you out of this position. Hopefully, you have something that will let you undo your install.
The far safer way of installing different copies of Ruby is to use Ruby Version Manager (RVM). It does all the magic necessary to install multiple copies of Ruby without changing the system. From the command line you can invoke using a different interpreter for that session. Thus, system stays untouched but you can run JRuby, v1.9.2, etc.
Hope this gets you in a direction.

Resources