Rails 6: Cannot install pg on mac m1 - ruby-on-rails

I tried installing the gem pg on my mac m1 but am getting this error:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.
I installed postgresql with homebrew and can connect on a database with the command psql. This means I have postgres install on my machine but the gem pg doesn't work on rails.

The simplest solution is to use sudo and force permissions, however it may not be the best solution.
https://www.moncefbelyamani.com/the-definitive-guide-to-installing-ruby-gems-on-a-mac/
Looks like a good article, you should probably go with https://www.moncefbelyamani.com/the-definitive-guide-to-installing-ruby-gems-on-a-mac/#ruby-manager

If you have installed PostgreSQL with Homebrew, that means you have the write permission to the related directory, as long as you execute the command (be it rails-related or pure Ruby) with the same privilege as you installed PostgreSQL. So, if you have installed PostgreSQL with sudo, then you should run sudo gem install pg or whatever, using sudo. Or, if you have switched your account to an administrator who controls Homebrew software, then you must do the same in running gem install pg (note that this latter case can be complicated in terms of permission if you combine it with a Rails project installed under a user's directory).
Personally, I use rbenv (Github) for the version control software of Ruby (or, a popular alternative rvm should work fine, too), while Homebrew installation is limited to administrators only in my environment. With rbenv, all the Ruby libraries are under each user's control, as everything (but external software like PostgreSQL) is installed under the user's Home directory. This sort of permission-conflict (which really is a pain as I experienced countless times with Rails in the past!) would not happen if you use rbenv (or rvm).

Related

Trying to Install MySQL with/for Ruby on Ubuntu, keep getting an error

I am using Ubuntu 22.04.1 LTS, and I installed Ruby and Rails via rbenv as reccomended by others and the Ruby docs. I am trying to setup a MySQL server to use with an application and I first need to gem install it. Even when signed in as root, I keep getting the following error when issuing the command
gem install mysql2
error shown
I've tried un-installing and re-installing Ruby, and I've tried changing the permissions of the gems folder. Permission changes don't seem to be permanent

rails wont install on my mac, filepath error? gems error

I cant install rails with the usual commannd it gets ignored on my mac. However it seems to be installed according to the terminal output.
Answer can be found by typing
sudo gem environment
then
gem environment
and comparing the results. (gem env is a short version of the command)
Ruby gems are installed locally in directory under specific ruby version, which is also installed in user specific folder (not sure bout MAC situation, but that's the case on my Ubuntu).
Please see the difference on my machine (I am using rvm - ruby version manager):
with sudo - INSTALLATION DIRECTORY: /var/lib/gems/2.7.0
without sudo - INSTALLATION DIRECTORY: /home/le-hu/.rvm/gems/ruby-2.7.2
Using sudo in this case causes the gem to install under root user, not your regular user profile, this leads into gem missing while trying to invoke the gem script being logged in as not-root user.

How to manage ruby gems in linux?

As I have started using Linux (Ubuntu) to broaden my knowledge, but the flow for ruby (rails) development does not feel so smooth as on Mac OSX.
Do you use rvm to manage ruby gems in Linux?
One particular issue I face is that I could install rails with only sudo command. But this forces me to use sudo for all the time under my rails project.
P.S.
Might be I am missing some point with the way you do things in Linux so it is not related to ruby (or rails) at all.
It is preferred to use rvm. we can install ruby and rails without rvm as well, but we can have only one version at a time. You can find the instructions to install rvm from following link - rvm installation.
If you are the only person working on ruby rails, install in single user mode(doesn't need to go with sudo)
Installing ruby and rails only using superuser privileges means you're installing it from distribution packages. That's not recommended, and you get unneeded overload writting sudo before any rails command.
Install rvm or rbenv in your home directory and things should be smooth.

How do I install Ruby gems when using RVM?

I set up RVM and used it to install Ruby and a few other libraries. As I was going through various tutorials and set-ups of other technologies like Rails, I began getting confused about what I should do via RVM and what I should just do as the tutorials suggest.
One example is the RubyGems tutorial here:
http://rubygems.org/pages/download
Should I download that tar file they are talking about? Seems unnecessary since that is what I thought RVM was for. Do I even need RubyGems? What is that for really?
Also, how do I actually get Rails? Is there a precise RVM command to actually download and install Rails?
It helps me to think of RVM as a layer of abstraction between you and the ruby ecosystem.
Without RVM: ruby, gems, and ruby related binaries (like rake, spec, gem, etc) are all installed directly into your operating system directories.
With RVM: ruby related stuff is intercepted by rvm so that ruby, gems, and ruby related binares are "installed" into ~/.rvm dir in a nice, clean, organized way. RVM sits between ruby, gems, and related binaries and the operating system. It provides a way to have multiple ruby environments (with different gems and binaries) on the same machine.
So, no matter whether you have rvm installed or not, you should be able to run the commands almost exactly(*) as they appear in any tutorials out there on the web. In other words, you can sort of "forget" that RVM is installed; the ruby ecosystem should work just as if it wasn't installed.
So, yep, you're gonna have to run gem install rails, etc.
Hope that helps clear the confusion.
(*) There are some small differences. For example: you shouldn't run commands as sudo when RVM is installed.
Should I download that tar file they are talking about?
No. Ruby 1.9+ includes gems. RVM retrofits it for 1.8+.
In general, be careful with any directions you find on the internet explaining how to install anything, unless you have enough experience to understand completely what they want you to do. In particular, any time they want you to install something using sudo or as root.
Specifically, when working with RVM, you do NOT want to use sudo to install Ruby, or any gem. RVM works by setting up a sandbox for your development, and relies on your account's environment, modifying your path so any Ruby requests go to the currently selected RVM-managed Ruby or gems or any commands they install. sudo pushes your normal environment to the side, substituting root's temporarily, installs whatever you asked it to do with root's permissions, then reverts to your environment.
When you go to run the command, or find the gem, as you, it can't be found by RVM's Ruby, because the file was installed outside RVM's sandbox, or, it can't be read or modified, because it's owned by root. Whatever the actual cause, the end result will be weeping and gnashing of teeth.
RVM doesn't subvert the gem functionality. gem is used to install and manage Ruby gems, and RVM tweaks it to use the sandbox for all its machinations. You get added functionality because of RVM's support of gemsets, but gem works as it always has, only it has "big brother", RVM, controlling its world.
No need to install rubygems. RVM should have already installed rubygems. RVM is (in my opinion) useful for managing different installations of ruby (say 1.8.7 and 1.9.2) or different gemsets. If you just have one version of ruby and don't care about different gemsets, RVM isn't really that much of a change. If you want to just install rails, just use gem install rails. If you have an existing rails 3 app, install bundler first gem install bundler and then bundle install to get rails and other gem dependencies.

no such file to load -- rails/cli under Ruby Enterprise Edition 1.8.7

On a VPS I tried to install Ruby. Because of memory exhaustion problems, we were unable to compile ruby 1.9.2 , and we decided to give Ruby Enterprise Edition a try. This compiled without complaining about memory problems.
Next, we install rubygems, and to test that it's working, I installed haml. It worked.
The next step was to install rails, via gem install rails. The version it downloaded was the latest, 3.0.5. Installation went ok. However, when I try to create a new project, I receive:
no such file to load -- rails/cli
Looking around on the internet, people said this is a problem related to older versions of rails, and some of them suggested to do a gem clean. I didn't have any other version of ruby before, therefore, no other version of rails. But, just to be sure, I did a gem clean and then reinstalled rails. The problem persists.
How can I fix this?
Make sure your running the gem binary that Ruby Enterprise Edition installed. So rather than:
gem install rails
it would be something like:
sudo /opt/ruby-enterprise-X.X.X/bin/gem install rails
To save having to type the whole path each time, you can symlink the REE binaries with something like:
sudo ln -sf /opt/ruby-enterprise-X.X.X/bin/* /usr/bin/.

Resources