gem install permission problem - ruby-on-rails

qichunren#zhaobak:~> gem install hpricot
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions into the /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8 directory.
Current login user is qichunren, and qichunre user have write permission with .gem dir.I would like to know why gem not install files into my home .gem dir first? Why my gem common first want to install files into /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8

Try setting GEM_HOME and GEM_PATH to ~/.gem,
For the current terminal session, just type:
export GEM_HOME=~/.gem
export GEM_PATH=~/.gem
If you want these to be set whenever you open a terminal, add the above commands to your ~/.bashrc file.
For a more comprehensive solution to setting up a custom ruby environment, see this tutorial from Site5KB, which describes using a .gemrc file.

For a systemwide Ruby install, become root. For example:
$ sudo gem install hpricot
However, the modern approach in many circumstances, including in development, is to use a tool that lets you easily install and use Ruby as a normal user. This lets you avoid having to become root. There are a few such tools, and the one I use is RVM.
# install rvm into your ~
$ \curl -sSL https://get.rvm.io | bash -s stable
# install latest version of ruby into your ~
$ rvm install ruby
# installs a gem into your ~
$ gem install $SOME_GEM_NAME

I was getting this error on my shared server through 1and1 hosting.
my solution was adding the --user-install option, which just installs it for your logged in user (which is all you need in a shared server environment)
example; installing sass
gem install sass --user-install

If you're using rbenv and this is happening, you need to add the following to your .bash_profile:
export RBENV_ROOT="$HOME/.rbenv"
if [ -d $RBENV_ROOT ]; then
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"
fi

re-install ruby resolve my problem.
brew install ruby

Related

rails new <app-name> requires password for gems installation in ./vendor/bundle

Your user account isn't allowed to install to the system RubyGems.
You can cancel this installation and run:
bundle install --path vendor/bundle
to install the gems into ./vendor/bundle/, or you can enter your password
and install the bundled gems to RubyGems using sudo.
Password:
If it says that you don't have the priviliges, then you just need to run it with sudo
sudo gem install bundler --no-rdoc --no-ri
but I really recommend installing rbenv or rvm, to have a better control of the ruby versions and with that you don't need password or sudo command to install the gems.
I will guide you to install rvm here, this is for personal preference, but you con install rvenb too with the same result.
before anything else, you have to remove completely the ruby version that you have installed on this moment and all the gems
gem uninstall --all # maybe you will need sudo here
sudo apt-get purge ruby
first install mpapis public key
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
then install rvm
\curl -sSL https://get.rvm.io | bash
then add to .bashrc
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
add to .bash_profile
source ~/.profile
then you can install the ruby version that you want (i am using 2.3.0 here as an example, change for the version that you were working before on the app)
rvm install 2.3.0
with this you have installed ruby, I recommend to run this command if you would not use the documentation of gems in local
echo 'gem: --no-document' >> ~/.gemrc
then you can set the ruby version that you want to use, you have different options, select it manually everyime you open the terminal with
rvm use 2.3.0 # or the version that you want to use in that moment
or add to the gemfile the ruby version and rvm will select if for you everytime you make a cd to the path of the app adding this to your gemfile
source 'https://rubygems.org' # this is by default on your gemfile
ruby '2.3.0' # this is the line you need to add, change for the version that you want on he app
when you have the selected version of ruby, you have to install bundler gem, you need to run this just once by ruby version that you install
gem install bundle
then you can run bundle install on your app and will work like a charm.
With rvm you can have different versions of ruby on rails installed on your machine, an as I said, per version that you install you need to install the bundler gem once.
if you have differents version of ruby installed, I recommend that you add a default so if you haven't set the ruby version on the terminal in that session, it will take one by default, you can do it like this
rvm --default use 2.3.0
hope that this helps you to be clear.

Issue creating new Rails App

When I create a new rails app using rails new Test, I am getting the following error.
run bundle install
Your user account isn't allowed to install to the system Rubygems.
You can cancel this installation and run:
bundle install --path vendor/bundle
to install the gems into ./vendor/bundle/, or you can enter your password
and install the bundled gems to Rubygems using sudo.
I have rbenv -v rbenv 0.4.0 and rails -v Rails 4.2.4
How can I fix this so I can create a new rails app without this issue?
Run
bundle install --path=vendor/bundle
That will install the Gems inside the vendor/bundle directory of your project vs the system gems directory. This is a nice way to keep your gems isolated between apps vs polluting your system gems. Just my 2 cents.
Make sure your user belongs to the RVM group
sudo usermod -a -G rvm myUserName
Run gem env. You should see a list of paths like so under GEM PATHS:
/Library/Ruby/Gems/2.0.0
/Users/sam/.gem/ruby/2.0.0
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0
Open your ~/.bashrc file and add the paths as follows:
export GEM_HOME=/Library/Ruby/Gems/2.0.0
export PATH=$PATH:$GEM_HOME:/Users/sam/.gem/ruby/2.0.0:/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/gems/2.0.0
Open a new shell and run the gem install rubygems-update.
Everything should run fine now without sudo.
Running:
rails new test
will not work for an application name anyway because it is a reserved rails word. You would want to go with something like testapp

