79

I've tried a number of things like uninstalling/reinstalling rails and gems but to no avail.

When I go into my new project and run rails s or bundle exec rails server I'm getting this error:

bin/rails:6: warning: already initialized constant APP_PATH
/Users/toabui/Sites/cms/bin/rails:6: warning: previous definition of APP_PATH was here Usage: rails COMMAND [ARGS]

Inside my bin/rails I see this code:

#!/usr/bin/env ruby
begin
load File.expand_path("../spring", __FILE__)
rescue LoadError
end
APP_PATH = File.expand_path('../../config/application',  __FILE__)
require_relative '../config/boot'
require 'rails/commands'

Does anyone know why I keep getting that error when I run rails s?

I've googled and it seems like there is an error with the spring gem but I can‛t seem to get it to work.

Andrew Hendrie
  • 6,205
  • 4
  • 40
  • 71
tobu
  • 1,951
  • 2
  • 12
  • 7

20 Answers20

116

I couldn't find the an_initilizer.rb in my directory and I tried uninstalling/installing the spring gem but it didn't work.

However I did managed to finally get it working.

Apparently there is some conflict with spring and rails 4+.

I needed to run:

rake rails:update:bin 

But I ran across another error:

Library not loaded: libmysqlclient.18.dylib

I ran the following command which I found on another stackoverflow post:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib 

Then ran the original command:

 rake rails:update:bin 

Then run the server command:

 rails s

And now my WebBrick Server is running.

Jonathan Allard
  • 18,429
  • 11
  • 54
  • 75
tobu
  • 1,951
  • 2
  • 12
  • 7
40

rake rails:update:bin to the rescue.

cisolarix
  • 500
  • 5
  • 6
13

If you are on El Capitan (OS X 10.11), Security Integrity Protection (SIP) will prevent linking into /usr/lib to fix mysql. Link it into /usr/local/lib instead:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
jcraigk
  • 131
  • 1
  • 4
  • I get the error ln: /usr/local/lib/libmysqlclient.18.dylib: File exists .. Any ideas? – Isaac Y Nov 01 '15 at 13:14
  • @IsaacY what about -f option ? – geoom Dec 18 '15 at 06:50
  • If you installed mysql using homebrew or other method you can find out the libraries path by running `mysql_config --libs` then you do `sudo ln -s mypath /usr/local/lib/libmysqlclient.18.dylib` – gosukiwi Jan 06 '16 at 20:18
6

This is work for me.

gem uninstall mysql2

bundle install or gem install mysql2

kun
  • 91
  • 1
  • 5
4

I was getting the same error. Removed spring from Gemfile and re-bundled. Not really a solution though.

I found the code that created this error in config/initializers/an_initializer.rb

require "lib/a_file_i_need"

I changed it for

require "#{ Rails.root }/lib/a_file_i_need"
gabriel
  • 83
  • 6
2

I got this error by trying to update rails 4 and imagemagick and rmagick. So I just ran

gem uninstall rmagick

Select the All Versions option. Then try again

EDIT: This happaned again with me just now because I tried to use a gem without installing the required base gem. In my case the solution was to install 'omniauth-google' before trying to use 'omniauth-google-oauth2', but because I didn't install I got the same error again

betoharres
  • 1,736
  • 2
  • 19
  • 25
2

I got the same error. I had ruby 2.1.3 and rails 4.1.6 running on Mavericks and then I migrated to Yosemite and installed the 4.2.0 rails version an ruby 2.1.5 and my apps I made in the previous version didn't work with the new one, so I made some gem sets with RVM and installed the 2.1.3 version. Now when I wanted to run the server I got these error:

bin/rails:6: warning: already initialized constant APP_PATH
/Users/Lexynux/_WebProjects/RoR_Apps/SAIIP2/bin/rails:6: warning: previous definition of APP_PATH was here
Usage: rails COMMAND [ARGS]

And as tobu mentioned I ran:

rake rails:update:bin

I got this:

LoadError: dlopen(/Users/Lexynux/.rvm/gems/ruby-2.1.3@SAIIP2/extensions/x86_64-darwin-14/2.1.0-static/mysql2-0.3.16/mysql2/mysql2.bundle, 9): Library not loaded: libmysqlclient.18.dylib

Then I ran this:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

