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
Related
I am having trouble with the Jekyll installation. Everything is up to date with Ruby, Homebrew and Gems:
Quocs-MacBook-Air:~ quochoantrinh$ brew -v
Homebrew 2.1.6
Homebrew/homebrew-core (git revision 2796; last commit 2019-07-01)
Quocs-MacBook-Air:~ quochoantrinh$ ruby -v
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin16]
Quocs-MacBook-Air:~ quochoantrinh$ gem -v
3.0.3
Then I ran the following from the Jekyll on macOS setup from the official website (https://jekyllrb.com/docs/installation/macos/)
export PATH=/usr/local/opt/ruby/bin:$PATH
Afterwards I did the next commands:
gem install --user-install bundler jekyll
export PATH=$HOME/.gem/ruby/2.6.3/bin:$PATH
However when I run jekyll in my terminal it gives me the "command not found".
This is my gem env
Quocs-MacBook-Air:~ quochoantrinh$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 3.0.3
- RUBY VERSION: 2.6.3 (2019-04-16 patchlevel 62) [x86_64-darwin16]
- INSTALLATION DIRECTORY: /Users/quochoantrinh/.rvm/gems/ruby-2.6.3
- USER INSTALLATION DIRECTORY: /Users/quochoantrinh/.gem/ruby/2.6.0
- RUBY EXECUTABLE: /usr/local/opt/ruby/bin/ruby
- GIT EXECUTABLE: /usr/bin/git
- EXECUTABLE DIRECTORY: /Users/quochoantrinh/.rvm/gems/ruby-2.6.3/bin
- SPEC CACHE DIRECTORY: /Users/quochoantrinh/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /usr/local/Cellar/ruby/2.6.3/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-16
- GEM PATHS:
- /Users/quochoantrinh/.rvm/gems/ruby-2.6.3
- /Users/quochoantrinh/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /usr/local/opt/ruby/bin
- /Users/quochoantrinh/.yarn/bin
- /Users/quochoantrinh/.config/yarn/global/node_modules/.bin
- /Users/quochoantrinh/anaconda3/bin
- /Users/quochoantrinh/anaconda3/bin
- /Users/quochoantrinh/anaconda/bin
- /Users/quochoantrinh/.gem/ruby/2.6.3/bin
- /Users/quochoantrinh/.rvm/gems/ruby-2.6.3/bin
- /Users/quochoantrinh/.rvm/gems/ruby-2.6.3#global/bin
- /Users/quochoantrinh/.rvm/rubies/ruby-2.6.3/bin
- /Users/quochoantrinh/.yarn/bin
- /Users/quochoantrinh/.config/yarn/global/node_modules/.bin
- /Users/quochoantrinh/anaconda3/bin
- /Users/quochoantrinh/anaconda3/bin
- /Users/quochoantrinh/anaconda/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /Library/Frameworks/Mono.framework/Versions/Current/Commands
- /Users/quochoantrinh/.rvm/bin
There are a few things going on here, but to fix your immediate problem, I believe this should do it:
export PATH="$HOME/.gem/ruby/2.6.0/bin:$PATH"
When you use the --user-install flag, it installs gems in the USER_INSTALLATION_DIRECTORY that's mentioned when you run gem env, which in your case is /Users/quochoantrinh/.gem/ruby/2.6.0, and then you have to add /bin when you add it to the PATH. It's not so much that you want to change the USER_INSTALLATION_DIRECTORY, but that you need to set the PATH to point to the USER_INSTALLATION_DIRECTORY. I know it's not very intuitive because you would think that you want to use the path that corresponds to your current Ruby version (2.6.3), but the USER_INSTALLATION_DIRECTORY always ends with a 0. It only uses the first 2 digits of the Ruby version. If you ever upgrade to 2.7.2, which is the latest version as of today, your PATH has to point to 2.7.0.
Note that running the export PATH command above will only work during your current session. If you want to be able to use jekyll every time you quit and relaunch Terminal, or open a new tab, you need to add that export PATH command to your shell startup file, typically either ~/.bash_profile or ~/.zshrc. If you're not sure which shell you are using, read this: https://www.moncefbelyamani.com/which-shell-am-i-using-how-can-i-switch/
I also noticed that you seem to have installed Ruby with both Homebrew and RVM. This could potentially cause problems. I always recommend that people install Ruby with a Ruby manager (such as RVM, rbenv, and chruby), because most of them automatically set everything up for you. You don't have to worry about setting the PATH manually and thinking about whether to add --user-install. My preferred manager is chruby because it's the lightest and easiest to use.
Based on your gem env, it looks like you are currently using the Homebrew version of Ruby, so I wanted to let you know of another issue you might run into. (I can tell it's Homebrew by looking at the RUBY_EXECUTABLE, and I know from experience that Homebrew installs things in /usr/local and I recognize that path as the Homebrew Ruby.)
Let's say you want to install rails months from now, and you type gem install rails (without the --user-install flag). Once it's installed, if you try to use the rails command, you will get "command not found", because when you use the Homebrew version of Ruby, and you don't use --user-install, the gem will get installed in a directory managed by Homebrew, not in your USER_INSTALLATION_DIRECTORY. The Jekyll documentation forgot to point out that you need to add the Homebrew gems location to your PATH. The fix is covered in my definitive guide to installing Ruby gems on a Mac, and also in the pull request I opened to fix the Jekyll documentation.
To understand more about how PATH works, and why you can get a "command not found" error, I recommend this article:
https://www.moncefbelyamani.com/troubleshooting-command-not-found-in-the-terminal/
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.)
I've already asked about installing python packages at $HOME. It worked flawlessly.
Now, I'm planning to install Vagrant. It's a gem (a ruby package), and I would like to install it in my $HOME folder as well but I'm not used to it so I'm asking for help here.
I've already run,
sudo gem update --system
As followed by their mac tutorial. And when I try to install Vagrant using their recomendation:
gem install vagrant
I get:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions into the /usr/bin directory.
(It's strange that just gem install vagrant doesn't work, I followed the guide from Vagrant's official website in a vanilla installed Mac, never used Ruby here and I got the error. Specially when using sudo is not required.)
Ok. Using sudo is going to resolve it, but I don't want to install this gem system wide, I want to install them at $HOME like I did with pip - and I have no idea how I can do it without messing up with this mac machine. Can someone help me how to do it the best way? Which variables to I have to export, if I have to add ~/.gem to my $PATH, or something like that. (Did I already messed everything up when I used sudo gem update --system?)
Some people are suggesting to use rvm, but I think this is overkill since I'm not going to develop ruby applications against the system Ruby, I just want to run Vagrant. Of course, if there isn't another way, I'm going to use it.
My gem environment command outputs:
gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.10
- RUBY VERSION: 1.8.7 (2009-06-12 patchlevel 174) [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/user/.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://rubygems.org/
EDIT: I've found a tutorial, that says that I need just to add to my .profile:
export GEM_HOME=$HOME/.gems
export PATH=$GEM_HOME/bin:$PATH
Is it safe to use this approach?
Using the tutorial approach I posted it seems to work. I'm going to wait for more experienced ruby developers if this approach has any caveats in my scenario (I just want to run ruby applications, don't want to develop them) and them going to accept my own answer. :)
Transcripted tutorial:
Recommended: Installing Gems to Your Home Directory
Where possible, avoid installing gems into the global system. This
ensures that the gems that you install are easy to identify and
manage, and do not interfere with the global Ruby installation.
As of RubyGems 1.3, RubyGems will automatically install gems into the
directory specified by the GEM_HOME environment variable if the system
location is not accessible. You will see that directories are created
within your GEM_HOME directory to store gem files. This means that
RubyGems will do the right thing when managing packages, provided that
you do not run the gem utility with administrative privileges.
To do this, edit the .profile file in your home directory, and add or
amend it to include these lines:
export GEM_HOME=$HOME/gems
export PATH=$GEM_HOME/bin:$PATH
This takes effect the next time that you login or create a terminal window.
The presence of the bin subdirectory on your PATH enables you to use
any command-line utilities that are installed with your gem packages.
I manually built Ruby 1.9.2 on Snow Leopard. Now I can’t find my old GEM files. I’m guessing they're in a different path now or something. So I have three questions:
What is the "old" gem path, where gem install sinatra puts the sinatra gem?
What is the "new" gem path, which is set when I build Ruby manually?
How do I change it so Ruby finds my gems again?
Typing gem env (Using your old Ruby install's gem command) at a command prompt gives something similar to:
> RubyGems Environment:
> - RUBYGEMS VERSION: 1.3.6
> - RUBY VERSION: 1.9.1 (2009-07-16 patchlevel 243) [i386-mingw32]
> - INSTALLATION DIRECTORY: C:/Ruby19/lib/ruby/gems/1.9.1
> - RUBY EXECUTABLE: C:/Ruby19/bin/ruby.exe
> - EXECUTABLE DIRECTORY: C:/Ruby19/bin
> - RUBYGEMS PLATFORMS:
> - ruby
> - x86-mingw32
> - GEM PATHS:
> - C:/Ruby19/lib/ruby/gems/1.9.1
> - C:/Users/Username/.gem/ruby/1.9.1
> - GEM CONFIGURATION:
> - :update_sources => true
> - :verbose => true
> - :benchmark => false
> - :backtrace => false
> - :bulk_threshold => 1000
> - REMOTE SOURCES:
> - http://rubygems.org/
(On Windows... I imagine Snow Leopard will have a similar format)
The GEM PATHS field is the interesting thing here. If you go to those directories listed, you should see a folder named cache. That will contain a list of .gem files corresponding to all the installed gems in that specific directory. You should just be able to call gem install *gemname* on each of those gem files (using your new Ruby install's gem command).
EDIT: Mistakenly referred to INSTALLATION DIRECTORY instead of GEM PATHS. Greg reminded me that there are multiple locations known by a specific installation of Rubygems. All of those locations needs to be checked for gems used by that installation of Ruby.
Your "old" gems would be relative to the Ruby that came bundled with the Mac because the gem command is included with Ruby 1.8.7, which is stock on Snow Leopard. If your which ruby shows /usr/bin/ruby, your gem environments should be similar to:
- GEM PATHS:
- /Library/Ruby/Gems/1.8
- /Users/greg/.gem/ruby/1.8
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
If you are using RVM you might be able to tell it to copy your gems from the system gemset to one under RVM's control. I haven't tried doing that as I install RVM immediately and let it handle all my Ruby installation and then I ignore the system's installation.
If your which ruby shows /usr/local/bin/ruby then the gem env command should reflect the changed path for the version you compiled from source.
I'm curious WHY you would build it manually, when RVM is available to handle all the configuration and installation, and largely remove any concerns about where things are and whether you've just stomped on the system's installed version.
When RVM has installed a Ruby version, it will all be in ~/.rvm and your Gems will be nicely located there too. You'll be able to manage the gems as gemsets, relative to each version of Ruby, and switch back and forth instantly. Or, even better, you can run a command/program in each version of Ruby you have installed to test them using rvm ruby 'some command'.
Notice in the above gem env output that gems are in three separate areas on the disk. Under RVM's control they're in RVM's sandbox:
- GEM PATHS:
- /Users/greg/.rvm/gems/ruby-1.9.2-p0
- /Users/greg/.rvm/gems/ruby-1.9.2-p0#global
That makes it trivial for me to back them up, or blow them away if I want to.
I used to compile my rubies from source on my Macs and Linux boxes. I use RVM for that now. It's so much better than doing it by hand.
I believe the standard gem install path on OS X is:
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/
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