Install a Ruby version manager
It is possible to install Ruby itself as a standalone package, but this creates a small problem: It common to have multiple projects that require different versions of Ruby.
To the rescue rbenv
You can install rbenv by running
$ brew install rbenv
We’re not done though!
We still need to do two things:
- Configure your shell to work with rbenv
- Install a version of Ruby
rbenv init
In order to make $ ruby
do different things depending on what directory you are in rbenv
provides a command that outputs autogenerated bash code.
Have a look yourself! - $ rbenv init -
This is great but you will want to get bash to execute that code, in your current session.
There is a man page for bash builtins man builtins
, you can also find zsh builtin extensions in man zshbuiltins
.
For now we’re only interested in eval
, which comes from the core bash builtins.
Combining eval with another command
$ eval "$(rbenv init -)"
You don’t need to fully understand this yet, but to break it down:
- The parenthesis creates a subshell to run a command
- The $ does Command Expansion, which will turn the output of the subshell from output into literal commands
- The double-quotes will convert the output of the expanded subshell into a single parameter for eval
We’re not done yet!
Running $ eval "$(rbenv init -)"
will work, but it isn’t persistent.
We want it to run automatically every time we restart our machines and open a Terminal.
There’s a useful file ~/.zshrc
or ~/.bash_profile
that contains commands that are run automatically.
Go ahead and add the command to the bottom of ~/.zshrc
!
If you make changes to this file, you can reload it by running
$ source ~/.zshrc
Stop and understand
Phew! That was a lot.
- What does
~
mean in a path? - What does source do? Is it a command or a builtin?
To Do
- Install rbenv
- Add rbenv to your zshrc file