Skip to content

Instantly share code, notes, and snippets.

@laph
Created August 11, 2012 18:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laph/3326247 to your computer and use it in GitHub Desktop.
Save laph/3326247 to your computer and use it in GitHub Desktop.
Solving the "Could not find a JavaScript runtime" error (Rails 3.1 deployment with Capistrano 2.12)

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.

@xtreme-allan
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment