title: 'Failed to build gem native extension' on Windows 7 (The system cannot find the path specified)
body: <h2>The problem in short</h2>
  
  <p>I'm on Windows and am getting the following error when running <code>gem install json —platform=ruby</code>:</p>
  
  <pre><code>The system cannot find the path specified.
  Temporarily enhancing PATH to include DevKit...
  Building native extensions.  This could take a while...
  The system cannot find the path specified.
  ERROR:  Error installing json:
      ERROR: Failed to build gem native extension.
  
      C:/Ruby193/bin/ruby.exe extconf.rb
  creating Makefile
  
  
  Gem files will remain installed in C:/Ruby193/lib/ruby/gems/1.9.1/gems/json-1.8.1 for inspection.
  Results logged to C:/Ruby193/lib/ruby/gems/1.9.1/gems/json-1.8.1/ext/json/ext/generator/gem_make.out
  </code></pre>
  
  <h2>Background and some Investigations</h2>
  
  <p>So first off, I'm not a Windows person so this is a brave new world for me. Having inherited a laptop from work that had a mad collection of libraries spread all over it I've managed to remove all previous installations of ruby and the Devkit and then installed the following:</p>
  
  <ul>
  <li>Ruby 1.9.3p484 with <a href="http://rubyinstaller.org" rel="noreferrer">Ruby Installer</a> into <code>C:/Ruby193</code></li>
  <li>Ruby 2.0.0p353 with <a href="http://rubyinstaller.org" rel="noreferrer">Ruby Installer</a> into <code>C:/Ruby200</code></li>
  <li>Devkit <code>DevKit-tdm-32-4.5.2-20111229-1559-sfx.exe</code> (for ruby 1x) extracted into <code>C:/Ruby193-devkit</code></li>
  <li>Devkit <code>DevKit-mingw64-32-4.7.2-20130224-1151-sfx.exe</code> (32-bit for ruby 2x) extracted into <code>C:/Ruby200-devkit-x32</code>.</li>
  </ul>
  
  <p>I then installed <a href="https://github.com/vertiginous/pik" rel="noreferrer">Pik 0.2.8</a> as a gem and ran <code>pik_install</code> into a new directory <code>C:/bin</code> as per the installation instructions.</p>
  
  <p>My PATH looks like this:</p>
  
  <pre><code>PATH=C:\bin;C:\Ruby193\bin;C:\windows;C:\windows\system32;C:\windows\system32\Wbem;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Java\jdk1.6.0_33\bin;C:\Program Files (x86)\Common Files\Apple\Mobile Device Support\;C:\Program Files (x86)\Common Files\Apple\Apple Application Support;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\TortoiseSVN\bin;C:/inpath;C:\Program Files (x86)\WinMerge;C:\ChromeDriver;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\syswow64
  </code></pre>
  
  <p>The important things being that <code>C:/bin</code> and <code>C:/Ruby193/bin</code> are in the path. This means that ruby 1.9.3 is loaded by default when I fire up a shell and I can successfully switch to 2.0.0 with <code>pik use 2.0.0p353</code>. In other words, pik works fine.</p>
  
  <p>Devkit <a href="https://github.com/oneclick/rubyinstaller/wiki/Development-Kit" rel="noreferrer">is intended</a> to allow the compiling of native C/C++ binaries from gems on Windows, so as to aviod using precompiled windows binaries.</p>
  
  <p>Because I've got two versions of ruby installed, and each requires a different devkit (one for 2x and one for 1x), I had to do the setup for devkit twice:</p>
  
  <pre><code>cd C:/Ruby193-devkit
  ruby dk.rb init
  # Edit config.yml to remove all but Ruby193
  ruby dk.rb install
  
  cd C:/Ruby200-devkit
  ruby dk.rb init
  # Edit config.yml to remove all but C:/Ruby200
  ruby dk.rb install
  </code></pre>
  
  <p>At this point I should have been able to run <code>gem install json —platform=ruby</code> successfully, but got the error above. After a little digging <a href="https://github.com/oneclick/rubyinstaller/wiki/Troubleshooting#wiki-gems_fails_comspec_autorun" rel="noreferrer">I discovered this</a>, which advises checking that COMSPEC is set corectly and removing any AutoRun keys from <code>HKEY_CURRENT_USER\Software\Microsoft\Command Processor</code> – I had one from ANSIcon and duly deleted it.</p>
  
  <p>Unfortunatly I was still unable to install the json gem.</p>
  
  <p>It then struck me that perhaps the wrong version of GCC was being used, or not being found. The two versions of Devkit come with different versions of gcc:</p>
  
  <pre><code>&gt; C:\Ruby193-devkit\mingw\bin\gcc —version
  gcc (tdm-1) 4.5.2
  
  &gt; C:\Ruby200-devkit-x32\mingw\bin\gcc —version
  gcc (rubenv-4.7.2-release) 4.7.2
  </code></pre>
  
  <p>I then wondered if pik wasn't loading the version of devtools (and therefore gcc) for the specific version of ruby that i'd picked, and was always using 1.9.3. Thanks to <a href="http://rubyonwindowsguides.github.io/book/ch02-04.html" rel="noreferrer">this article</a>, it seems that's not the case:</p>
  
  <pre><code>&gt; pik use 193
  &gt; where ruby
  C:\Ruby193\bin\ruby.exe
  
  &gt; cat C:\Ruby193\lib\ruby\site_ruby\devkit.rb
  # enable RubyInstaller DevKit usage as a vendorable helper library
  unless ENV['PATH'].include?('C:\\Ruby193-devkit\\mingw\\bin') then
    puts 'Temporarily enhancing PATH to include DevKit...'
    ENV['PATH'] = 'C:\\Ruby193-devkit\\bin;C:\\Ruby193-devkit\\mingw\\bin;' + ENV['PATH']
  end
  ENV['RI_DEVKIT'] = 'C:\\Ruby193-devkit'
  ENV['CC'] = 'gcc'
  ENV['CXX'] = 'g++'
  ENV['CPP'] = 'cpp'
  
  
  &gt; pik use 200
  &gt; where ruby
  C:\Ruby200\bin\ruby.exe
  
  &gt; cat C:\Ruby200\lib\ruby\site_ruby\devkit.rb
  # enable RubyInstaller DevKit usage as a vendorable helper library
  unless ENV['PATH'].include?('C:\\Ruby200-devkit-x32\\mingw\\bin') then
    phrase = 'Temporarily enhancing PATH to include DevKit...'
    if defined?(Gem)
      Gem.ui.say(phrase) if Gem.configuration.verbose
    else
      puts phrase
    end
    puts "Prepending ENV['PATH'] to include DevKit..." if $DEBUG
    ENV['PATH'] = 'C:\\Ruby200-devkit-x32\\bin;C:\\Ruby200-devkit-x32\\mingw\\bin;' + ENV['PATH']
  end
  ENV['RI_DEVKIT'] = 'C:\\Ruby200-devkit-x32'
  ENV['CC'] = 'gcc'
  ENV['CXX'] = 'g++'
  ENV['CPP'] = 'cpp'
  </code></pre>
  
  <p>(I don't actually have cat available on windows but it makes for a clearer explanation)</p>
  
  <p>As you can see, it looks like the correct version of devkit is being added to the path by devkit.rb, which is obviously being loaded because my error contains 'Temporarily enhancing PATH to include DevKit…'.</p>
  
  <h2>Back to the original error</h2>
  
  <p>It was:</p>
  
  <pre><code>The system cannot find the path specified.
  Temporarily enhancing PATH to include DevKit...
  Building native extensions.  This could take a while...
  The system cannot find the path specified.
  ERROR:  Error installing json:
      ERROR: Failed to build gem native extension.
  
      C:/Ruby193/bin/ruby.exe extconf.rb
  creating Makefile
  
  
  Gem files will remain installed in C:/Ruby193/lib/ruby/gems/1.9.1/gems/json-1.8.1 for inspection.
  Results logged to C:/Ruby193/lib/ruby/gems/1.9.1/gems/json-1.8.1/ext/json/ext/generator/gem_make.out
  </code></pre>
  
  <p>Unfortunatly the results log doesn't exactly offer much in the way of help. This is what gem_make.out looks like:</p>
  
  <pre><code>C:/Ruby193/bin/ruby.exe extconf.rb
  creating Makefile
  </code></pre>
  
  <p>I thought that <code>extconf.rb</code> might offer some help, but I can't make head nor tail of it:</p>
  
  <pre><code>require 'mkmf'
  
  unless $CFLAGS.gsub!(/ -O[\dsz]?/, ' -O3')
    $CFLAGS &lt;&lt; ' -O3'
  end
  if CONFIG['CC'] =~ /gcc/
    $CFLAGS &lt;&lt; ' -Wall'
    unless $DEBUG &amp;&amp; !$CFLAGS.gsub!(/ -O[\dsz]?/, ' -O0 -ggdb')
      $CFLAGS &lt;&lt; ' -O0 -ggdb'
    end
  end
  
  $defs &lt;&lt; "-DJSON_GENERATOR"
  create_makefile 'json/ext/generator'
  </code></pre>
  
  <p>The Makefile in <code>C:/Ruby193/lib/ruby/gems/1.9.1/gems/json-1.8.1/ext/json/ext/generator</code> <a href="https://gist.github.com/dannysmith/8043321" rel="noreferrer">looks like this</a>. It seems odd to me that this Makefile is even being created.</p>
  
  <p>If anybody with a bit more Windows/Ruby experience can shed any light on this it would be amazing!</p>
  
  <p>PS. I'm on Windows 7 Professional SP1</p>
  
  <h2>Update after some more digging</h2>
  
  <p>So I wanted to check that devkit was defiantly enhancing the path with the correct devkit directories. Thanks to a suggestion from another SO question, I moved the devkit installations inside the Ruby directories:</p>
  
  <p>The tdm devkit now lives in <code>C:\Ruby193\devkit</code> while the mingw64 lives in <code>C:\Ruby200\devkit</code>. Having run <code>ruby dk.rb install -f</code> for each devkit, I opened up both devkit.rb files to check that the path's had been updated correctly. They had, and I updated the puts so it should print "Temporarily enhancing PATH do include DevKit for 1.9" or "Temporarily enhancing PATH do include DevKit for 2". By way of confirmation that the correct devkit is being loaded:</p>
  
  <pre><code>C:\&gt;pik 193
  
  C:\&gt;ruby -rdevkit -ve "puts ENV['PATH']"
  ruby 1.9.3p484 (2013-11-22) [i386-mingw32]
  Temporarily enhancing PATH to include DevKit for 1.9...
  C:\Ruby193\devkit\bin;C:\Ruby193\devkit\mingw\bin;C:\bin;C:\Ruby193\bin;C:\windows;C:\windows\system32;C:\windows\system32\Wbem;c:\Program Files (x86)
  \Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Pro
  gram Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Java\jdk1.6.0_33\bin;C:\Program Files (x86)\Common Files\Apple\Mobile Device Su
  pport\;C:\Program Files (x86)\Common Files\Apple\Apple Application Support;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\TortoiseSVN\bin
  ;C:/inpath;C:\Program Files (x86)\WinMerge;C:\ChromeDriver;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\sy
  swow64
  
  C:\&gt;pik 200
  
  C:\&gt;ruby -rdevkit -ve "puts ENV['PATH']"
  ruby 2.0.0p353 (2013-11-22) [i386-mingw32]
  Temporarily enhancing PATH to include DevKit for 2...
  C:\Ruby200\devkit\bin;C:\Ruby200\devkit\mingw\bin;C:\bin;C:\Ruby200\bin;C:\windows;C:\windows\system32;C:\windows\system32\Wbem;c:\Program Files (x86)
  \Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Pro
  gram Files (x86)\Common Files\Roxio Shared\DLLShared\;C:\Program Files\Java\jdk1.6.0_33\bin;C:\Program Files (x86)\Common Files\Apple\Mobile Device Su
  pport\;C:\Program Files (x86)\Common Files\Apple\Apple Application Support;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program Files\TortoiseSVN\bin
  ;C:/inpath;C:\Program Files (x86)\WinMerge;C:\ChromeDriver;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM\Bluetooth Software\sy
  swow64
  </code></pre>
  
  <p>So that all looks like it works correctly. but:</p>
  
  <pre><code>C:\&gt;gem install json --platform=ruby
  Temporarily enhancing PATH to include DevKit for 2...
  Building native extensions.  This could take a while...
  The system cannot find the path specified.
  ERROR:  Error installing json:
          ERROR: Failed to build gem native extension.
  
      C:/Ruby200/bin/ruby.exe extconf.rb
  creating Makefile
  
  
  Gem files will remain installed in C:/Ruby200/lib/ruby/gems/2.0.0/gems/json-1.8.1 for inspection.
  Results logged to C:/Ruby200/lib/ruby/gems/2.0.0/gems/json-1.8.1/ext/json/ext/generator/gem_make.out
  
  C:\&gt;pik 193
  
  C:\&gt;gem install json --platform=ruby
  Temporarily enhancing PATH to include DevKit...
  Building native extensions.  This could take a while...
  The system cannot find the path specified.
  ERROR:  Error installing json:
          ERROR: Failed to build gem native extension.
  
      C:/Ruby193/bin/ruby.exe extconf.rb
  creating Makefile
  
  
  Gem files will remain installed in C:/Ruby193/lib/ruby/gems/1.9.1/gems/json-1.8.1 for inspection.
  Results logged to C:/Ruby193/lib/ruby/gems/1.9.1/gems/json-1.8.1/ext/json/ext/generator/gem_make.out
  </code></pre>
  
  <p>This clearly tells us two things:</p>
  
  <ol>
  <li>Some other devkit.rb file is being loaded when I'm using ruby 1.9, as the 'for 1.9' message isn't being printed.</li>
  <li>This is unlikely to be the actual problem, as the error is identical in either case.</li>
  </ol>
  
  <p>I'm going to see if I can build manually using the generated Makefiles.</p>