And finally I repeated the first command it the terminal asked me for this:

Overwrite /Users/Lexynux/_WebProjects/RoR_Apps/SAIIP2/bin/rails? (enter "h" for help) [Ynaqdh]

I just typed 'Y' and hit return.

After all this I started working and going well.

Thanks.

alexventuraio
  • 8,126
  • 2
  • 30
  • 35
2

I received this error after upgrading postgresql.

$ gem uninstall pg
$ gem install pg

resolved this issue for me.

Ryan Doom
  • 2,407
  • 1
  • 16
  • 13
2

This happened to me after doing a brew upgrade. My guess is that this broke some gems with native extensions, even though there was no error message pointing to that.

What I ended up doing was completely removing my installed gems (In my case I completely uninstalled and reinstalled the ruby version using rbenv).
Running bundle install recompiled the native extensions, and everything was running again.

amiuhle
  • 2,673
  • 1
  • 19
  • 28
  • 1
    In my case it was after upgrading mysql, so I only gem I had to uninstall was `mysql gem` and then run a bundle install. – guzart Mar 09 '16 at 15:27
2

I'll post what worked for me.

Comment out

gem 'spring'

Add gem 'net-shh'

and run bundle install

And restart your sever

Colper
  • 115
  • 2
  • 8
1

I got the same error, it happend to be related to gem dotenv. Instructions were to add the following to Gemfile:

gem 'dotenv', :require => 'dotenv/rails-now'

But as it turned out, dotenv/rails-now caused the error. If you use dotenv don't require rails-now

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
antonpot
  • 41
  • 9
1

Are you using pg and mysql in different branches ? If yes, please confirm db config file.

Vaisakh VM
  • 1,071
  • 11
  • 9
1

I received this error after upgrading rails. Disabling spring give me a hint that the issue was with:

gem 'google-api-client', require: 'google/api_client'

Changed to:

gem 'google-api-client', '0.9'

Resolved the issue.

Edu Lomeli
  • 2,263
  • 20
  • 18
0

I had the same error message output when trying to start an application within a Vagrant environment. It cropped up out of nowhere after zero changes to the application code (and other weird behaviour followed, such as development.rb being deleted upon attempting to run the app).

In the end I simply halted the VM & restarted it, everything was then fine so I'm assuming it was an issue with file syncing / shared folders perhaps? (default Vagrant shared folder being used).

Greg Annandale
  • 491
  • 4
  • 13
0

Run these in console:

rake tmp:clear
rake secret
Rustam Gasanov
  • 15,290
  • 8
  • 59
  • 72
0

IF rake rails:update:bin gives additional errors:

I had recently been doing some server maintenance and had subsequently updated OpenSSL.

When I tried running the rake rails:update:bin command, I was presented with an error relating to openSSL.

Having rebuilt my version of Ruby (`rvm reinstall ruby-x.x.x' with RVM), both errors went away.

This is always worth a try I guess.

DazBaldwin
  • 4,125
  • 3
  • 39
  • 43
0

My problem was I was using an outdated version of ruby 1.9.3 with rails 4.2. I upgraded to 2.1.2 , removed the broken project, ran rails new blog to recreate my project, navigated into my newly created app and ran rails server and it worked.

Donovan Thomson
  • 2,375
  • 3
  • 17
  • 25
0

I just had this problem and found that it was being caused by the fact that I had removed a gem from the gemfile without deleting the other require references. In my case, I just had to remove it from config/application.rb.

calyxofheld
  • 1,538
  • 3
  • 24
  • 62
0

Had this error recently, it is caused by spring, because of its suggested code in executables:

begin
  load File.expand_path('../spring', __FILE__)
rescue LoadError
end

It originally expects a LoadError for spring itself in production, but by this code all other load error will be ignored too.

Thus if you have any other LoadError in rails loading process (for example in routes/init) spring worker startup fails and then there goes branch that tries to load everything again like there was no spring.

Vasfed
  • 18,013
  • 10
  • 47
  • 53
0

For me this issue presented as a result of bundle upgrading rvm-capistrano amongst other things.
Adding this require:false fixed things in the end as per this previous post

gem  'rvm-capistrano',  require: false

Although could possibly be an additional issue - as running rake rails:update:bin may have helped clear the initial issue.

Community
  • 1
  • 1