LoadError: Could not load the 'listen' gem (Rails 5)
Asked Answered
W

11

76

I've got an API mode Rails 5 app that won't let me run rake routes or rails s. The error I get is:

$ rake routes
rake aborted!
LoadError: Could not load the 'listen' gem. Add `gem 'listen'` to the development group of your Gemfile
.../config/environment.rb:5:in `<top (required)>'
LoadError: cannot load such file -- listen
.../config/environment.rb:5:in `<top (required)>'
Tasks: TOP => routes => environment
(See full trace by running task with --trace)

I've verified that listen is in the development group in my Gemfile:

group :development do
  gem 'listen', '~> 3.1.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

And that it's in my Gemfile.lock:

$ cat Gemfile.lock | grep 'listen'
    listen (3.1.5)
    spring-watcher-listen (2.0.0)
      listen (>= 2.7, < 4.0)
  listen (~> 3.1.5)
  spring-watcher-listen (~> 2.0.0)

I've bundle updated, and bundle installed, and verified that gem install listen works. This was working earlier this week, but I'm not having luck going back through my commits.

$ ruby -v
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]

I don't see it in vendor/cache but I'm not sure what to make of that...

$ bundle package | grep 'listen'

Appreciate the help!

Update:

I can "fix" the problem by putting gem 'listen', '~> 3.1.5' in the global Gemfile (and removing it from :development). Then all the errors go away and everything works, but that seems wrong.

Whitacre answered 29/7, 2016 at 16:41 Comment(2)
I had this issue and fixed it by running bundle install --with development test.Romeoromeon
production server doesn't require development and test gems. so after the bundle install --without development test you may run rails commands by identifying the environment. RAILS_ENV=production bundle exec rails s or bundle exec rails s -e productionDelocalize
W
18

I'm posting this as an answer, but I don't like it.

I can "fix" the problem by putting gem 'listen', '~> 3.1.5' in the global Gemfile (and removing it from :development). Then all the errors go away and everything works, but that seems wrong.

Whitacre answered 29/7, 2016 at 19:8 Comment(5)
having the same issue right now, any update on this?Jackquelinejackrabbit
@PetrosKyriakou unfortunately no. Hopefully the solution posted works for you in the mean time.Whitacre
well yeah it did but i do not like this solution more than you do... thanks anyhow for it!Jackquelinejackrabbit
Hi, I would like to know if is it possible to disable the listen gem completely? In particular in production?Damask
I had the same error today, I checked my suspision and I turned out I was right. I will post as a possible answer.Leafstalk
F
116

If you are on rails 5 and you are using the default config/environments/development.rb file it will have this line of code in there.

config.file_watcher = ActiveSupport::EventedFileUpdateChecker

This requires the gem listen. This threw me for a bit as I was doing a rails 4 upgrades to a rails 5

edit: Forgot to mention that if you comment that line of code out it will not need the listen gem anymore.

Fulllength answered 28/2, 2017 at 17:14 Comment(5)
Thanks. I needed to comment out that line. This is kind of a silent dependency in the sense that it does not show up as a dependency in bundler.Switzer
Yeah Commenting it out makes the error above go away. Forgot to mention that part.Fulllength
instead of commenting it out, I made it conditional on if defined? Listen. I'd rather not describe the reasons why this was necessary...Harrumph
This was key for me - understanding that listen (and spring, and spring-watcher-listen) were optional in development. I made the above development.rb change, and commented those 3 gems out of the Gemfile, reran bundle install, and Voila! my rails app would run locally again! :DButterfly
I would encourage the use of the listen gem here as it should make things more efficient in detecting code changes in development.Micmac
S
78

You might by mistake have set bundle install --without at some point, I sure did anyways.

To revert this run:

bundle config --delete without

I also ran bundle config --delete with as I manually set with option as well by mistake. Running both should get you back to default behaviour.

After having deleted the without config I could successfully run a bundle install again and afterwards my rails s, rails db:migrate etc. worked.


You can confirm if this is your issue by running bundle install and look at the second last line in the output. If it states:

Gems in the groups development and test were not installed.

It's for sure above solution should work for you.

Sava answered 30/10, 2016 at 18:54 Comment(3)
bundle config --delete without fixed the problem for me. ThanksCannoneer
Why would you want to install test/development gems on a production machine? Instead run commands in explicit production mode.Swaney
The suggestion of running bundle install was enlightening. Locally, that command was not run on this application (but probable on others) and bundler and rails ran smoothly. It was when deploying in development mode that this issue cropped up. So is the cap command singular through all applications?Desuetude
N
28

I used this: bundle install --without development

Error:

Could not load the 'listen' gem. Add gem 'listen' to the development group of your Gemfile (LoadError)

After this, use that code:

bundle config --delete without
bundle config --delete with

Finally

