Search code examples
rubybashjekyllwindows-subsystem-for-linuxbundler

Cannot run ruby/bundle script from WSL 'bash script.sh' command line, but OK from a bash interpreter 'sh script.sh'


I have successfully installed ruby in my WSL2 environment on Windows 11 using the following commands:

$ sudo apt install gnupg2 build-essential dh-autoreconf
$ gpg --keyserver keyserver.ubuntu.com --recv-keys ...
$ \curl -sSL https://get.rvm.io | bash
$ rvm install ruby
$ gem update
$ gem install jekyll
$ gem install bundler

After that, the installed ruby version is 3.1.3p185 (that's what I get when running ruby --version)

Consider now the following test.sh script that I have in my WSL home directory:

#!/bin/bash
bundle exec jekyll build

When I open a bash command prompt (using Win+R then typing bash), I can successfully run:

$ sh test.sh

In this case, the Jekyll build runs fine.

Now, if, from a Powershell (or Windows CMD prompt) I type:

$ bash test.sh

I get the following error:

/usr/local/bin/bundle: /usr/bin/ruby2.7: bad interpreter: No such file or directory

Why? I have many other .sh scripts that I can run fine using the bash script.sh syntax.

I found the following question

`$ bundle install` throws the error: bash: /usr/local/bin/bundle: /usr/bin/ruby2.7: bad interpreter: No such file or directory

but unfortunately the proposed fix does not work (I already have bundler installed).

EDIT: using wsl -e bash test.sh instead of bash test.sh produces the same error. Also, if test.sh just contains ruby --version, I get ruby: command not found.


Solution

  • Change the test.sh script to:

    #!/bin/bash
    source ~/.rvm/scripts/rvm
    rvm use 3.1.3 --install
    bundle exec jekyll build
    

    Save changes and run again:

    bash test.sh
    

    This should solve the problem.