416

I'm trying to use the Mongoid / Devise Rails 3.1 template (Mongoid and Devise), and I keep getting an error stating ExecJS cannot find a JavaScript runtime. Fair enough when I didn't have any installed, but I've tried installing Node.js, Mustang and the Ruby Racer, but nothing is working.

I could not find a JavaScript runtime. See sstephenson/ExecJS (GitHub) for a list of available runtimes (ExecJS::RuntimeUnavailable).

What do I need to do to get this working?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
srboisvert
  • 12,679
  • 15
  • 63
  • 87
  • 4
    btw - I am using ubuntu karmic. – srboisvert Jun 08 '11 at 18:21
  • You should really consider changing the answer. The Node.js answer is not nearly as good as the execjs/rubyracer. – Sean Dunford Feb 02 '13 at 04:43
  • 3
    The rubyracer has other issues with it. Heroku no longer recommends including it in your Gemfile if you can avoid it. https://devcenter.heroku.com/articles/rails-asset-pipeline#therubyracer I think that installing a proper Javascript runtime on Ubuntu is the correct answer to this question. – M. Scott Ford Mar 27 '13 at 16:00

18 Answers18

452

Ubuntu Users

I'm on Ubuntu 11.04 and had similar issues. Installing Node.js fixed it.

As of Ubuntu 13.04 x64 you only need to run:

sudo apt-get install nodejs

This will solve the problem.


CentOS/RedHat Users

sudo yum install nodejs
Michael Butler
  • 6,079
  • 3
  • 38
  • 46
