12

I have Rails project. When I try to run any rake task or rails server it give me this error

env: ruby\r: No such file or directory

Could someone help me?

Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Igor Babkin
  • 509
  • 2
  • 6
  • 14

7 Answers7

40

If you are working on a Unix / Mac, then this error is because you have incorrect line endings.

Here is a solution using dos2unix; you may need to install this program on your system. If apt is available, you can use sudo apt install dos2unix.

    1. Set your line endings correctly, and have git manage how it handles them:
git config --global core.autocrlf input
    1. In your directory you are going to convert all of the files by running:
find ./ -type f -exec dos2unix {} \;

This will cycle through all of your files, converting them. and solving the problem. Add your changes. Commit them, and you should be good to go.

ad absurdum
  • 19,498
  • 5
  • 37
  • 60
R.J. Robinson
  • 2,180
  • 3
  • 21
  • 33
  • 14
    For those of you who got "find: ‘dos2unix’: No such file or directory" error: sudo apt install dos2unix – RealMan Jul 26 '17 at 14:59
  • 3
    Note that that find command may be excessive... this point is arguable; it may well be fine, but it may be overkill in some situations. Another possible route (for step 2 in this answer) is `git rm -r --cached .` followed by `git reset --hard HEAD`... which is likely faster (if nothing else, it won't run dos2unix on files in the `.git` housekeeping directory!)... This has potential gotchas as well (probably quite fine if you're running from a "clean" checkout, though), but thought I'd at least mention it. – lindes Jul 13 '19 at 00:42
  • For me on Debian it was enough to use: sudo dos2unix file.txt – k1000 May 27 '21 at 08:41
4

You probably have edited ./bin/rake file and added \r at the end of first line:

#!/usr/bin/env ruby
begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
end
require_relative '../config/boot'
require 'rake'
Rake.application.run

Make sure that after "ruby" you have only new line char.

3

Thanks to the comments above, I solved my server issue that was caused from cloning my group's github rails app and causing localhost:3000 to fail. I was just working on the backend from my fullstack app: ruby(-v 2.7.1)/rails(-v 6.0.3.4). And these 2 people's comments solved my error:

"For those of you who got "find: ‘dos2unix’: No such file or directory" error: sudo apt install dos2unix" – RealMan Jul 26 '17 at 14:59

"Note that that find command may be excessive... this point is arguable; it may well be fine, but it may be overkill in some situations. Another possible route (for step 2 in this answer) is git rm -r --cached . followed by git reset --hard HEAD... which is likely faster (if nothing else, it won't run dos2unix on files in the .git housekeeping directory!)... This has potential gotchas as well (probably quite fine if you're running from a "clean" checkout, though), but thought I'd at least mention it." – lindes Jul 13 '19 at 0:42

2

I kept getting this error and finally figured out how to fix it.

  1. I made sure all the permissions on the files in my bin folder were executable.

Run ls -lha in your current repository. You want each file to have an x at the end like this

-rwxr-xr-x.

To achieve this, you will want to run chmod +x <file_name_here> for each file in your bin folder, such as chmod +x rails, chmod +x bundle, etc.

Now when you run ls -lha you should see that they all have an x at the end.

  1. Next, either in SublimeText, Atom or what ever text editor you have, you will want to check that you are not using Windows line endings. The \r character is something Windows uses. Unix just uses \n for a new line.

I use Atom so I went to the plugins section (Cmd + , on Mac) and then searched for line-ending-selector in the Packages section, and then went to the line-ending-selectors settings. Change your default to 'LF'.

You will find that at the bottom of files, Atom will tell you the type of line ending the file is using with a CRLF for Windows and LF for Unix/Mac. You want all your files to use 'LF'.

So in your terminal, open each file in your bin folder in Atom, by running atom ./bin/filename (such as atom ./bin/rake).

At the bottom you will see 'CRLF' or 'LF'. If you see 'CRLF', click on it and, at the top of Atom, you can choose 'LF'.

Cmd + s to save.

Do this for each. You are basically telling your file to strip all Windows line endings and use Unix line endings instead.

Once all files are edited, you should be able to run your rake or rails command.

Note: Sublime Text and Text Mate should have equivalents to Atom's line-ending-selector.

Anissa
  • 21
  • 2
2

For macOS users

Step 1: HOMEBREW_NO_AUTO_UPDATE=1 brew install dos2unix
Step 2: git config --global core.autocrlf input
Step 3: find ./ -type f -exec dos2unix {} \; (in the repo you were trying to run your task on)

git add and git commit
You are good to go!

Lawrence Gimenez
  • 2,662
  • 4
  • 34
  • 52
1

I had the same problem on Windows Terminal, using WSL 2! I followed a post that recommended to install the dos2unix dependencie: sudo apt install dos2unix (Using apt package manager) and run other two commands:

  • git config --global core.autocrlf input (Set your line endings correctly, and have git manage how it handles them)

  • find ./ -type f -exec dos2unix {} \; (In your directory you are going to convert all of the files)

The git will identify a couple of changes, but you don't need to commit it. I just made a git restore . , remove node dependencies rm -rf node_modules and download it again yarn install.

0

If none of the other answers works, try this:

git config --global core.autocrlf true
rails app:update:bin
Babatunde Mustapha
  • 2,131
  • 20
  • 21