Why does gem persist in installing to ~/.rvm after I've moved to rbenv? - ruby

I've decided to move away from rvm to rbenv on my personal machine. Followed the rvm removal instructions, installed rbenv and everything looks peachy. After restarting my shell session:
$ rbenv global
1.9.3-p0
$ ls .rvm
ls: cannot access .rvm: No such file or directory
$ rbenv exec gem list
*** LOCAL GEMS ***
A completely fresh install in a fresh shell--RVM's long gone. Now, when I install bundler
$ rbenv exec gem install bundler
Fetching: bundler-1.0.22.gem (100%)
Successfully installed bundler-1.0.22
1 gem installed
$ rbenv rehash
$ echo $?
0
$ rbenv which bundle
rbenv: bundle: command not found
What? Indeed, look at this nonsense:
$ ls .rvm/gems/ruby-1.9.3-p0/gems/bundler-1.0.22/
bin bundler.gemspec CHANGELOG.md ISSUES.md lib LICENSE man Rakefile README.md spec UPGRADING.md
Somehow, someway, RVM still has its hooks into me. My ~/.bash* files are clean and
$ cat ~/.gemrc
gem: --no-ri --no-rdoc
I have no system ruby, no globally installed RVM (nothing in /etc/profile/, no /etc/profile.d/rvm.sh and nothing in /etc/bash.bashrc) and I'm at a complete loss.
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.11
- RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [i686-linux]
- INSTALLATION DIRECTORY: /home/blt/.rvm/gems/ruby-1.9.3-p0
- RUBY EXECUTABLE: /home/blt/.rbenv/versions/1.9.3-p0/bin/ruby
- EXECUTABLE DIRECTORY: /home/blt/.rvm/gems/ruby-1.9.3-p0/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /home/blt/.rvm/gems/ruby-1.9.3-p0
- /home/blt/.rvm/gems/ruby-1.9.3-p0#global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- "gem" => "--no-ri --no-rdoc"
- REMOTE SOURCES:
- http://rubygems.org/
Where else can I look to figure out why gem's configured to install to ~/.rvm?

Something's clearly rotten on my system; no grepping has turned it up, but tacking
unset GEM_HOME
unset GEM_PATH
to the end of my ~/.bashrc made things sane. Horrible hack, though.

Have you ever installed rvm in a system location? Maybe those env vars got into a system-wide shell config.
Try running:
bash -l -x -c 'echo'
This will print bash debugging output so you can see what files are being loaded. It may not show commands inside sourced scripts, depending on your version of bash.
This probably doesn't apply to you, but if it exists, what are the contents of ~/.gemrc ?

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.)

Installing gems for a JRuby instance which runs in cron

