Ruby Environment Management 101
Tags: ruby, rails, management
Published: 2020-03-20 12:40:17

An automated Ruby environment management is necessary for developers to easily work on multiple Ruby programs on the same machine.

You can read this whole article from our [original blog post](https://hixonrails.com/ruby-on-rails-tutorials/ruby-environment-management/). It's a bit prettier.

In the Ruby community, there are two popular managers responsible for installing and maintaining multiple Ruby versions - [RVM](https://github.com/rvm/rvm) and [Rbenv](https://github.com/rvm/rvm).

This tutorial will guide you on choosing the best one and configuring your own environment. 

##What is Ruby Environment Management?

If you are actively working on programs written in Ruby, at some point you are going to install multiple versions of the language on your system.

Ruby environment managers such as RVM and Rbenv allow you to do just that.

It is a generally accepted practice to include the .ruby-version file in the root of Ruby-written programs and applications, such as Ruby on Rails projects. The version file includes a specific Ruby version with which the program is supposed to work.

'.ruby-version'

```ruby
2.6.5
```

Both RVM and Rbenv respect the aforementioned Ruby version, and upon navigating to the root of Ruby program that includes the file, they inform you that a particular Ruby version has to be installed in case it is missing on your system.

##RVM vs Rbenv - which Ruby Version Manager should I use?

The short answer for developers is: it does not really matter, just pick one and be done with it - both RVM and Rbenv get the job done.

The long answer is: it depends.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/e9eucy4juy9zzld93d4o.png)

One factor in favor of Rbenv over RVM is that it receives more love on Github. But let's get to the facts.

RVM pros over Rbenv:

- RVM is easier to install than Rbenv,
- RVM has more features than Rbenv,
- RVM includes a built-in Ruby installation mechanism while Rbenv does not.

Rbenv pros over RVM:

- Rbenv is lightweight, RVM is heavier,
- Rbenv is more developer-friendly than RVM,
- Rbenv has a dedicated plugin for Ruby installation mechanism, RVM has it built-in
- [Rbenv main sell point is that its lightweight in comparison with RVM](https://github.com/rbenv/rbenv-installer), while still providing just enough functionality for the Ruby development.

Now that we definitely have not decided which one to use leaving this decision to you, follow the installation instructions of the Ruby environment manager of your choice.

##RVM installation

Depending on your operating system, follow the instructions below in order to install the RVM Ruby Environment Manager.

###Ubuntu

RVM has a dedicated package for Ubuntu. In order to use it, you are going to be able to add stuff to PPA.

```bash
sudo apt-get install software-properties-common
sudo apt-add-repository -y ppa:rael-gc/rvm
sudo apt-get update
sudo apt-get install rvm
```

The next step is configuring the terminal to always perform login in order to always load the RVM.

At the terminal window:

1. Navigate to Edit / Profile Preferences ,
2. Go to the Title and Command tab,
3. Check Run command as login shell

The last step is to reboot your machine.

###Other operating systems

In order to install RVM, you are going to need the following packages: curl and gpg2. After confirming their presence, simply run

```bash
curl -sSL https://get.rvm.io | bash -s stable
```

Confirm your installation running the `rvm install ruby` command.

##RVM overview

As stated before, RVM has more features built-in that Rbenv does.

In order to browse all the RVM commands, run either `rvm --help` or `man rvm` in your CLI.

Basic command-line RVM usage can be boiled down to this set of commonly used commands:

- `rvm install <version>` installs given Ruby version on your system,
- `rvm remove <version>` removes given Ruby version from your system,
- `rvm list` lists all Ruby versions installed on your system,
- `rvm list known` lists all available Ruby versions that you can install with rvm install command,
- `rvm current` displays currently used Ruby version
- `rvm use <version>` changes the currently used Ruby version to the given one
- `rvm use default <version>` sets the Ruby version that your system uses by default

The last two commands are useful to remember whenever you start the development of a new Ruby-written program. In most cases, it's best to use the latest stable version, which is always installable via the `rvm install ruby --latest` command.

##Rbenv installation

Based on your operating system, follow the instructions below in order to install the Rbenv Ruby Environment Manager.

###macOS

Installing the Rbenv on macOS is really easy and can be done using Homebrew.

```
brew install rbenv
rbenv init
```

After running the commands, close and open your terminal for changes to take effect.

The Ruby installation plugin is included by default, so you can now install any Ruby version using the `rbenv install X.Y.Z` command.

###Linux

In order to set up Rbenv on Debian based system, we are going to install all Ruby dependencies first.

```bash
sudo apt update
sudo apt install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm5 libgdbm-dev
```

For OSs other than Debian, the same might be done using other system package managers.

Next, we are going to clone the official Rbenv repository into the home directory and add its binary to `$PATH` in order to use `rbenv` command line utility.

```bash
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
```

The next step is to add the following command to your shell configuration file in order to load Rbenv automatically when the CLI starts.

~/.zshrc or ~/.bashrc

```bash
eval "$(rbenv init -)"
```

The last step is installing Ruby installation Rbenv plugin by simply cloning its official Github repository into the correct path.

```bash
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
```

That's it, Rbenv is ready to use. You can install the latest stable Ruby version using the `rbenv install -l` command.

##Rbenv overview

In order to use Rbenv via CLI, you need to have the aforementioned [ruby-build](https://github.com/rbenv/ruby-build) command-line utility installed.

In order to browse all the Rbenv commands available, run the `rbenv --help` method.

There are basic Rbenv commands that you are going to use at some point of Ruby development:

- `rbenv install <version>` installs given Ruby version,
- `rbenv global <version>` sets the default system Ruby version,
- `rbenv version` displays the currently used Ruby version of your system,
- `rbenv install --list` option lists all available versions of Ruby.

Whenever you start to develop the new Ruby program, it is worth using the latest stable Ruby version, which you can easily install using the `rbenv install -l` command.

##Conclusion

Ruby Environment Management is a must-have for every Ruby developer.

Ruby community provides two very popular Ruby environment managers, Rbenv and RVM. Both of them are pretty easy to install and get the job done.

If you have any questions, leave a comment!