Asdf wont switch local version based on tool-versions or using the local command
Asked Answered
S

14

23

I downloaded asdf the other day and am trying to use it with a ruby on rails project I downloaded from Github. When I run asdf install, I get this response:

firebase 9.10.0 is already installed
ruby 2.7.2 is already installed

Then I run rails s :

No preset version installed for command rails
Please install a version by running one of the following:

asdf install ruby 2.7.2

or add one of the following versions in your config file at 
/Users/******/Desktop/****/.tool-versions
ruby 3.0.1

The project runs on ruby 2.7.2.

.tool-versions:

ruby 2.7.2
node 12.18.3

I installed asdf with homebrew and use oh-my-zhs with the asdf plugin.

EDIT: running

asdf install ruby 2.7.2

just says

ruby 2.7.2 is already installed
Scientific answered 22/4, 2021 at 14:55 Comment(0)
W
19

You need to reshim ruby. Run asdf reshim ruby and then try running rails s again.

Whitaker answered 5/5, 2021 at 13:28 Comment(3)
So weird, the command line was saying the right version when I checked asdf current or did ruby -v but this seems to have fixed it πŸ‘ thanks! Saved me a big headache. I wonder why they do not do this automatically when installing/changing versions? There must be a reason because it seems too trivial to not just do it. Seems there's some info here: github.com/asdf-vm/asdf-ruby/issues/63 – Eventuality
Thanks; the reshim command worked for the same issue I was having with kubectl not switching versions with asdf. adsf reshim kubectl – Antechamber
If this doesn't work, check if you have added . /opt/homebrew/opt/asdf/libexec/asdf.sh to ~/.zhrc file. – Hypersonic
C
18

For RubyMine users

If you run the terminal commands inside of your integrated RubyMine terminal, nothing will change until you change the project SDK to the newly installed version. After restarting your terminal, everything should work as expected.

Here's where you can find the setting, check the box next to the sdk you want to use for the project: enter image description here

Cottingham answered 14/3, 2022 at 10:43 Comment(2)
How do i add asdf SDK to rubyMine. I am not able to figureout the location of this. I Have installed asdf using rosetta on my Apple silicon. – Syne
Hey @Divnoorsinghkang, if you don't have the asdf SDKs in the Settings like in the screenshot, then you might need to ask a new question. I think I had this issue once and resolved with reshims and properly installing asdf. – Cottingham
L
16

For people experiencing this issue, who've installed asdf via Homebrew and who are using the standard ZSH shell (no oh-my-zsh plugins), don't forget to source the asdf bash script:

echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ${ZDOTDIR:-~}/.zshrc
Landlady answered 29/4, 2022 at 22:45 Comment(4)
And for those who are using BASH: echo -e "\n. $(brew --prefix asdf)/libexec/asdf.sh" >> ${ZDOTDIR:-~}/.bash_profile – Halfpenny
dont forget to resource source ~/.zshrc – Tenaille
After dealing with asdf for countless hours, this is what helped me. Such a simple line that solved all my problems. – Vary
This is the exactly reason that give me error when use local version installed with asdf. The very old config has a "\." (backslash plus dot) to load asdf command line, it is works for a long time but for now I had to remove the backslash. When install from git + bash or brew, like this answer is needed to revisit the configuration. – Bivens
B
7

If Ruby 2.7.2 is already in your .tool-versions file, you can fix this by simply running

$ bundle

This will install the correct version of the Rails gem and everything should work again.

Try looking at the output of shim-versions first and you will most likely see that Ruby 2.7.2 is there, but Rails 2.7.2 is not:

$ asdf shim-versions rails
ruby 2.7.0

$ asdf shim-versions ruby
ruby 2.7.2
ruby 2.7.0
Bethezel answered 26/5, 2021 at 11:28 Comment(0)
T
7

The following steps given by purplespline that I found in this Reddit thread worked for me:

asdf exec gem install rails

then you should be able to run other rails commands.

Trost answered 29/9, 2021 at 6:3 Comment(0)
T
5

On my end I did not export or add this on my zshrc

. /opt/homebrew/opt/asdf/libexec/asdf.sh

And resource

source ~/.zshrc
Tenaille answered 29/9, 2022 at 11:0 Comment(0)
A
3

You should install bundle gem first

gem install bundle

then you can run bundle or bundle install

Archduchess answered 5/10, 2021 at 14:32 Comment(1)
This was it for me. Forgot to run bundle and the error wasn't clear. – Squires
S
3

This is what worked for me. I had forgotten to bundle after updating.

asdf plugin-update ruby
asdf install ruby 3.1.4
bundle
Spoke answered 24/8, 2023 at 21:28 Comment(0)
G
0

I had a similar issue and I just reinstalled rails.

gem install rails
Grandson answered 24/4, 2021 at 16:51 Comment(0)
H
0

for diagnostics, please check out outputs of

which ruby

and

ruby --version

in the working directory of your project. If it indeed shows you ruby version 2.7.2, then run "gem install rails" or ("bundle install" if gemfile.lock is present) from this working directory again.

One common mistake is not to open new shell after modification of shell config files. Also zsh requires to run "rehash" command if some program is installed at different path, but reopening shell will solve that.

Human answered 14/5, 2021 at 21:43 Comment(0)
H
0

One more tip: check if you have updated .bashrc or .zshrc as in the installation guide.

Hypersonic answered 19/8, 2023 at 20:23 Comment(0)
I
0

Try:

rm ~/.asdf/shims/rails
asdf reshim
Illuviation answered 17/5 at 14:57 Comment(0)
B
0

As I said in a comment at @roman answer, is the exactly reason that give me error when use local version installed with asdf. The comment of @viacheslav draw my attention to configuration post installation with some shell.

The very old installation config has a "\." (backslash plus dot) to load asdf command line, it is works for a long time but for now I had to remove the backslash. When install from git + bash or brew, like the original answer, it is needed to revisit the configuration.

I update the asdf version but not revisited shell config file (.zshrc in my case).

Correct way to load asdf: enter image description here

Wrong way that worked someday: enter image description here

Bivens answered 14/6 at 7:50 Comment(0)
P
-1

Once Homebrew is installed, you can use it to install Ruby.

brew install ruby This will install the latest Ruby version on Homebrew (which is 2.5.0 at the time of this writing) on /usr/local/bin/ruby. Add /usr/local/bin to the start of your PATH so it becomes the default Ruby.

Add this to ~/.profile or ~/.bash_profile.

export PATH='/usr/local/bin:/usr/local/sbin:$PATH'

If you’re using Ruby to run a few scripts then using the Homebrew version is enough

Porcia answered 25/10, 2023 at 5:59 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. – Cerous
unclear what version you are talking about when you say 2.5.0. ruby 2.5.0 is deprecated for about 3 years. 2.7.x is going EOL in April, and 3.3.0 is scheduled to be release on Dec 25th. Homebrew I have is version 4.1.17. The question is about asdf. not about installing ruby in some other ways. and why hacking the PATH manually? – Bicuspid

© 2022 - 2024 β€” McMap. All rights reserved.