RBENV resolve duplicate directories for ruby-build - ruby

I recently updated my ruby version for the first time since installing rbenv. On my development machine I discovered that I needed to reinstall bundler and all my gems again for each application using bundle install.
Now, when I login to my production server I notice that I have a ruby-build directory in my home folder at ~/ruby-build and in .rbenv/plugins. How can I find out which one my server is using and so which one to run git pull and install the new version of ruby? Can I just delete the directory at ~/ruby-build and deal with the other? Doing some exploring I find I also have a ruby-build in usr/local/bin on my server but this is not a directory.
I am completely confused by all of this. Rbenv lives in my home directory, rather than in any system directory on the server. Does this matter? How does it still work on a server even if I am not logged in?
Running rbenv versions from my home directory on the server reports "The program 'rbenv' is currently not installed" but lists the versions installed on my development laptop.
My .bashrc on the sever contains this entry at the top
if [ -d "${RBENV_ROOT}" ]; then
export PATH="${RBENV_ROOT}/bin:${PATH}"
eval "$(rbenv init -)"
fi
My .bashrc on my development laptop contains this entry at the bottom.
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

Since you include that in your .bashrc, I imagine that if you were to echo $RBENV_ROOT in the terminal you'll find out which rbenv directory is the one being used.
Although, you say that the rbenv command doesn't work so perhaps $RBENV_ROOT is not defined. In that case, you can simply do something like RBENV_ROOT=$HOME/.rbenv/ before that conditional -- or whatever you like -- to set the rbenv directory it should use.
When you do that, to define $RBENV_ROOT, re-login to make the changes take effect, or re-source the .bashrc file with source ~/.bashrc.

Related

Cannot resolve "rbenv init" instruction for zsh and oh-my-zsh on Mac

I'm a Ruby developer setting up my local development environment on a new iMac running macOS High Sierra with iTerm2, Homebrew, rbenv, zsh and oh-my-zsh.
I used RVM before on my Linux machine, but want to switch to rbenv with this iMac because I've heard it's better in some ways, so I'm trying to make sure all is as it should be with my rbenv setup before I start downloading/installing Rails and other gems.
I'm running rbenv init, which, as I understand it, will tell me what I need to do next to get rbenv working. It displays this message:
# Load rbenv automatically by appending
# the following to ~/.zshrc:
eval "$(rbenv init -)"
So I went into my .zshrc file and added that line. My .zshrc file currently looks exactly like this (comments omitted):
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
export ZSH="/Users/christian/.oh-my-zsh"
plugins=(git)
source $ZSH/oh-my-zsh.sh
No matter how many times I restart my iTerm terminal and run rbenv init, it always gives me that same instruction, and nothing else.
The first line in the file used to be: export PATH=$HOME/bin:/usr/local/bin:$PATH but I changed it to the current line because another StackOverflow page suggested this to someone having a very similar problem, but it didn't help.
I don't understand why it keeps telling me to add something that's already in the .zshrc file, and I don't know whether I've successfully setup rbenv with everything and am ready to start developing in Ruby on Rails.
I've looked at lots of stuff about this on here and other sites, but I can't seem to find the answer. I would be very grateful if someone could tell me why it keeps giving me that same message? how I make it proceed with the instructions and tell me the next thing to do? and how I'll be able to tell for sure when my rbenv is properly setup for development?
I eventually ran curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash and it showed green for everything. It seems I had everything already setup correctly; the rbenv init interface was just confusingly telling me to do something I'd already done. I confirmed it was all working by downloading different versions of Ruby with rbenv install, switching between them with rbenv local and rbenv global, and making sure ruby -v always displayed whatever version I had selected.

How to install ruby system-wide using rbenv

