63

I have limited privileges on a shared machine I'm using, so I can't install gems the way I'm used to. For example:

$ gem install request-log-analyzer
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions into the /usr/lib/ruby/gems/1.8 directory. 

Is it possible to install a gem locally? (if it matters, I'm trying to install this gem.)

I saw a number of posts on Stack Overflow that talked about using Bundler or gemfiles but I'm not installing this gem for a Ruby on Rails project - I just want to use it in isolation.

I'm running Linux, not sure which flavor though.

3 Answers 3

122

You can try:

gem install --user-install gem_name
5
  • 9
    This is the correct answer, at least in ruby 1.9.3. The accepted answer is confusing because it mentions both installing a gem FROM a local file using the --local flag and TO a local directory.
    – jdlourenco
    Feb 12, 2017 at 23:13
  • 4
    I also had to add the -n~/bin option because it was still trying to link an executable into /usr/local/bin. In full: gem install --user-install -n~/bin gem_name.
    – mbauman
    Mar 1, 2018 at 18:57
  • @MattB. thanks, it really right and full answer, for example in some linux distributives .local dir used for user's software and ~/.local/bin/ already in PATH Aug 29, 2019 at 8:25
  • Also see guides.rubygems.org/faqs/….
    – sschuberth
    Jan 23, 2022 at 11:44
  • to make it permanat. add this line to your ~/.gemrc file. gem: --user-install Nov 4, 2022 at 6:53
33

Add the --local flag to your install:

gem install --local request-log-analyzer

If that causes any problems, try downloading the gem manually and pointing gem directly to it using:

gem install --local path/to/gem/filename.gem

If you want to install it to your user home, as per rubygems:

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.

The command for this would just be

gem install --user-install request-log-analyzer
3
  • adding "--force" worked for me. gem install --force --local *.gem Nov 22, 2017 at 14:03
  • 1
    For people who "just want to install a gem but in their user directory", scroll down to Tho's answer (the --local flag is good to know but not entirely necessary here) Jun 19, 2018 at 15:58
  • 3
    @information_interchange, isn't that what the second half of my answer says? Happy to improve the answer to make that more clear if you have a better idea on phrasing.
    – jkeuhlen
    Jun 19, 2018 at 16:04
2

You could just use RVM: Ruby Version Manager. It is a complete version manager, along the lines of node version manager (nvm) and others, in that it allows you to have different versions of ruby and different collections of gems for each project. It does the job of keeping gems isolated from each other as well as from the system ruby, but at the expense of learning a complete version manager.

When run without root, it installs locally in ~/.rvm and doesn't affect other users.

12
  • Hmm, is there a way to do it without installing rvm? Since this is a shared computer, I'm trying to minimize the number of things that I need to install.
    – m81
    Jul 23, 2015 at 19:36
  • The advantage of RVM is no privilege needed. You could do it "by hand" but it's more complicated.... Setting up bash yourself, etc
    – rholmes
    Jul 23, 2015 at 20:01
  • @mchenja RVM is great and I would recommend you use it. But if you do not want to, I posted a solution for installing locally.
    – jkeuhlen
    Jul 23, 2015 at 20:28
  • 1
    I think that trying RVM would be a low-risk venture; if the documented install isn't satisfactory, you can do rvm implode and if it fails to install (partial install) you can just rm -fr .rvm do delete its contents (and patch your .bashrc / .profile if needed). It's especially low risk if you don't have root ;-)
    – rholmes
    Jul 27, 2015 at 19:35
  • 1
    Yeah no problem- it's a pain not having root! If I run across a workaround I'll shoot it your way!
    – rholmes
    Jul 27, 2015 at 20:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.