Can't figure out how to add rbenv shims to $PATH - ruby

I'm having difficulty getting rbenv to change my ruby version. I've reviewed the question here but still struggling: rbenv not changing ruby version
Then ran rbenv init after install, then ran env | grep PATH but don't see any reference to rbenv.
THe documentation (https://github.com/rbenv/rbenv#how-rbenv-hooks-into-your-shell) states that rbenv init adds the necessary text to your PATH automatically.
I am using zsh. I don't know how to edit my PATH file. If someone provides an answer on how to add what I need to PATH, please be explicit with instructions to do so.

Related

Accidentally deleted many hidden files with "git clean -fd" and now I cannot install Ruby

So I really messed up on this one.....
I was working on a rails application and wanted to revert back to a previous commit. I opened a new shell and forgot to cd into my projects folder, so I ran "git reset --hard" and then "git clean -fd" in my user directory. I noticed before everything was gone, but here is what got deleted:
bash-3.2$ git clean -fd
Removing .CFUserTextEncoding
Removing .Trash/
Removing .Xauthority
Removing .adobe/
Removing .bash_history
Removing .bash_profile
Removing .bundle/
Removing .config/
Removing .cups/
Removing .dropbox/
Removing .fontconfig/
Removing .gem/
Removing .gitconfig
Removing .guard_history
Removing .heroku/
Removing .irb-history
Removing .irb_history
Removing .lesshst
Removing .macports/
^C
I was able to save my project in that shell, but I (now realized stupidly) thought restarting my computer would be best once I realized my rails and bundle commands were not working in another shell. Unfortunately I lost that working shell which I through researching realize would have helped.
What is done is done, and I am trying to fix everything, but something I cannot get right is installing ruby. I am using rbenv to install ruby but even after installing it, it won't register with in any shell:
-bash-3.2$ rbenv local
2.1.1
-bash-3.2$ ruby -v
-bash: ruby: command not found
I am a bit inexperienced when it comes to the terminal and I am freaking out. I am wondering if I should restore factory settings. My important pictures and documents are backed up but I don't use time machine.
I don't know if it is just ruby which is the problem, so far nothing else has been an issue, but I would appreciate any help solving the problem!
EDIT
So I realize now that the issue was deleting my bash_profile which has had all sorts of knock on effects. I really don't know how to fix or reset it....I am trying to uninstall everything and reinstall it but I am having trouble figuring out what. I tried installing ruby via mac ports, which worked, but it was not sending the message to rbenv.
I suggest HomeBrew to install rbenv. It has worked better for me than MacPorts did.
This older post Modern Ruby Development (on Mac) is still very helpful.
The install for HomeBrew needs ruby, which will be a problem for you as you have lost ruby.
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
The rbenv lines you need in your .bash_profile, according to the rbenv install docs, are,
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Do not forget to say, source .bash_profile after editing the profile, or open a new terminal.
If your rbenv command is no longer available, you can install it directly from git with,
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
That is assuming the git command still works.
With the rbenv lines restored to your .bash_profile, you might be able to install a ruby with,
rbenv install 2.1.1
(rbenv install -l will list available versions. Any of the lines output from there can go after rbenv install.)
Finally, use rbenv global to set the default ruby to one of the rbenv installed ones.
rbenv global 2.1.1
You can view the current global ruby set for rbenv with,
rbenv global
As you know,
ruby -v
will get the version of the ruby that runs by default. You might not know that,
which ruby
will tell you where the shell finds that ruby command in your path.

does rbenv set the PATH variable to point to the correct ruby?

I installed rbenv and it seems and copied 2 versions of ruby to ~/.rbenv/versions, it shows
them both correctly.
When I run
rbenv global 1.8.7-p72
It says ok and also points to it. However when I run--
ruby --version
I get ruby not found. Basically, the ruby executable is not found in the path. Can anyone help me with this problem?
My .bash_profile is
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
rbenv versions outputs --
1.8.7-p72
*1.9.3-p448 ( set by /home/user/.rbenv/version)
Also my ~/.rbenv/shims directory doesn't have a ruby executable. It has erb, gem,irb , rake, rdoc , ri and testrb. Could this be the issue?
I'm sorry I'm completely clueless
Did you execute all the step in the installation instructions https://github.com/sstephenson/rbenv? I.e. did you add the required snippets to your shell startup (.bash_profile) and so forth?
If you followed the instructions, could you provide the output of rbenv versions and which -a ruby for a start?
EDIT:
rbenv rehash seems to be what generates the shims. This must be executed every time you install a new ruby version. How did you install the rubies? With ruby-build, as recommended in the installation instructions?
When I encountered this error, all I had to do was to make sure that $PATH had shims prior to system version of ruby like so:
~/.rbenv/shims:/usr/local/bin:/usr/bin:/bin

After installing jekyll, getting -bash: jekyll command not found

hey there jekyll users... I am new to using ruby/rvm and have followed the instructions to install it on my mac locally, and then installed the jekyll gem, but when i try to run jekyll commands I get
bash: jekyll: command not found
Any advice?
I know that when I installed rvm, (or maybe it was during the homebrew install), I received a notice that I have both .profile and .bash_profile and it asked me to add source ~/.profile to .bash_profile could that be the problem?
I've also tried installing using this command gem install --source http://rubygems.org jekyll
and it says it installs but when I try jekyll in the terminal, I keep getting command not found.
If there is any more information I need to post to help, please let me know.
I know that when I installed rvm, (or maybe it was during the homebrew install), I received a notice that I have both .profile and .bash_profile and it asked me to add source ~/.profile to .bash_profile could that be the problem?
Yes. Do what it said there and you should have access to the jekyll command.
user$ echo "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile
I read most of the solutions around the web and most of them seem to suggest a $PATH change. I don't like to edit my path unless I have to so I just did a symbolic link:
ln -s /usr/local/Cellar/ruby/2.0.0-p247/lib/ruby/gems/2.0.0/gems/jekyll-1.2.1/bin/jekyll
Here's an updated answer for 2020 (soon 2021).
One of the reasons you can get "command not found" after installing gems is because they were installed in a location that your bash shell does not yet know about. The way the computer looks up commands is by looking at the PATH, which is a list of folders where the computer should look for commands, such as jekyll.
The location of the gems depends on how you installed Ruby, and since there are many ways to install Ruby on a Mac, how to fix the "command not found" error will depend on how you installed Ruby. If you used a Ruby manager, such as RVM, rbenv, or chruby, then you might have missed a step in the installation instructions. They typically take care of the PATH for you automatically with a script. If you installed Ruby with Homebrew, then you'll need to add /usr/local/lib/ruby/gems/2.7.0/bin to your PATH, for example:
echo 'export PATH="/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/2.7.0/bin:$PATH"' >> ~/.bash_profile
(Replace .bash_profile with .zshrc if you are using zsh. If you're not sure which shell you're using, read this: https://www.moncefbelyamani.com/which-shell-am-i-using-how-can-i-switch/)
I strongly believe that there should only be one recommended way to install Ruby, which is with a Ruby manager. I prefer chruby because it's the lightest and easiest to use. I wrote a script that will automatically set up a proper Ruby environment for you with chruby, including updating your PATH and everything else that is necessary to be able to install gems and use them right away without getting any errors.
You can read more about my script and other ways to fix the "command not found" error in these articles:
https://www.moncefbelyamani.com/troubleshooting-command-not-found-in-the-terminal/
https://www.moncefbelyamani.com/how-to-install-jekyll-on-a-mac-the-easy-way/
https://www.moncefbelyamani.com/the-definitive-guide-to-installing-ruby-gems-on-a-mac/

irb not starting

I'm running Ruby 1.9.2 w/ rbenv. I recently moved my Users directory to a different drive. Ruby and irb show up using which.
$ which ruby
/Volumes/Pig/Users/arrow/.rbenv/shims/ruby
$ which irb
/Volumes/Pig/Users/arrow/.rbenv/shims/irb
However, running irb hangs indefinitely.
I've also been running ruby files under the current config but executing $ ruby -v also hangs.
rbenv rehash can help solve these sort of problems. Anytime you modify your Ruby installation you need to rerun that command to let rbenv rebuilt its soft-links.
From the command-line output:
rehash Rehash rbenv shims (run this after installing binaries)
You might also need to tell rbenv where your binaries are again, i.e., rbenv global 1.9.2... and let it adjust things.
Remember, rbenv assumed your binaries were on a different volume so it might have gotten confused and needed you to straighten it out.
The other issue could be that Ruby bound to files on the other volume at compile time (because that's what rbenv does, it compiles Ruby for you) so when the files couldn't be found the apps crashed. Reinstalling rbenv would fix that, as telling it to reinstall a particular Ruby should have fixed it.
I uninstalled/installed rbenv and all is well.

Installing Ruby gems not working with Home Brew

The gems I install via sudo gem install ... can't be executed (I get a command not found).
They seem to install into /usr/local/Cellar/ which is Brew's install directory (also, the gems in /Library/Ruby/ don't work either). Is there anything else I need to do to make the gems executable? I'm using ZSH on Mac OS X 10.6 with Ruby v1.8 for the one in Brew.
EDIT: It seems to be working now. I just went out for a few hours and came back to try it again.
Homebrew is nice. However unlike brew and npm, gem does not make aliases in /usr/local/bin automatically.
Solution
I went for a very simple approach (as of March 2020):
# Based on "`brew --prefix ruby`/bin"
export PATH=/usr/local/opt/ruby/bin:$PATH
# Based on "`gem environment gemdir`/bin"
export PATH=/usr/local/lib/ruby/gems/3.0.0/bin:$PATH
Add this to your .bashrc (or .bash_profile, .zshrc, etc.).
That's it! Now all Ruby bins and installed gems will be available from your shell!
In older versions of Homebrew (before 2017), there was a separate package for Ruby 2 called ruby20, for which you'd use the following snippet instead:
export PATH=/usr/local/opt/ruby20/bin:$PATH
This line was the only line needed at the time. But, in Ruby 2.1 the gems got moved to a separate directory. No longer under /usr/local/opt/ruby/bin, but instead at /usr/local/lib/ruby/gems/2.0.0/bin (where "2.0.0" is the last major Ruby version for Gem's purposes).
How it works
Homebrew keeps track of where it installed a package, and maintains a symbolic link for you that points there.
$ brew --prefix ruby
/usr/local/opt/ruby
$ l /usr/local/opt/ruby
/usr/local/opt/ruby# -> ../Cellar/ruby/2.5.3_1
Effectively, adding /usr/local/opt/ruby to PATH is the same as the following:
export PATH=/usr/local/Cellar/ruby/2.5.3_1/bin:$PATH
Except, this long version hardcodes the currently installed version of Ruby and would stop working next time you upgrade Ruby.
As for Gem, the following command will tell you the exact directory Gem adds new packages to:
$ gem environment gemdir
/usr/local/lib/ruby/gems/2.7.0
Tools
These tools were meant to automatically bridge between Homebrew and Gem:
josh/brew-gem (no longer exists)
indirect/brewbygems (unmaintained)
I haven't used these but they might work for you.
brew unlink ruby; brew link ruby might add symlinks to /usr/local/bin/:
$ which sass
$ brew unlink ruby; brew link ruby
Unlinking /usr/local/Cellar/ruby/2.0.0-p0... 20 links removed
Linking /usr/local/Cellar/ruby/2.0.0-p0... 31 symlinks created
$ which sass
/usr/local/bin/sass
brew --prefix ruby is still pretty slow, but you could also just add /usr/local/opt/ruby/bin to the path.
$ time brew --prefix ruby
/usr/local/opt/ruby
0.216
$ time brew --prefix ruby
/usr/local/opt/ruby
0.076
$ stat -f%Y /usr/local/opt/ruby
../Cellar/ruby/2.0.0-p0
I like home brew. There's probably a better way to do this, but if you run:
gem environment
That will print out a nice list of all the relevant paths. Look for the one labeled EXECUTABLE DIRECTORY. That's the one you want to add to your path. In my case that's /usr/local/Cellar/ruby/1.9.3-p362/bin/ruby but I would imagine it would change with newer version of Ruby.
I'm using /bin/bash as my shell, but the process of adding it to your path should be pretty much the name.
I use TextWrangler (via the command line tools) to edit my .profile file. To do that, it's just:
edit ~/.profile
When your done, either close your terminal and open a new one, or run:
source ~/.profile
Using the info in Timo's answer, I've got this:
PATH=/usr/local/opt/ruby/bin:$PATH
GEMSDIR=$(gem environment gemdir)/bin
PATH=$GEMSDIR:$PATH
export PATH
Works for Homebrew, works for the separate gems directory, and doesn't hardcode a Ruby version.
You can be fine with ruby installed by homebrew too.. You just lack the functionality of custom gemsets with homebrew.
first do:
sudo nano /etc/paths
this will bring up nano editor,
then add the following to the paths:
/usr/local/Cellar/ruby/1.9.3-p194/bin
your version of ruby will probably vary.
Thats it. It should now detect your gems.
Oh, btw, you need to Ctrl+X > y > ENTER to save a file in nano.
Instead of using => $(cd $(which gem)/..; pwd)
You could use this instead => $(brew --prefix ruby)/bin
I think this evolve a bit.
Just add
export PATH=/usr/local/opt/ruby/bin:$PATH
To your .bashrc (or .bash_profile, .zshrc/.bashrc, .. – whatever you use).
If you have a problem with ruby itself
brew unlink ruby
brew link ruby

Resources