You may think this is a classic "global" command question, but it is not.
I can set ruby globally by this:
rbenv global 2.5.1
However this makes ruby2.5.1 global for all users but not for the whole system. When an application in same server want to call ruby or access/pipe ruby, they get command not found error.
I believe we should have to install or symlink to /usr/bin or /usr/local/bin or something...
I couldn't find anything regarding to this. How can I make selected ruby version to be seen to other applications?
My only solution is to build ruby myself to system dir but this will conflict with rbenv.
Examples:
When postfix want to pipe an email to ruby it cannot find ruby even i set global.
When webmin trying to run a command via ruby it cannot find ruby too.
Workaround:
Adding /root/.rbenv/shims folder to the $PATH on executed script works as expected but in some cases it is not possible to directly modify $PATH. What is my option here?
rbenv is simply not designed to do support this, you can see the discussion surrounding this within this Github Issue. There are many technical considerations to take into account like permissions if you do this. I found another blog post outlining the process - System Wide Install With rbenv. Going to copy it into this answer incase the blog post goes away.
However, in the long run, you may find that it's simply easier to create or use Ruby packages, such as the BrightBox PPA ones.
Installing rbenv
Instead of the usual location of ~/.rbenv for single installs we'll
be installing to /usr/local. You can use a different path if you
want, but this is what I prefer.
cd /usr/local
git clone git://github.com/sstephenson/rbenv.git rbenv
chgrp -R staff rbenv
chmod -R g+rwxXs rbenv
Make sure the users that will use rbenv are part of the group you
associated with the rbenv folder.
Now we want to add the following code into each users ~/.profile,
~/.bash_profile, or ~/.zshenv depending on the environment. You
can also add it in /etc/skel/.profile or /etc/skel/.bash_profile
template files that are copied when new users are created.
export RBENV_ROOT=/usr/local/rbenv
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"
Installing ruby-build (optional)
Optionally, you can install the ruby-build plugin to save yourself
from building it yourself.
cd /usr/local/rbenv
mkdir plugins
cd plugins
git clone git://github.com/sstephenson/ruby-build.git
chgrp -R staff ruby-build
chmod -R g+rwxs ruby-build
Notes
Now you should have rbenv and optionally ruby-build setup so you can
get started installing and using Ruby. This install is the same as the
single user install with two exceptions. The global setting applies to
all users and single user rbenv installs can "override" the system
wide install.
If you have permission issues make sure all the files in the rbenv
folder belong to the proper group and that the users trying to use
rbenv are also members of the group.

RVM $PATH variables to homeless user

I have a staging server running Ubuntu 12.04 and I'm creating some post-receive scripts on git to automate deployment of some repositories we keep on that server.
I created a homeless user 'git' (for security reasons) to deal git's ssh connections.
I want to run compass compile (compass is installed as a ruby gem) when somebody pushes something on the repo.
So I used RVM to install and manage ruby on that server.
And I did all rvm installation process using the multi-user approach.
When I run ruby -v with the 'git' user, I get the right output, I mean, rvm multi-user instalation is almost ok.
The problem is when I try to run an executable gem.
I noticed my normal user's .bash_profile had this (added by rvm):
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" # Load RVM into a shell session *as a function*
This code add gems' bin directory to the $PATH.
[normaluser]$ echo $PATH
/usr/local/rvm/gems/ruby-1.9.2-p320/bin:/usr/local/rvm/gems/ruby-1.9.2-p320#global/bin:/usr/local/rvm/rubies/ruby-1.9.2-p320/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/rvm/gems/ruby-1.9.2-p320/bin/:/usr/local/rvm/bin
As 'git' user is homeless it doesn't have a .bash_profile so I added that code to a script on /etc/profile.d/
But it didn't worked because every time I try to execute /usr/local/rvm/scripts/rvm as git, it exit's the git's bash session and return to my normal user session. It goes something like:
[normaluser]$ sudo -u git bash
[git]$ exec /usr/local/rvm/scripts/rvm
[normaluser]$
No erros are printed...
Why is rvm script exiting from bash session?
How can I set rvm $PATH on a homeless user?
three issues:
you should not use /bin/sh it is not supported by RVM, use /bin/bash instead (but you got it already
as explained by #Casper => exec replaces current shell with the command you run, there is no return
there are docs describing how to use RVM in init.d => https://rvm.io/integration/init-d
you should not try to source rvm for the user in shell initialization files as they are not used by init.d scripts, use wrappers instead, it's the most proven way to make it work.

Is there a way to restore rvm installation if i have the .rvm folder backup?

I recently re-installed my operating system and before that i made a backup of my ~/.rvm folder that contains all my gems, ruby installations and configurations.
Doing all those steps from scratch is a bit difficult due to my internet being painfully sluggish.
So my question is:
Is there any way to have the same configuration as before using my backup of the ~/.rvm folder.
Any help appreciated.
Installation
Put your backup back to ~/.rvm. Be sure, that you setup rvm correctly with:
echo "source $HOME/.rvm/scripts/rvm" >> ~/.bash_profile
After a source ~/.rvm/scripts/rvm (or opening a new terminal), rvm should be back.
Test if it worked
To test your installation, do the following:
type rvm | head -n 1 # should return: "rvm is a function"
Note
Ruby and some gems have binaries specifically compiled for your system. If the installed libraries have changed (or are missing), expect errors. A reinstallation of the failing gem/ruby should help.

Ruby Version Manager broke my $PATH

I installed RVM on OSX and now my $PATH variable is totally changed. I tried to change it in .bashrc and .profile but it doesn't seem to have any effect. Any ideas what I'm doing wrong?
It looks like you have some misuse in your RC files, have a read here: https://rvm.io/support/faq/#shell_login
basically .profile should not be used as bash login RC script, there is .bash_login or .bash_profile which are better choice for it.
to fix your profile files you need to run:
rvm get stable --auto-dotfiles
and restart computer - yes restart the machine, it's only a minute on mac
I ran into this problem when I had an rvm binary in /opt/local/bin (a macport called 'rsync vault manager'). After uninstalling that, rvm stopped removing /opt/local/bin from my path.

Resources