Unable to use GEM_HOME in radrails

I have setup Radrails on my linux machine. I dont have root privileges on this machine and I also cant edit the ruby installation folder. I have set GEM_HOME and GEM_PATH to a location where I have privileges. I am running radrails from the terminal where I have set these variables. Does Radrails is recognize the gem location?
Also I am not able to start the Webrick server using Radrails. The server always is in stopped state and the console output is blank. I am not able to fix this since I dont see any errors.
thank you!
Use rvm or rbenv to change ruby and gem space to another one. If you will begin usage of the them do the following:
Install rvm with ruby:
$ \curl -sSL https://get.rvm.io | bash -s stable --ruby
or install rbenv, and then install ruby, and make it global:
$ \curl https://raw.githubusercontent.com/fesplugas/rbenv-installer/master/bin/rbenv-installer | bash
$ rbenv install 2.1.4
$ rbenv global 2.1.4
Install the rails without documentation into the general gem space:
$ gem install rails --no-ri --no-rdoc
Enter to the project, create two files .ruby-version with just installed version of ruby (in example 2.1.4), and .ruby-gemset with name of your project:
$ cd project-folder
$ echo "2.1.4" > .ruby-version
$ echo "your-project-name" .ruby-gemset
Fix Gemfile with newly intsalled version of ruby adding a line:
ruby '2.1.4'
Reenter to the project folder, and rvm will generate its wrappers:
$ cd .. ; cd project-folder
Issue gem installation:
$ bundle install

Why Bundle Install is installing gems in vendor/bundle?

