ruby 1.9.2 installation and gems - ruby

I have installed Ruby 1.9.2 from source. But it seems there is some trouble recognizing the bundler gem which I have already installed.
My /etc/environment file:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/ruby/bin"
It looks like my gems are in /usr/local/ruby/gems/1.9.1/gems/.
In my rails application when I attempt to run sudo bundle install I get an error:
sudo: bundle: command not found
Also, why do the directories say 1.9.1?

Type which ruby to see the path your system thinks ruby is in.
Did you use a --prefix=... option for configure when you set up the configuration? Normally Ruby from source wouldn't be in /usr/local/ruby/bin. The Ruby executable would be in /usr/local/bin/ruby, but that is not how you'd set up your PATH to use it, so that looks suspicious. Notice that your path already contains /usr/local/bin so if Ruby installed into the normal location for a source-installation, that path will pick it up and your final search of /usr/local/bin/ruby will be wrong and unnecessary.
If you installed the gem before you installed the new version of Ruby then the gem would be part of the previous installation, not the current one, and wouldn't be visible to it. They're separate installs.
Unless you are trying to do a system-wide install for multiple users there is no real reason to compile from source and allow it to install to /usr/local/bin. I highly recommend installing RVM, then letting it install any Ruby versions into RVM's ~/.rvm sandbox. Gems will also be installed relative to the currently enabled RVM controlled ruby, which is a really good thing.

Related

Remove version of Ruby/RubyGems and gems on MacOS properly

I recently installed a version of Ruby (and RubyGems) via homebrew on my Mac because the RubyGems that comes installed on Mac doesn't let you install gems due to permissions issues. I followed the instructions in the top answer in this thread: You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user)
After installing some gems, I realized that I don't need this version of Ruby and RubyGems anymore, so I ran brew uninstall ruby to get rid of them (I assume RubyGems gets deleted along with Ruby). However, I checked and it appears the gems I had installed are still in /usr/local/lib/ruby/gems/2.7.0. Can I just delete the usr/local/lib/ruby folder, and is this the correct way of going about it? I don't know if the default RubyGems that comes with MacOS uses this folder.
Basically when you run rbenv versions it will display all your ruby versions including the one installed by default (system).
system ruby usually located in /usr/local/lib/ruby/gems/2.5.1
rbenv ruby default location is inside user path ~/.rbenv/versions/2.5.1/gems
to uninstall ruby using rbenv you need to remove the specific version e.g rbenv uninstall 2.5.1
I suggest that you leave your ruby system installation, but you can remove all the gems inside the ruby system path manually /usr/local/lib/ruby/gems/2.5.1.
I'm the author of that top post in the link you mentioned. You are correct that Homebrew installs gems in /usr/local/lib/ruby/gems/2.7.0. You can confirm that in two ways:
You can run brew info ruby, and then visit the Formula page that's displayed: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/ruby.rb
This is the line of code where the Rubygems directory is defined: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/ruby.rb#L48
You can install Ruby again with Homebrew, and you should see this section in the terminal output:
By default, binaries installed by gem will be placed into:
/usr/local/lib/ruby/gems/2.7.0/bin
You may want to add this to your PATH.
I wrote about this in the Homebrew section of my definitive guide to installing Ruby gems on a Mac.
The system Ruby is NOT installed in /usr/local/lib/ruby. It is in /Library/Ruby/Gems/2.6.0. You can safely remove the /usr/local/lib/ruby directory. On a fresh macOS installation, the /usr/local directory is empty.
You can then check if Homebrew is still healthy by running brew doctor.
/usr/local/lib/ruby/gems/2.7.0 used to be brew default install path. Now they have changed it to /opt/homebrew/lib/ruby/gems/2.7.0. So you can delete the previous folder under /usr without a problem.

Ruby Gem Bin Name with Ruby Version Appended

After installing a Ruby gem system-wide, the binary name of the gem is always appended with a ruby version. Am I doing something wrong?
I'm new to Ruby. So I was checking out some ruby vim plugins and some of them require to install some gems. So I installed the gem system-wide using sudo gem install rubocop for example, but after installation I was not able to execute the rubocop bin in terminal. Instead, I had to run rubocop.ruby2.5. Why is the ruby version always appended? How do I get rid of this? Might be a dumb question but I really appreciate some help. I checked a similar post Ruby gem names with appended ruby version but it didn't help me much. The following is the bin name and location. I'm using openSUSE and Ruby 2.5.3.
~ $ which rubocop.ruby2.5
/usr/bin/rubocop.ruby2.5
Whether or not RubyGems appends the Ruby version number to the executable name is governed by the --[no-]format-executable flag to gem install.
If you don't want the version suffix, you can either use
gem install --no-format-executable <somegem>
Or set this flag in one of your RubyGems configuration files.
My guess is that your Linux distribution has this configuration flag set for globally so that gem executables installed for different Ruby versions don't clobber each other.