Problem
My goal is to install a gem for a JRuby script that is running in cron. That gem is typhoeus.
Information
Here is the output of env, running from cron:
SHELL=/bin/sh
USER=user
PATH=/usr/bin:/bin
JRUBY_HOME=/home/user/rubies/jruby-1.6.1
JAVA_HOME=/usr/java/default
SHLVL=1
HOME=/home/user
OLDPWD=/home/user
_=/usr/bin/env
Running $JRUBY_HOME/bin/jruby -S gem environment yields the following:
RubyGems Environment:
- RUBYGEMS VERSION: 1.5.1
- RUBY VERSION: 1.8.7 (2011-04-12 patchlevel 330) [java]
- INSTALLATION DIRECTORY: /home/user/rubies/jruby-1.6.1/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /home/user/rubies/jruby-1.6.1/bin/jruby
- EXECUTABLE DIRECTORY: /home/user/rubies/jruby-1.6.1/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-java-1.6
- GEM PATHS:
- /home/user/rubies/jruby-1.6.1/lib/ruby/gems/1.8
- /home/user/.gem/jruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- "install" => "--no-rdoc --no-ri --env-shebang"
- "update" => "--no-rdoc --no-ri --env-shebang"
- REMOTE SOURCES:
- http://rubygems.org/
What I've Tried
Changing GEM_HOME to match cron's jruby
In view of the output of the above gem environment and another SO post about specifying gem install directories,
I temporarily changed $GEM_HOME to the directory I have access to as this user (/home/user/.gem/jruby/1.8).
I ran /home/user/rubies/jruby-1.6.1/bin/jruby -S gem install typhoeus.
I check for typhoeus using /home/user/rubies/jruby-1.6.1/bin/jruby -S gem list and I do see typhoeus in the output
I run the same command from cron and do not see typhoeus in its output that I save to a file
Running jruby's gem install from cron
Hacky, I know. I run exactly /home/user/rubies/jruby-1.6.1/bin/jruby -S gem install typhoeus --verbose from inside cron and get the following dumped to a file:
GET http://rubygems.org/latest_specs.4.8.gz
302 Moved Temporarily
GET http://production.s3.rubygems.org/latest_specs.4.8.gz
200 OK
Installing gem ffi-1.9.8-java
But still, running /home/user/rubies/jruby-1.6.1/bin/jruby -S gem install list from cron does not show typhoeus. (And of course, the script that I'm trying to get working barfs on the library not being there).
The answer to this is that attempting to install typhoeus with this version of jruby results in a jruby bug in versions earlier than 1.6.2.
I only witnessed by redirecting stderr to an ouput file in cron when running gem install typhoeus from the crontab. (I had previously only redirected stdout).

"cannot load such file" with rubygems and rvm

I develop a package called CodeRunner that has a number of add-on package (CodeRunner modules, all with names like ...crmod). One of these is called trinitycrmod.
On Debian stable (wheezy), I have installed RVM using the standard instructions. My .bashrc is setup correctly: the final line is:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
I am using Ruby 2.1.0 (but this error can be reproduced with 1.9.3)
rvm use 2.1
I have installed CodeRunner and trinitycrmod:
gem install coderunner
gem install trinitycrmod
Now the CodeRunner gem contains the tool coderunner. One of coderunner's commands is called ex, and it evaluates the ruby fragment you give the command. I want to load trinitycrmod.
coderunner ex ' require "trinitycrmod"'
But I get an error when loading the file (which is part of the installed gem).
.rvm/gems/ruby-2.1.0/gems/coderunner-0.13.32/lib/coderunner.rb:118:in `require': cannot load such file -- trinitycrmod (LoadError)
I don't get this error if I use ruby directly....
ruby `which coderunner` ex ' require "trinitycrmod"'
runs fine. I also don't get this error if I use the system ruby:
rvm use system
coderunner ex ' require "trinitycrmod"'
I also don't get this error if I edit the coderunner bin file and replace ruby_executable_hooks with plain ruby:
sed -i s/ruby_executable_hooks/ruby/ `which coderunner`
This makes me think that it has to be an error with ruby gems and/or rvm and not my mistake, particularly as this code is all at least 6 months old and has worked fine up till now.
PS My gem environment seems to be just fine....
RUBYGEMS VERSION: 2.2.1
RUBY VERSION: 2.1.0 (2013-12-25 patchlevel 0) [x86_64-linux]
INSTALLATION DIRECTORY: /home/edmundhighcock/.rvm/gems/ruby-2.1.0
RUBY EXECUTABLE: /home/edmundhighcock/.rvm/rubies/ruby-2.1.0/bin/ruby
EXECUTABLE DIRECTORY: /home/edmundhighcock/.rvm/gems/ruby-2.1.0/bin
SPEC CACHE DIRECTORY: /home/edmundhighcock/.gem/specs
RUBYGEMS PLATFORMS:
ruby
x86_64-linux
GEM PATHS:
/home/edmundhighcock/.rvm/gems/ruby-2.1.0
/home/edmundhighcock/.rvm/gems/ruby-2.1.0#global
GEM CONFIGURATION:
:update_sources => true
:verbose => true
:backtrace => false
:bulk_threshold => 1000
REMOTE SOURCES:
https://rubygems.org/
SHELL PATH:
/home/edmundhighcock/.rvm/gems/ruby-2.1.0/bin
/home/edmundhighcock/.rvm/gems/ruby-2.1.0#global/bin
/home/edmundhighcock/.rvm/rubies/ruby-2.1.0/bin
/home/edmundhighcock/.rvm/bin
/home/edmundhighcock/Code/scripts
/usr/local/bin
/usr/bin
/bin
/usr/local/games
/usr/games
the error message simply states that trinitycrmod gem is not installed correctly,
run gem list command and check whether the trinitycrmod gem is installed or not in your current gemset?

Ruby Gems returns "command not found"

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

Resources