public
Last active

Solving the "Could not find a JavaScript runtime" error (Rails 3.1 deployment with Capistrano 2.12)

  • Download Gist
gistfile1.md
Markdown

Capistrano: Solving the "Could not find a JavaScript runtime" error

Did you ever try to deploy your Rails application with Capistrano but got an error saying "Could not find a JavaScript runtime" like this:

[out :: web.YourServer.com] rake aborted!
[out :: web.YourServer.com] Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes.

Some people on the web were able to solve this issue by adding therubyracer to their Gemfile (as the linked website in the error message suggests).

gem 'therubyracer'

Unfortunately rather than solving the issue, in my case I got a new error when doing the bundle install locally. The 'patch.exe' stopped working.

As you can see, I'm developing on Windows.

However you can easily resolve this problem by changing your Gemfile.

gem 'therubyracer', :platforms => :ruby

This will prevent Bundler from installing the gem on Windows but it get's installed on the production server. Fine - but still when doing a cap deploy it couldn't find a JavaScript engine.

As I found out this is because by default, Capistrano uses the --deployment flag when doing the bundle install on the production machine. In turn this means Bundler uses the Gemfile.lock to solve gem dependencies.

Got it? Again: Bundler uses the Gemfile.lock to solve gem dependencies.

As I said, I'm running bundle install and cap deploy on my Windows development box. Therefore and because of the :platforms => :ruby option in the Gemfile, therubyracer won't show up in the specs: section of your Gemfile.lock. In turn this is why I still got the error when running cap deploy.

Finally solving the issue

Keep therubyracer gem in you Gemfile.

gem 'therubyracer', :platforms => :ruby

But also add this to your deploy.rb file:

set :bundle_flags,    ""

And remove the Gemfile.lock from your version control repository (don't forget to commit!).

This overrides one of the default settings of Bundler recipie for Capistrano which is the mentioned --deployment flag. This means Bundler will recalculate your gem dependencies every time you do a cap deploy. This also removes the --quiet flag so you can see what Bundler is doing on your production machine ;-)

Please note

I am not sure if it is a good idea to remove the Gemfile.lock from version control and doing a dependency recalculation on every deployment. E.g. as mentioned here it should be in version control.

However, this was the only way for me to solve the issue. Suggestions are welcome.

You can also fix this by forcing the installation of the V8 engine on your destination server. For instance, on an Ubuntu instance, "apt-get install nodejs" will do this.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.