'require' not working with rvm

The following code lives inside my rails app's root folder and daemonizes/runs myserver.rb.
# myserver_control.rb
require 'rubygems'
require 'daemons' # causes 'no such file to load -- daemons' error since gem reinstall with rvm
#options = {
# options defined
}
Daemons.run('myserver.rb', #options)
It was working just fine until I installed rvm and now it can't seem to find the daemons gem. I have a feeling maybe the above is searching for the daemons gem in a system wide folder somewhere? Instead of being able to use the rvm installed daemons gem? How do I install the daemons gem on a system level where it can find it? OR how do I make it so it can find the rvm installed gem?
Currently I do not even have a 'system' gem set. How would I install gems on the system level after having already installed rvm?
If wishing to use the system ruby and gems you can type in
rvm use system
which will allow to use the system installed ruby and gems, but I think this kinda of defeats the purpose of using RVM I would instead install a default ruby in RVM and install any gems into that. To set a default Ruby after you have installed it, type this
rvm --default use 1.9.2
Edit
Based on comment your problem lies with running sudo, this creates a new subshell and different instance of rvm
RVM uses a concept of gemsets, which are unique groups of gems that you can use, most often specific to ruby versions (although you can make them specific to applications or global).
RVM will change your GEM_HOME when changing ruby versions, which tells ru ygems where your gems are installed. So when you installed RVM, you created a new blank gemset and RVM told rubygems to use that.
This is because gems are not always cross compatible between rubygems.
However, if you just writing rails apps, you should ignore the above for now and use bundler. Place 'gem "daemon"' I am your Gemfile and run bundle update from your app root directory.
Bundle helps you maintain gems on a per app basis, which means your app won't break if you upgrade a gem somewhere else. Using it and knowing how it works is best practices. Good luck.

When installing a gem, should only the library get installed, or should the executable get installed as well?

I have installed RSpec with the gem (gem install rspec) command. Everything went ok. If I try to reinstall it again, I get a message like it has already been installed.
When I try to use rspec from the command line on Ubuntu, the terminal can't find the program.
My question is does the gem command only install the libraries, meaning I need to manually install the rspec executable myself, or is the gem command supposed to install everything, which would mean something went wrong during the install?
I have confirmed that the executable does get installed and should be able to run on it's own.
You can verify that the gem is installed by checking the list of installed gem using : gem query.
You can also check the paths used by gem using: gem env.
This should give you the path to the Ruby version the gems are using, their executable path (so you can update your path) and where they are stored.
Make sure they are configured to use the correct Ruby version (if you use more than one) and the path is in you $PATH variable.

Rails -- gem installation

Quick question about gem installation -- when I use bundle install I know it installs the gems necessary for my individual project, but it doesn't affect other projects on my computer. If I use gem install name_of_gem would that also only affect the current project or would it affect all projects on my computer using rails (a generic installation)? In general I think I am a little confused about how exactly gem installation works, so if your answer could include some general background information to help me understand this that would be great!
Yes, gems are typically installed system-wide, or in your home directory is this is not possible. By default, when your application uses a gem, RubyGems loads the latest installed version. If you want to use a specific version, RubyGems lets you do that with this syntax:
require 'rubygems'
gem 'RedCloth', '3.0'
Bundler is a helpful tool that tracks the versions of a gem that are being used to develop a project, and then allows you to both install them in one fell swoop with bundle install, and also to load those exact versions. The application loads them by loading the Bundler code, which overrides parts of RubyGems to use the versions specified in the Gemfile.
By default, Bundler just calls RubyGems to install gems (again, system-wide or in your homedir). You can ask it to store the gems in a directory called vendor/cache by using bundle package. This lets you "freeze" the gems so that you can distribute them with the source code.
If you want further isolation of your Ruby environments, you should use RVM, which lets you set up isolated gemsets, and in fact, different versions of Ruby, to use on different projects. When you're using RVM, the directory where RubyGems installs things is overridden and is specific to your current Ruby version and gemset.
I'd recommend reading the docs for both RubyGems and Bundler; they're both quite good.
When you do bundle install the gems are installed at rubygems and would be available for all your projects unless you're using RVM and setting up gemsets for your projects.
When you're not using RVM and you do a gem install your operating system is probably going to install the gem at your current user's files (usually ~/.gem), if you sudo install gem it's going to install wherever is the place your system Ruby is installed.
I would really recommend you to setup RVM do manage separate groups of gems and rubies. You can read their website linked above or a simple tutorial I wrote to use it.

Resources