62

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.

m81
  • 2,217
  • 5
  • 31
  • 47

3 Answers3

120

You can try:

gem install --user-install gem_name
d4nyll
  • 11,811
  • 6
  • 54
  • 68
Tho Nguyen
  • 1,216
  • 2
  • 8
  • 4
  • 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 '17 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 01 '18 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 – Alexey Shrub Aug 29 '19 at 08:25
  • Also see https://guides.rubygems.org/faqs/#i-installed-gems-with---user-install-and-their-commands-are-not-available. – sschuberth Jan 23 '22 at 11:44
  • to make it permanat. add this line to your `~/.gemrc` file. `gem: --user-install` – Oshan Wisumperuma Nov 04 '22 at 06: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
Lesmana
  • 25,663
  • 9
  • 82
  • 87
jkeuhlen
  • 4,401
  • 23
  • 36
  • adding "--force" worked for me. gem install --force --local *.gem – Biswajit Maji Nov 22 '17 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) – information_interchange Jun 19 '18 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 '18 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.

rholmes
  • 4,064
  • 3
  • 25
  • 34
  • 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 '15 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 '15 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 '15 at 20:28
  • @jkeulen has an answer that looks good, give it a try (away from computer now). – rholmes Jul 23 '15 at 20:28
  • RVM is definitely great - used it plenty of times. Will try @jkeuhlen's solution now – m81 Jul 23 '15 at 20:32
  • Turns out that I need I higher version of Ruby to install it. Anyhow, thanks for the tips! – m81 Jul 23 '15 at 20:34
  • Do you need a higher version of Ruby for RVM or for the local install? So I'm thinking that some of RVM's requirements must have needed root... I'm usually doing Homebrew on Mac so root's (almost) never needed, and when I'm on Linux I'm usually always root ;-) – rholmes Jul 23 '15 at 21:39
  • @rholmes I needed a higher version of Ruby for the local install - it turns out the computer is running CentOS 6.6 but only has ruby 1.8.7 installed -_- (and I need at least 1.9.3). I don't have root privileges, but I'm pretty sure I can install RVM without it - guess I'll have to find out the hard way! – m81 Jul 27 '15 at 19:32
  • 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 '15 at 19:35
  • It was a while ago but I've installed it (both as root and locally) on CentOS 5.x. – rholmes Jul 27 '15 at 19:41
  • I tried it! Installing RVM worked fine - but then I tried to do `rvm install 2.2.1` and got a number of error messages when it tried to automatically install the required packages (libyaml-devel, autoconf, readline-devel, libffi-devel, automake, libtool, bison, sqlite-devel...). As it turns out, I need to be in the `sudoers` file to actually install those things :( but really, thanks for all the help! – m81 Jul 27 '15 at 20:05
  • 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 '15 at 20:29