2

I need to install the Ruby gem 'Mechanize' for a class that I am taking and do a project using it. I am using windows and I have tried installing versions 2.6.1 and 2.5.3 with the devkits from the website rubyinstaller.org. After installing these versions I have then done 'gem install mechanize' and the gem installs correctly. However when I try to use even " require 'mechanize' ", I get a sizable stacktrace and I cannot figure out what is wrong. I have tried to uninstall and reinstall everything multiple times.

The stacktrace:

C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/net-http-persistent-3.0.0/lib/net/http/persistent.rb:205:in `<class:Persistent>': uninitialized constant Process::RLIMIT_NOFILE (NameError)
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/net-http-persistent-3.0.0/lib/net/http/persistent.rb:190:in `<top (required)>'
    from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/mechanize-2.7.6/lib/mechanize.rb:6:in `<top (required)>'
    from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `require'
    from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:135:in `rescue in require'
    from C:/Ruby25-x64/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:39:in `require'
    from test.rb:1:in `<main>'

Any help or suggestions would be greatly appreciated. Thank you!

1 Answers1

3

It seems this is a known windows problem in one of this gem's dependencies, see: uninitialized constant Process::RLIMIT_NOFILE (NameError)

You might try the hack given there to put this line before your require

Process::RLIMIT_NOFILE = 7 if Gem.win_platform?
require 'mechanize'

You could also try running ruby inside some kind of virtualized environment if you are serious about becoming a ruby dev and you must use windows. See Developing in Ruby on Windows

UPDATE: This is a known issue not for mechanize but one of its' dependencies see this issue and the proposed workaround:

Locate the source path of the mechanize gem. You should find it one of the paths shown in the results section from running

 gem env
 #look for a section that says:
 - GEM PATHS:

CD to the path from step 1, then lib/net/http/persistent.rb in your text editor: and modify this:

Find and remove this line:

DEFAULT_POOL_SIZE = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4

Then add the following in it's place:

if Gem.win_platform? then 
  DEFAULT_POOL_SIZE = 256
else
  DEFAULT_POOL_SIZE = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4
end

and save the file. Keep in mind if you use bundler and plan to run bundle update or bundle upgrade you will lose these changes. But this will hopefully be fixed in a some future release. I have not tested this as I don't use windows, but you can try it.

lacostenycoder
  • 8,780
  • 3
  • 23
  • 38
  • This fixed it for me. Windows 10 v1803 x64, using rubyinstaller-devkit-2.5.3-1-x64.exe from https://rubyinstaller.org/ . Thanks! – Dusty Vargas Apr 12 '19 at 22:44