On all my gem installs I have to do sudo ? So
sudo gem install rails
will work, while only
gem install
try gem install package --install-dir $HOME/.gem
.
Don't forget setting the path as it was mentioned before export PATH="$PATH:$HOME/.gem/bin"
.
Something you have to consider is changing the PATH order like export PATH="$HOME/.gem/bin:$PATH"
. It might happen for example if you are trying to install rails into your local directory on a OS X system but there is an executable alredy built in. In this case if you don't change the path order you will also receive the following output:
Rails is not currently installed on this system. To get the latest version, simply type:
$ sudo gem install rails
You can then rerun your "rails" command.
And setting the variable GEM_HOME
like export GEM_HOME="$HOME/.gems"
For Mac users, this works for me...
.bash_profile
For example, nano ~/.bash_profile
and add export GEM_HOME=/Users/Michael/.gem
where the path is to your own Home folder
Also in .bash_profile
, add export PATH="$GEM_HOME/bin:$PATH"
Source: http://michaelehead.com/2016/02/06/installing-gems-without-sudo.html
MacOS
brew install ruby
edit ~/.zshrc
for zsh (for Bash could be something like ~/.bash_aliases
)
# By default, binaries installed by gem will be placed into:
# /usr/local/lib/ruby/gems/2.6.0/bin
export GEM_HOME="/usr/local/lib/ruby/gems/"
export PATH="$GEM_HOME/bin:$PATH"
When you install them without sudo, Ruby doesn't know where they get installed to. I can't remember where it installs them by default, probably somewhere like ~/.gems or something. Anyway, you can tell Ruby that's where they're installed by setting the GEM_HOME environment variable.
$ # assuming your gems are stored in ~/.gems
$ GEM_HOME="$HOME/.gems" ruby my_program.rb
If that works, then you might put it in your ~/.bashrc (there are a number of possible files this could go in, depending on your system)
I resolved my issue in the following way: (I have ubuntu 17.10)
Open terminal and type:
echo "gem: --user-install" >> ~/.gemrc
Then add ruby's bin path in .bashrc file
export PATH="$PATH:$HOME/.gem/ruby/x.x.x/bin"
You should install Ruby Version Manager. It stores installed rubies and gems in your home dir, so now you don't have to use sudo
to install gems. It has a lot more features besides this. :)