Ruby LoadError: cannot load such file

Jason Dugdale

So, you’ve just upgraded your Ruby app to Ruby 1.9.2, 1.9.3, 2.0.0, or greater, and you’re hit with:

<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;" tabindex="0" data-language="bash"><code><span class="line"><span style="color:#B392F0">LoadError:</span><span style="color:#9ECBFF"> cannot</span><span style="color:#9ECBFF"> load</span><span style="color:#9ECBFF"> such</span><span style="color:#9ECBFF"> file</span><span style="color:#79B8FF"> --</span><span style="color:#9ECBFF"> somefile</span></span></code></pre>

Your code will most likely look something like this:

<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;" tabindex="0" data-language="ruby"><code><span class="line"><span style="color:#F97583">require</span><span style="color:#9ECBFF"> 'my-ruby-code'</span></span> <span class="line"><span style="color:#F97583">require</span><span style="color:#9ECBFF"> 'some-gem'</span></span> <span class="line"><span style="color:#E1E4E8">...</span></span></code></pre>

Don’t panic! In Ruby 1.9.2, the current path was removed from the load path, so you simply need to change the ‘require’ statements to your own code to include the current load path:

<pre class="astro-code github-dark" style="background-color:#24292e;color:#e1e4e8; overflow-x: auto;" tabindex="0" data-language="ruby"><code><span class="line"><span style="color:#F97583">require</span><span style="color:#9ECBFF"> './my-ruby-code'</span></span> <span class="line"><span style="color:#F97583">require</span><span style="color:#9ECBFF"> 'some-gem'</span></span> <span class="line"><span style="color:#E1E4E8">...</span></span></code></pre>

You can also do this by using File.expand_path(__FILE__)

Note: You do not need to change any require statements for your Gems – the load path for your Gems is managed by RubyGems.