Error with gradlew: /usr/bin/env: bash: No such file or directory

49,934

Solution 1

The problem's cause was that Git on Windows converted the line endings of gradlew from Unix style (LF) to Windows style (CRLF).

You can turn off that automatic conversion using git config core.autocrlf false.

Setting the line endings of gradlew back to Unix style fixed the problem. In Vim this is done using :set fileformat=unix.

Solution 2

This is because the gradlew file has Windows file-endings.

You can install dos2unix with apt-get or yum by running:

sudo apt-get install dos2unix

sudo yum install -y dos2unix

and then use dos2unix to convert the line-endings

from CRLF Windows CarriageReturn + LineFeed

to LF Linux LineFeed only:

dos2unix ./gradlew

Then run your gradlew for a test:

./gradlew clean -d

Solution 3

Thanks for the answers, these highlighted that the problem was Windows creating the gradlew, when I am on mac.

To fix this, from the directory of the app I ran the command:

gradle wrapper

This creates (replaces the broken) a gradle wrapper, that works! (notice it uses your local gradle install to fix the wrapper of the app)

Solution 4

To solve it permanently for Git checkouts on Linux and Mac:

In your root Git repo directory add a file named .gitattributes with this content:

gradlew text eol=lf

More information: https://git-scm.com/docs/gitattributes

PS: Works too for .sh files using this entry:

*.sh text eol=lf

The entries work at checkout time and convert the line endings of these files to LF.

Solution 5

A vendor had shipped a gradlew with Windows carriage returns. Opening gradlew in vim revealed ^M line endings. Running :e ++ff=dos removed the line endings and resolved this issue.

As per https://vim.fandom.com/wiki/File_format

Share:
49,934
Matthias Braun
Author by

Matthias Braun

Build a better Stack Exchange: Codidact All code I publish on Stack Overflow and other sites of the Stack Exchange network shall be licensed under the BSD 2-Clause License: Copyright Matthias Braun All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Updated on March 05, 2021

Comments

  • Matthias Braun
    Matthias Braun about 3 years

    After committing my project's gradlew file from my Windows machine to the remote repo using Git, invoking gradlew on my Linux server failed with this message:

    /usr/bin/env: bash: No such file or directory

    What happened?

  • Thiago
    Thiago almost 9 years
    @MatthiasBraun - Hello. I am having the same problem. I have an Android project that was built on Windows using Android Studio. When I import in my MAC, I can not run ./gradlew clean or ./gradlew build as i get = env: bash\r: No such file or directory . i try set fileformat=unix but does not work.
  • Martin Erlic
    Martin Erlic about 7 years
    This doesn't solve my problem. I'm using Windows as well.
  • Jeremy Mangas
    Jeremy Mangas over 6 years
    As an additional help for Windows users, it can be done in Notepad++ using the menu item Edit -> EOL Conversion -> Unix (LF)
  • Nils Werner
    Nils Werner almost 5 years
    Is this really an answer? Can you provide more information about what your answer does compared to all the other answers?
  • Blundell
    Blundell almost 5 years
    it runs the gradle wrapper cmd from the root dir, which the other answers dont..
  • Tarun Gupta
    Tarun Gupta about 3 years
    that exactly was the problem in my case.. I am sharing my project files between windows machine and macbook
  • Ali Yar Khan
    Ali Yar Khan over 2 years
    not working ... zsh: command not found: gradle
  • Blundell
    Blundell over 2 years
    You need gradle installed, like brew install gradle