bundle install
Nesselrode answered 6/8, 2017 at 11:0 Comment(3)
Like a monkey flying a 747 i inputted those commands and it worked like a charm! chrs.Magaretmagas
@BKSpurgeon This didn't work for me (another answer did), but upvote for hilarious comment about monkey flying a 747 :DButterfly
This should be marked the right answer to the questionObstruction
W
18

I'm posting this as an answer, but I don't like it.

I can "fix" the problem by putting gem 'listen', '~> 3.1.5' in the global Gemfile (and removing it from :development). Then all the errors go away and everything works, but that seems wrong.

Whitacre answered 29/7, 2016 at 19:8 Comment(5)
having the same issue right now, any update on this?Jackquelinejackrabbit
@PetrosKyriakou unfortunately no. Hopefully the solution posted works for you in the mean time.Whitacre
well yeah it did but i do not like this solution more than you do... thanks anyhow for it!Jackquelinejackrabbit
Hi, I would like to know if is it possible to disable the listen gem completely? In particular in production?Damask
I had the same error today, I checked my suspision and I turned out I was right. I will post as a possible answer.Leafstalk
F
11

I found an easy fix after a lot of trials of rebuild's

bundle config --delete without
bundle config --delete with
bundle install

If you accidentally built the gemset prior to seting your envirnoment correctly, then this may help clear out the problems.

Fettling answered 15/8, 2020 at 5:39 Comment(0)
L
9

I'm having the same problem by running rails c.

By reading this other Stack Overflow post I did realize that it is normal that both bundle exec rake command or rails console are running in a default production environment.

I figured I will solve the issue either by:

  1. adding export RAILS_ENV=production in ~/.bash_profile
  2. explicitly writing the environment in which I want the command to execute like bundle exec rake a_rake:task RAILS_ENV=production rails console --env=production etc...
Lepine answered 23/3, 2017 at 4:12 Comment(2)
I had the same problem because I was installing my application with bundle install --without development test and then running it without specifying RAILS_ENV=production. So my application was running in development mode without the development gems installed.Placida
This was needed for running rails c in my live app deployed by capistrano.Elodia
U
9

I had similar problem today after upgrade from Rails 5.1.5 to 5.2.0. First time running the server there was the following 'missing assets' problem:

ActionView::Template::Error (The asset "asterisk_orange.png" is not present in the asset pipeline.)

Trying to precompile the assets shows the 'gem listen error':

$ bundle exec rake assets:precompile
rake aborted!
LoadError: Could not load the 'listen' gem. Add `gem 'listen'` to the development group of your Gemfile

My solution was to explicit set production environment:

$ RAILS_ENV=production bundle exec rake assets:precompile

This precompiles the assets w/o problems and the 'missing assets' problem was fixed.

Unhouse answered 1/5, 2018 at 5:7 Comment(0)
F
6

I had the same issue. Thanks to @newdark answer I figured out the correct solution. Basically I wanted to deploy rails in production mode. But obviously forgot to set environment variable RAILS_ENV=production before running server.

So to recap, dependencies for production mode were installed while rails tried to start in development mode due to forgetting to set RAILS_ENV=production. If I went on to add gem listen to the production dependencies, I'd be running in development mode without being able to notice.

For me the solution was to do export RAILS_ENV=production before executing any rails commands and keep dependencies intact. Hope I managed to explain.

Fabiano answered 17/3, 2017 at 16:6 Comment(0)
S
2

I had the same problem, i fix it by running

rails c -e production
Savarin answered 30/4, 2018 at 7:35 Comment(2)
solution is not explains the true nature of the problem. in production mode, there is no listen gem installing any more from Gemfile. Therefore, production switch is totally bypassing the requirement.Giana
@Giana production doesn't need the listen gem. since the code is frozen, there is no reason to listen for the code modifications. None of the dev and test gems is required there. unless your coding on the server ;)Delocalize
L
1

I had same error when trying to generate mongoid database file.

but I created rails new project on ruby 2.5.1. Your ruby is 2.2. so the causality could be different in my situtation.

when I used rails new project, it was created in version 5.2, then I changed gem to 5.1.2 and problem arises. When I created with rails _5.1.6_ new in Gemfile there is generated additional pack for development.

  group :development do
      # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
      gem 'web-console', '>= 3.3.0'
      gem 'listen', '>= 3.0.5', '< 3.2'
      # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
      gem 'spring'
      gem 'spring-watcher-listen', '~> 2.0.0'
    end

this way 'listen' showed me in gemfile automaticaly

Leafstalk answered 21/10, 2018 at 9:49 Comment(0)
V
0

I was working with Docker and encountered this error when executing

rails webpacker:install

It was happening because this line was being executed before

ENV RAILS_ENV production
RUN chmod +x bin/rails
ENTRYPOINT ["bin/rails"]

Fixed it by simply placing all rails commands after this

Verism answered 18/12, 2021 at 20:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.