eldewall
  • 4,816
  • 1
  • 16
  • 9
  • 1
    tried it. no luck. I wonder if it is a rvm conflict or something. Everything other people have suggested hasn't worked for me. – srboisvert Jun 08 '11 at 18:21
  • Okey, i got the error when i tried generating a model in rails 3.1.0rc2. Does generating models work for you? (this was before installing nodejs that fixed it) – eldewall Jun 08 '11 at 18:48
  • You can try changing the Gemfile so it uses rails3.1.0rc2 instead of 3.1.0.rc1 and run bundle install – eldewall Jun 08 '11 at 18:51
  • actually, i just reinstalled nodejs and it worked. Not sure what I did before but yay! – srboisvert Jun 08 '11 at 19:14
  • 78
    gem 'execjs' gem 'therubyracer' it's a better solution than this one. – dwaynemac Aug 02 '11 at 14:42
  • I'm on Ubuntu 11.10 running Ruby 1.9.2 on Rails 3.1.3 freshly installed through RVM. I need both the **package nodejs** and the **gem execjs** to resolve the isse on my machine. I did not need the therubyracer gem, but the execjs _per se_ did not solve the problem, so I guess for me it was a mix from the above and below solutions. Hope this can help others... – jasoares Dec 28 '11 at 00:51
  • 27
    Heroku is now [strongly discouraging](http://devcenter.heroku.com/articles/rails31_heroku_cedar#troubleshooting) therubyracer due to memory use. I installed nodejs on my dev machine per this answer and took therubyracer out of my gemfile. See also http://stackoverflow.com/questions/7092107/rails-3-1-error-could-not-find-a-javascript-runtime. – Mark Berry Jan 17 '12 at 01:58
  • 11
    @dwaynemac I disagree. I'd rather the runtime exist as an OS library (as with Mac and Windows by default), rather than adding yet another gem to the dependancy list. – William Denniss Feb 02 '12 at 14:13
  • @WilliamDenniss but it **is** a dependency. having it as a gem ensures you'll have that installed in any server you deploy. – dwaynemac Feb 03 '12 at 14:24
  • @dwaynemac, true, but a lot of existing projects (as well as a fresh project generated by rails) do not have this dependency in their `Gemfile` and it's pretty annoying to have to add `therubyracer` every time on your own. – KL-7 Feb 04 '12 at 11:39
  • I prefer the Gemfile solution as it allows the project to build without dependancies on system-wide libraries – Benoit Apr 19 '12 at 10:56
  • @dwaynemac: To run the rails test suite, you need to have it installed manually. Within your own application, both ways are valid options. – iGEL Jun 02 '12 at 22:35
  • 1
    It seems that therubyracer and execjs are causing problems with heroku deployment, unless there's something else that was modified that I missed. This is the preferred solution. – Sturm Mar 14 '13 at 22:35
  • I hesitate to add ```therubyracer``` to my Gemfile as it causes issues with Windows. I know I may be crazy to develop on Windows, but I have to for work. – Travis Pessetto Oct 24 '13 at 16:08
  • Doing `sudo apt-get install nodejs` worked for me on 14.04, but I suspect it's because it also installed libv8. I feel like installing `therubyracer` should fail if you don't have libv8 installed, but it doesn't -- it acts like it succeeds. – duma Dec 16 '14 at 14:37
  • It does not work with Rails 6 and Ubuntu 18.04, does anyone show me how to fix it? – Huy Vu The Dec 16 '21 at 10:58
446

Just add ExecJS and the Ruby Racer in your gem file and run bundle install after.

gem 'execjs'

gem 'therubyracer'

Everything should be fine after.

l0b0
  • 55,365
  • 30
  • 138
  • 223
vincent jacquel
  • 5,107
  • 2
  • 17
  • 18
  • 6
    This worked for me, rails 3.1rc4 and Ubuntu 11.04 (my first try with rails 3.1). I was trying to generate a scaffold when I got the error. Still pretty lame that rails 3.1 is "broken" out of the box. – wonderfulthunk Jun 24 '11 at 21:00
  • Was deploying on Heroku and received that error. Once I added the new gem and deployed, I was golden. – François Beausoleil Jul 06 '11 at 16:28
  • 35
    For Rails 3.1 RC 6, you just need to specify `gem 'therubyracer'` – Amree Aug 18 '11 at 09:07
  • 5
    Is there anyway to have these gems included in the default Gemfile when a new application is created? – Richard Fawcett Sep 25 '11 at 16:16
  • Works on Ubuntu 11.04, 11.10. Thanks. Easy solution, but like other comments say: Why this? Must we always do this now? – mydoghasworms Oct 19 '11 at 08:56
  • 2
    this is a better solution than installing node.js. If you're deploying to a remote server or sharing the app with other developers, having everyone install node.js is a lot more pain than just running bundle install, which they would be doing anyway. – jdkealy Oct 29 '11 at 12:34
  • 1
    As stated above, since the official release of rails 3.1, just adding the rubyracer gem is enough. Although we would have liked to add nothing in the final release ! Thanks to foo for the precision. – vincent jacquel Oct 30 '11 at 12:44
  • 20
    Heroku is now [strongly discouraging](http://devcenter.heroku.com/articles/rails31_heroku_cedar#troubleshooting) therubyracer due to memory use. I installed nodejs on my dev machine (https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager) and took therubyracer out of my gemfile. See also http://stackoverflow.com/questions/7092107/rails-3-1-error-could-not-find-a-javascript-runtime. – Mark Berry Jan 17 '12 at 01:56
  • I was having this problem with Sinatra. I tried installing nodejs and may have missed a step but I was still receiving the error. Installing the two gems worked quite well. If this is the case with your rails app I recommend putting therubyracer in your development group and putting your app on heroku's cedar stack. – leetheguy Jun 15 '12 at 20:24
  • @MarkBerry thanks! I removed therubyracer and installed nodejs and it also sped up my dev env. Sweet. – Seth Malaki Oct 29 '12 at 14:54
  • This seems to be causing problems with heroku deployment. – Sturm Mar 14 '13 at 22:35
78

In your Gem file, write

gem 'execjs'
gem 'therubyracer'

and then run

bundle install

Everything works fine for me :)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
manish nautiyal
  • 2,556
  • 3
  • 27
  • 34
48

I had a similar problem: my Rails 3.1 app worked fine on Windows but got the same error as the OP when running on Linux. The fix that worked for me on both platforms was to add the following to my Gemfile:

gem 'therubyracer', :platforms => :ruby

The trick is knowing that :platforms => :ruby actually means only use this gem with "C Ruby (MRI) or Rubinius, but NOT Windows."

Other possible values for :platforms are described in the bundler man page.

FYI: Windows has a builtin JavaScript engine which execjs can locate. On Linux there is not a builtin although there are several available that one can install. therubyracer is one of them. Others are listed in the execjs README.md.

jwfearn
  • 28,781
  • 28
  • 95
  • 122
37

Adding the following gem to my Gemfile solved the issue:

gem 'therubyracer'

Then bundle your new dependencies:

$ bundle install
JDutil
  • 3,622
  • 25
  • 32
17

An alternative way is to just bundle without the gem group that contains the things you don't have.

So do:

bundle install --without assets

you don't have to modify the Gemfile at all, providing of course you are not doing asset chain stuff - which usually applies in non-development environments. Bundle will remember your '--without' setting in the .bundle/config file.

moschops
  • 318
  • 2
  • 7
  • Best Answer. Worked for rails version 3.2.3 – Frantz Romain Apr 12 '12 at 05:23
  • 1
    This is great, and such a simple fix. The Heroku site says RubyRacer is no longer required (with Cedar stack) so I was puzzled over why suddenly my program wasn't running locally. This did the trick. – Mike Blyth Aug 20 '12 at 21:41
8

Add following gems in your gem file

gem 'therubyracer'
gem 'execjs'

and run

bundle install

you are done :)