Whenever I do bundle install all of the gems get installed at
app_dir/vendor/bundle
path and consumes loads of disk space. I also tried installing gems where it should get installed i.e gemsets while development by this:
bundle install --no-deployement
but this isn't working for me and installeing gems at vendor/bundle. How can I make it to be installed globally for all applications or in ruby gemsets location ? I also tried removing .bundle/config but nothing changed.
I am using:
rvm version: 1.23.14
ruby version: 2.0.0-p247
rails 3.2.13
Here is my ~/.bash_profile:
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
eval "$(rbenv init -)"
alias pg='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log'
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
My ~/.bashrc:
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
Some other information that you might need:
aman#Amandeeps-MacBook-Pro ~/Projects/qe (develop)*$ which bundle
/Users/aman/.rvm/gems/ruby-2.0.0-p247#global/bin/bundle
aman#Amandeeps-MacBook-Pro ~/Projects/qe (develop)*$ rbenv which bundle
/Users/aman/.rbenv/versions/2.0.0-p247/bin/bundle
amandeep#Amandeeps-MacBook-Pro ~/Projects/qe (develop)*$ rbenv which ruby
/Users/aman/.rbenv/versions/2.0.0-p247/bin/ruby
aman#Amandeeps-MacBook-Pro ~/Projects/qe (develop)*$ rbenv gemset active
rbenv: NO such command `gemset'
aman#Amandeeps-MacBook-Pro ~/Projects/qe (develop)*$ which rails
/Users/aman/.rvm/gems/ruby-2.0.0-p247#global/bin/rails
I tried this also but didn't helped:
bundle install --system
and removing .bundle directory.
Please help me in installing gems in gemsets not vendor/bundle or a default place.
In your project folder you will have .bundle directory that holds configuration for bundler. try deleting that folder. it should reset the install path for your gems back to system-wide settings.
In the case you just want to edit the install path, opening .bundle/config with your favorite editor should show you the path to vendor/bundle. Removing that line will restore it to defaults without removing other configs you might have.
Also, another less frequent scenario is your system-wide settings being messed up. According to #NaoiseGolden:
I had to delete .bundle from my Home folder (rm -rf ~/.bundle). You can check out your configuration running bundle env
Try installing using
bundle install --system
I think initially the bundle install was run with --path flag and bundler now rememebers that confguration.
From the bundler man page
Some options are remembered between calls to bundle install, and by the Bundler runtime.
Subsequent calls to bundle install will install gems to the directory originally passed to --path. The Bundler runtime will look for gems in that location. You can revert this option by running bundle install --system.
EDIT: As mentioned in comments below, and also otherwise, this installs the gems system wide. In case you are using rvm etc to manage your environment for different apps, check #IuriG's answer mentioned above.
Use bundle env to view paths and bundle configuration
After this set bundle path to ~/.rvm/gems/ruby-2.0.0-p247 like this:
bundle install --path ~/.rvm/gems/ruby-2.0.0-p247
which is global and also you can use your own custom path.
Post this bundle install will never need path again and will always install all of your gems in that directory(~/.rvm/gems/ruby-2.0.0-p247 in my case) for that app not in app_folder/vendor/bundle
Try running bundle env. This will tell you where the path configuration is set.
First of all, acording to your info, it seems that you have installed both rvm and rbenv. Thats a very bad idea. You have to delete one of them (rbenv + bundler works like a charm for me, didnt try rvm).
In regard to your question check .bundle/config in your project, as all the configuration for bundle to that project lies there (if its still deleted, you can create a new one). You migh want to add this line (or change it, if its already there): BUNDLE_DISABLE_SHARED_GEMS: '0' for sharing gems, they go where your BUNDLE_PATH: is set (BUNDLE_PATH: vendor in my case).
For the global configuration file look in ~/.bundle/config
Also this man page could be of use: bundle config
To Install Gem in system wide avoiding path vendor/bundle, just run the following command in project directory
bundle install --system
$ brew install rbenv
$ rbenv install 2.7.6
$ rbenv install -l
2.7.6
$ rbenv global 2.7.6
$ gem install bundler
$ brew install rbenv-gemset
$ rbenv gemset create 2.7.6 mygemset
$ gem env home
/Users/myuser/.rbenv/versions/2.7.6/gemsets/mygemset
$ gem install rails
$ rails new myapp && cd myapp
$ bundle install
$ bundle show --paths
/Users/myuser/.rbenv/versions/2.7.6/gemsets/myapp/gems/actioncable-7.0.1
...
$ bundle config set --local path 'vendor/bundle'
$ file .bundle
$ .bundle: directory
$ cat .bundle/config
---
BUNDLE_PATH: "vendor/bundle"
$ bundle install # Note from time to time you will get some bizarre error "Could not find timeout-0.2.0 in any of the sources". Simply delete Gemfile.lock and run bundle install again.
$ bundle show --paths
/Users/myuser/myapp/vendor/bundle/ruby/2.7.0/gems/actioncable-7.0.4
$ bundle config set --local system 'true' # or you may want to delete BUNDLE_PATH: "vendor/bundle" from .bundle/config
$ rm -rf vendor/bundle
$ rbenv gemset active
mygemset global
$ bundle install
$ bundle show --paths
/Users/myuser/.rbenv/versions/2.7.6/gemsets/mygemset
All in all, when you install a ruby version manager, such as the preferable rbenv, then it will install gems according to its setup. However, if you update .config/bundle in your project to use 'vendor/bundle', then that's where 'bundle install' will install gems. Sometimes this is used to keep project specific gems, instead of using a gemset tool like rbenv-gemset. Other times it is required for deployment, such as deploying to AWS Lambda on its ruby2.7 runtime. The use of 'vendor/bundle' can be easily removed by removing its reference from .config/bundle as shown above. Once it is removed, it will default to the gem environment that exists globally, which in the above case is managed by rbenv.

Error during Installing Rails in Ubuntu 10.04

First I installed rvm for multi-user using script
\curl -L https://get.rvm.io | sudo bash -s stable
and I added users to rvm group.
and rvm seems worked fine. so I installed ruby 1.9.3 and set 1.9.3 as default
and now I tried to install rails with command
gem install rails
It seems worked fine, but when fetching json-1.7.6.gem and an error occurs.
ERROR: Error installing rails:
ERROR: Failed to build gem native extension.
/usr/local/rvm/rubies/ruby-1.9.3-p374/bin/ruby extconf.rb
creating Makefile
make
sh: make: Permission denied
Gem files will remain installed in /usr/local/rvm/gems/ruby-1.9.3-p374/gems/json-1.7.6 for inspection.
Results logged to /usr/local/rvm/gems/ruby-1.9.3-p374/gems/json-1.7.6/ext/json/ext/generator/gem_make.out
So I thought it would be related with permission, so I tried
sudo gem install rails
but then this error occurs.
sudo: gem: command not found
What should I do?
use sudo as follows..
sudo gem install rails
Updated Answer:
our $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.
you can use rvmsudo to run sudo commands. But you should really be using Gemfiles to install gems using Bundler.

Resources