Where is the default Gem.dir path set? - ruby

This is really odd: I installed ruby 1.9.1 using a "191" suffix so that it will coexist with other versions. e.g. the executables look like ruby191, gem191 and the library directory where all of the vendor/site code lives is /usr/local/lib/ruby191. The version of RubyGems (1.3.1) that is included with ruby 1.9.1 works fine. The installation was performed with:
cd /usr/local/src/ruby-1.9.1-p429
./configure --prefix=/usr/local --program-suffix=191
make
sudo make install
However, when I upgrade my RubyGems installation with sudo gem191 update --system it appears to forget about all of the installed gems. When I checked it out with gem191 environment it is now looking in /usr/local/lib/ruby.
I know I can create a ~/.gemrc and change the location there, but that smells like a hack to me. I have spent a couple of hours trying to figure out where Gem.dir is set in the code, but have been unable to find it. Does anyone know where the default location is set?

Apparently the Gem engine didn't work properly under ruby 1.9.1, but has been fixed under ruby 1.9.2. After installing the latest build, all of my Gem issues have disappeared.

You should definitely use rvm gem for multiple ruby installations on one system.
P.S. You can view your gem installation path by using $ gem{suffix} env.

Related

Cleaning gems from older gem version

I recently upgraded my distro (Ubuntu from 16.04 to 18.04), and I guess either on the upgrade or on the regular sudo update/upgrade calls gem was upgraded and I currently have the 2.7.6 version. I barely use Ruby lately, but just now I had to run a jekyll/bundle command which returned me that it had a bad interpreter (it was looking for ruby2.3 and it's currently installed on the 2.5 version).
My next step would be updating the jekyll and bundle gems (so they update the interpreter version), but to my surprise they aren't listed when I run gem list. They were obviously installed since there's a /usr/local/bin/jekyll executable and a /usr/local/bin/bundle one as well. But I think when gem was upgraded it started checking gems on a different folder (/var/lib/gems/2.5.0 I'm guessing) but jekyll/bundle apparently are in /var/lib/gems/2.3.0. I'd be fine just installing jekyll/bundle on the 2.5.0 version, but is there a way to remove all gem files from the older version since they are useless now?
I highly recommend you to use a ruby version manager like rvm or rbenv to manage different ruby versions installed on your system.
If you just want to remove the gems from your disk, you can find the location of each gem with the command bundle show $gem_name and delete the parent directory to delete all of them. You may need to delete the Gemfile.lock as well to reset the locked gem versions.
Here's what I recommend:
Use rbenv for multiple Ruby version management, no customizations needed
a ruby installer plugin is now included with rbenv
it also handles ruby executable shims automatically, don't need to rbenv rehash anymore
it loads really fast (rvm has a noticable load time on shell startup)
Use bundler to dynamically resolve gems at runtime (options below)
it's fast enough anyways
don't need a special gem solution, bundler comes included /w Ruby now
Options to invoke bundler dynamically (I recommend the last one):
use bundle exec in front of every ruby executable
variant: create alias be='bundle exec'
create bundle binstubs <LIST GEM EXECUTABLES YOU WANT> for each project
use bin/ in front of every ruby executable to call the binstubs
do #2 and then set up .git/safe
lets you manually allow PATH lookups to the bin/ folder while in that project root
don't need to type bin/ anymore
Now multiple gem versions will all be installed into the same Ruby version bucket, and you let bundler dynamically add the right versions to the load path before every startup.
Removing a Ruby version will also mean removing all the gems (and versions of those gems) installed for that Ruby version as well.
I'd like to thank both answers. I upvoted them because even though they're not a direct solution to my problem they give good directions to prevent it.
NOTE: These are the steps I took, which doesn't mean they are the correct way, so don't take more as a reference than a guide.
Here's what I did to clean up those old Ruby version gems:
First I needed to add an environment variable for gem to be able to find those obsolete gems, by running GEM_PATH=/var/lib/gems/2.3.0.
Then it's possible to get the contents and specification of the gems with gem contents name_of_gem and gem specification name_of_gem.
I individually uninstalled those gems that were obsolete with sudo GEM_PATH=/var/lib/gems/2.3.0 gem uninstall -i /var/lib/gems/2.3.0 name_of_gem. In my case I did a sudo install, so I needed sudo which also required me to set the environment variable again because of sudo's security policy. Also, I needed to manually set the install directory for some reason.
Some gems will have executables, and the uninstall will ask if you want to remove it. I wasn't able to get the gem uninstall to remove it because it claimed I didn't have write permission to /var/lib/gems/2.3.0/bin (which is weird because the EXECUTABLE DIRECTORY gem environment variable was /usr/local/bin). I asked gem not to remove the executables, wrote down their names and removed them manually from the EXECUTABLE DIRECTORY folder. You can also use which gem_executable_name to find out where it's located.
After uninstalling all the gems I believe it's safe to remove the /var/lib/gems/2.3.0 folder and its contents. Running gem contents on all of them only returned me files on this folder so I believe the only external files were the script/executables that were added to /usr/local/bin.

Ruby Gems suddenly stopped working on ubuntu 12.04

After a couple of days of not doing pretty much anything on the ubuntu box, I decided to try out some ruby stuff. For this, I wanted to fire up pry. Unfortunately, I was presented with
Sorry, you can't use Pry without Readline or a compatible library.
Please gem install rb-readline or recompile Ruby --with-readline.
~/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
`require': libreadline.so.5: cannot open shared object file: No such
file or directory -
~/.rbenv/versions/2.1.1/lib/ruby/2.1.0/x86_64-linux/readline.so
(LoadError)
Being quite new to linux in general, I figured I'd just do as it said, and install this rb-readline. The installation passes just fine, but doesnt seem to do anything at all. If I do gem list p, the package just doesn't appear.
So yeah, I just don't know what to do to get it working again, and the only thing I managed to find by searching was related to an installation without rbenv and had something to do with sudo etc.
Any ideas?
Without understanding the problem and following the advice in option 2 of the message:
Sorry, you can't use Pry without Readline or a compatible library.
Possible solutions:
* Rebuild Ruby with Readline support using `--with-readline`
* Use the rb-readline gem, which is a pure-Ruby port of Readline <==== Option 2
* Use the pry-coolline gem, a pure-ruby alternative to Readline
I added the gem into the Gemfile (as follows), bundled and pry was then available.
group :development, :test do
gem 'pry'
gem 'rb-readline'
end
I had this problem too. I am using rbenv and reinstalling ruby via
rbenv install -f 2.2.3
did fixed it for me. Of course you'd put in your respective version. -f forces the installation even though you already have that specific version installed. use rbenv global to find out what version you have installed and set.
I have some problem. But I don't want to add gem 'rb-readline'. So try it...
$ln -s /usr/local/opt/readline/lib/libreadline.8.0.dylib /usr/local/opt/readline/lib/libreadline.7.dylib
Do a sudo apt-get install libreadline-dev, seems like you're missing the readline shared library that pry is wanting. If it worked before, not sure why the library isn't there anymore.
I ended up doing rbenv uninstall, followed by rbenv install and re-installing all the gems, and got it back to working. Still no idea what caused it in the first place, but it works now.

rbenv clobbering my gem INSTALLATION DIRECTORY when using system

I want to use rbenv for setting ruby versions on a project per project basis. Otherwise, I want to use my default system ruby setup for everything else.
The problem I am having is the even though rbenv version returns system, gem command still uses the rbenv shim (.rbenv/shims/gem). So when I do gem environment my INSTALLATION DIRECTORY is '/Library/Ruby/Gems/1.8'. I would rather it remains ~/.gem/. I can see it still is that when I run /usr/bin/gem enviroment. I can't figure out where the rbenv shim gem is getting its settings from.
I've tried modifying my GEM_HOME both in my shell, and changing it in ~/.gemrc but that has no effect. I've been searching around with no luck. Hopefully someone here can help. Just some pointers to where the rbenv gem shim is pulling its values from would be helpful.
thanks
Since rbenv shims are pretty high in your PATH, their purpose is to intercept any invocation of ruby, gem, and similar, even if the currently selected Ruby version is "system". You shouldn't be worried about that.
Now, the default installation path for your system Ruby will always be /Library/Ruby/Gems/1.8. Here's my RBENV_VERSION=system gem env:
- GEM PATHS:
- /Library/Ruby/Gems/1.8
- /Users/mislav/.gem/ruby/1.8
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
However, if you don't use sudo (and you shouldn't), gem install won't have write access to that directory, and will install the gems in the next writeable path, which is ~/.gem/ruby/1.8. That seems to be the behavior you want.

Jekyll - command not found

I am trying to get Jekyll running but I have no experience with Ruby.
As far as I can tell the installation of Jekyll has succeeded.
However:
$ jekyll
Gives an error:
-bash: jekyll: command not found
This is the gem env result:
- RUBYGEMS VERSION: 1.3.4
- RUBY VERSION: 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin10.0]
- INSTALLATION DIRECTORY: /Volumes/HDD/DADU/gems
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
- EXECUTABLE DIRECTORY: /Volumes/HDD/DADU/gems/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-10
- GEM PATHS:
- /Volumes/HDD/DADU/gems
- /Volumes/HDD/DADU/.gem/ruby/1.8
- /Library/Ruby/Gems/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/
And I found the following paths leading to "something" Jekyll:
~.gem/ruby/1.8/gems/jekyll-0.11.0/lib/jekyll.rb
~.gem/ruby/1.8/gems/bin/jekyll (exec file)
If you are using MacOS, from the Troubleshooting guide:
Jekyll & Mac OS X 10.11Permalink
With the introduction of System Integrity Protection, several directories that were previously writable are now considered system locations and are no longer available. Given these changes, there are a couple of simple ways to get up and running. One option is to change the location where the gem will be installed (again, using sudo only if necessary):
$ gem install -n /usr/local/bin jekyll
For others coming here with the following set up:
OS X + brewed install of ruby + (possibly) zsh
I figured the problem is that after installing jekyll as per their instructions, gem installs the jekyll gem in the brew cellar, not where the OS usually expects it (somehwere in a gem directory for ruby).
So, all that was needed here was to find out where the brew install of ruby installs gems, locate the jekyll binary, and create a symbolic link to it in /usr/bin.
Here is are the steps I took to fix it:
Type gem env and look for GEM PATHS. For me it was:
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1.
Make sure you can see the jekyll binary in the directory from 1 above and copy its path (if you can't, search any other paths listed in GEM PATHS for it). For me it was:
/usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/jekyll-1.4.3/bin/jekyll
Use the path from step 2 above to create a symlink to /usr/bin/jekyll. I did it by typing this (you might need sudo to create the symlink):
cd /usr/bin && ln -s /usr/local/Cellar/ruby/1.9.3-p194/lib/ruby/gems/1.9.1/gems/jekyll-1.4.3/bin/jekyll jekyll
Now all should be merry if you type jekyll.
The easiest method of doing this is to use RVM. It manages Ruby and all its gems for you and it's easy to use. See this link for using it.
If you did not want to use that you will need to modify your PATH variables so it can find your gems. I have found this to be tedious and reverted to RVM, but here are the general steps.
You will need to find out where your gems are getting installed. If you did gem install ... the gems will be in ~/.gem/ruby/1.8/gems/bin, if you used sudo gem install ... the gems will be somewhere in /System/Library/Frameworks/Ruby.framework/Versions/1.8/Resources
You have to add this path to your PATH variable. Easiest way to do this is by running :
echo 'PATH=$PATH:above/path/to/gems' >> ~/.bash_profile
If you are using RBENV instead of RVM you simply need to run rehash in the command line after installing jekyll:
rbenv rehash
I installed my ruby2.6.0 and gem via brew on MacOS 10.14.
For me, add the following line to my ~/.zshrc solved this issue.
export PATH=/usr/local/lib/ruby/gems/2.6.0/bin:$PATH
I found jekyll executable file with command locate jekyll.
Maybe a little late, but...
I had some trouble to install Jekyll on Ubuntu and tried everything that people answered in this thread - unfortunately nothing worked.
Then, I watched a video on Jekyll's site and after installing the whole ruby package again, sudo gem install jekyll worked.
Try it before anything else:
sudo apt-get update
sudo apt-get install ruby-full
sudo gem install jekyll
jekyll -v
It seems pretty simple, but it works on Ubuntu.
One solution would be editing your ~/.bashrc file and add this line:
PATH=$PATH:~/.gem/ruby/1.8/gems/bin
This will add ~/.gem/ruby/1.8/gems/bin in Bash's lookup path.
Reopen the terminal and it should work. Or you can use the following command:
. ~/.bashrc
Following steps solved my problem
gem uninstall jekyll
sudo gem install jekyll
Open ~/.bash_profile and add this code in the last line,
export PATH=$PATH:/usr/local/lib/ruby/gems/1.9.1/gems/jekyll-2.5.2/bin
Save and close the .bash_profile
Close and reopen the mac terminal, try running jekyll now, it should work
For me, I followed this installation guide instead of their main page's installation instruction. It worked after I changed the bash_profile file and restarted Terminal.
Jekyll is a ruby gem : Ruby gems in linux, for example, are in /var/lib/gems/1.8, as can be seen in the "ruby env" output.
Thus, you need to add the executables in this directory to your path.
In general, if a ruby gem is "not found" by your OS, it simply indicates that either
1) You don't have the gem installed or
2) You don't have the gem installed in a directory that is on your path.
I have found that there have been a few issues with installing ruby and ruby gems on linux (I have found that it can be tricky on Ubuntu v10, and have confirmed this with the Ruby folks on IRC). Thus, tools like RVM or rbenv might be the best approach to setting up a stable, maintainable ruby environment.
Easier than creating a symlink just install it correctly.
If you got permission errors like a lot of people are getting when trying to use
gem install jekyll
instead use
sudo gem install jekyll
#jayunit100,
I'm running into the same issue with a Jekyll blog. I've installed the gem via RVM in a 'Blog directory and the _config.yml file says that it should generate into Blog/_site. Is it as simple as adding Blog to the PATH or is there something else I'm missing?
Update: My bad, I didn't really have the gem installed. Lesson learned: rvm requirements and brew doctor are there for a reason - before you install stuff USE THEM
In my case I had to run bundle install --force
Then bundle exec jekyll serve works, but jekyll serve still doesn't. It seems I'll have to go with the former from now on…
When you use the --user-install option, RubyGems will install the gems to a directory inside your home directory, something like ~/.gem/ruby/1.9.1. The commands provided by the gems you installed will end up in ~/.gem/ruby/1.9.1/bin. For the programs installed there to be available for you, you need to add ~/.gem/ruby/1.9.1/bin to your PATH environment variable.
For example, if you use bash you can add that directory to your PATH by adding code like this to your ~/.bashrc file:
if which ruby >/dev/null && which gem >/dev/null; then
PATH="$(ruby -r rubygems -e 'puts Gem.user_dir')/bin:$PATH"
fi
https://guides.rubygems.org/faqs/#user-install
I put it in the .bash_profile (Mac OS X).
https://hathaway.cc/2008/06/how-to-edit-your-path-environment-variables-on-mac/
Here's an updated answer for 2020 (soon 2021). To install any Ruby gem, whether it's Jekyll, Bundler, Rails, etc., you need a proper Ruby development environment on a Mac. There are various ways to install Ruby on a Mac, as I have written about in great detail in my definitive guide to installing Ruby gems on a Mac. The only one I recommend is to use a Ruby manager because it's the most flexible and sets you up for success for the long term. My preferred one is chruby because it's the lightest and easiest to use. As part of installing Ruby, you also need to properly configure your shell startup file (typically ~/.zshrc or ~/.bash_profile) so that it knows where to find the gems you install. Otherwise, you'll get the "command not found" error, which I've also written about.
Since there are several steps involved in setting up a working Ruby development environment on a Mac, I wrote some scripts to automate the whole process and make things easier and faster for people. You can read more about the scripts in my guide mentioned above.
I had this problem for a very stupid reason, which is that I was working on Linux and had installed both flatpak and .deb versions of Visual Studio Code. I was confused because I could successfully run bundle exec jekyll serve in the terminal application, but not from the integrated terminal in Visual Studio Code.
Well, it turns out the integrated terminal loaded my PATH correctly in the .deb version but not the flatpak one. So... if you, like me, have foolishly installed multiple versions of Visual Studio Code, check which one you are using.
This is what worked for me. I'm not developing in ruby, and don't have a lot of tools that I use it for, so I don't need RVM. I also don't need to install jekyll as root. I just want it to run.
(This answer is just a more descriptive version of answers by Santa Zhang, jayunit100, and a few others.)
1. Find Local Gem Path
> gem env
Assuming this runs, it will list a bunch of useful information. You are looking for GEM PATHS. If there are two, then you want the one that is found in your home directory. Mine was something like /home/<user>/.local/share/gem/ruby/3.0.0
If it doesn't run, install the ruby gem program and try again.
2. Find Gem Binaries Path
Look in that directory until you find the location where the jekyll executable is actually installed. I found it in /home/<user>/.local/share/gem/ruby/3.0.0/bin/jekyll. But what I need is the directory path, not the file, so: /home/<user>/.local/share/gem/ruby/3.0.0/bin/.
3. Add That to PATH
Figure out how to add a directory to your PATH environment variable. Instructions for that are too extensive to provide here, as it depends on your operating system and preferred shell, and the versions of the same. Search for that information elsewhere on Stackoverflow.
4. Restart Your Session
Close your terminal and open a new one. Make sure the directory was added to your path. Depending on how you set your PATH variable, you might need to log out of your session and log in again.

How do I retain installed gems after updating rubygems?

After a recent reinstallation of Ubuntu, I reinstalled RubyGems. The Ubuntu repository grabbed version 1.3.5. Later I found I need the latest version. So I installed the RubyGems update to get to version 1.3.7.
The trouble is, upon upgrading gem list returns only a small subset of all my originally installed gems. In the past, I've solved this by reinstalling all my gems following an upgrade, but this is troublesome and I hope to avoid this workaround now and in the future.
I tried adding an export GEM_HOME to my .bashrc file. It didn't work. gem list still doesn't list all my gems (even though I can find them on my computer in /var/lib/gems/1.8/gems. I also created a .gemrc yaml file and set the gemhome path. When I run gem env I can see that the home path is set, but still gem list doesn't return everything.
What's up with this?
This won't directly answer your question, but a lot of us use rvm to manage Ruby and gems. The benefit is it doesn't require us to mess with the system's Ruby installation and gems, leaving those alone for any OS routines needing a particular version of Ruby or a gem.
Since switching to rvm I've quit installing an updated version in /usr/bin/local too as rvm makes it so easy to keep my development versions separate from the system.
There are great reasons to tweak the system gems so I won't question why you might do so. I'm just tossing out the rvm thang as it might help solve your needs.

Resources