Ramiz Raja
  • 5,942
  • 3
  • 27
  • 39
7

For amazon linux(AMI):

sudo yum install nodejs npm --enablerepo=epel
William Hu
  • 15,423
  • 11
  • 100
  • 121
5

I had this same error but only on my staging server not my production environment. nodejs was already installed on both environments.

By typing:

which node

I found out that the node command was located in: /usr/bin/node on production but: /usr/local/bin/node in staging.

After creating a symlink on staging i.e. :

sudo ln -s /usr/local/bin/node /usr/bin/node

the application then worked in staging.

No muss no fuss.

Sean
  • 61
  • 1
  • 1
4

I installed node via nvm and encountered this issue when deploying with Capistrano. Capistrano didn't load nvm automatically because it runs non-interactively.

To fix, simply move the lines that nvm adds to your ~/.bashrc up to the top. The file will then look something like this:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
Kevin Cooper
  • 5,018
  • 4
  • 37
  • 51
4

I used to add the Ruby Racer to the Gem file to fix it. But hey, Node.js works!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Richard Zhang
  • 350
  • 1
  • 4
3

Don't Use RubyRacer as it is bad on memory. Installing Node.js as suggested by some people here is a better idea.

This list of available runtimes that can be used by ExecJs Library also documents the use of Node.js

https://github.com/sstephenson/execjs

So, Node.js is not an overkill, and much better solution than using the RubyRacer.

Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
Nerve
  • 6,463
  • 4
  • 29
  • 29
2

FYI, this fixed the problem for me... it's a pathing problem: http://forums.freebsd.org/showthread.php?t=35539

MichelV69
  • 1,247
  • 6
  • 18
1

The answer on a Mac in 2022 is simply:

brew install nodejs

Then rerun rails server.

danielricecodes
  • 3,446
  • 21
  • 23
0

I started getting this problem when I started using rbenv with Ruby 1.9.3 where as my system ruby is 1.8.7. The gem is installed in both places but for some reason the rails script didn't pick it up. But adding the "execjs" and "therubyracer" to the Gemfile did the trick.

arangamani
  • 354
  • 4
  • 11
0

In your gem file Uncomment this line.

19 # gem 'therubyracer', platforms: :ruby

And run bundle install

You are ready to work. :)

Sandeep Roniyaar
  • 204
  • 1
  • 8
  • 26
0

Attempting to debug in RubyMine using Ubuntu 18.04, Ruby 2.6.*, Rails 5, & RubyMine 2019.1.1, I ran into the same issue.

To resolve the issue, I uncommented the mini_racer line from my Gemfile and then ran bundle:

# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby

Change to:

# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'mini_racer', platforms: :ruby
CommandZ
  • 3,333
  • 1
  • 23
  • 28
0

I'm using Rails 7 and the bootstrap 5.2.3 Gem. On production it runs in a Docker Container, based on Alpine Linux. For my case it was not enough to install nodejs and npm. I had to install yarn like this:

apk add --no-cache yarn

Only after that, the error message disappeared on production.

Robert Reiz
  • 4,243
  • 2
  • 30
  • 43