Replacing RVM with rbenv in OS X
rbenv vs. RVM
Both rbenv and RVM are Ruby version management tool. RVM is more resourceful but rbenv is lightweight which makes it a strong contender for RVM. RVM is used to manage and install different versions of Ruby and gemsets on system where Rbenv is a lightweight Ruby version management tool. Compared to RVM, rbenv does not manage gemsets nor install different Ruby versions. It only manages different ruby versions. These tasks are now better off handled by other tools such as Bundler which is a much better tool for handling dependencies, and many gems these days are already using it. Starting a project is easy as typing bundle install
. This makes rbenv more developer friendly.
Installing rbenv
Before installing rbenv , it’s very important to remove RVM first from your system because it is incompatible with rbenv. To do this, just type this command:
$ rvm implode
This will remove all installations of Ruby it manages and everything in ~/.rvm
. If ~/.rvm
folder still exist then we have to manually remove the folder. This folder exists in /Users/username/
directory. Now manually remove this folder. After deleting this folder we have to delete or comment out the lines present in .bashrc and .bash_profile which contains RVM-specific settings. After removing or commenting these lines we are good to install rbenv. To install rbenv type the following commands:
$ brew update
$ brew install rbenv ruby-build
These commands will install the rbenv and ruby-build using Homebrew package. After this we need to add rbenv to the PATH environment. These paths will set the required PATH for rbenv. To add rbenv to the PATH environment use following commands:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
Installing and managing different Ruby Versions
Installing ruby is not the work of rbenv, it just manages the different ruby versions in different sub-folders. However, in some commands it implicitly runs the command to install ruby but installing ruby is not the function of rbenv.
# list all available versions:
$ rbenv install -l
Above command will list all the ruby versions available. Which will look like as shown in the below image.
To install a ruby version, type the following command which will check if the specified ruby version is present or not if it is not present it implicitly run the command to install ruby with specified version in this case it is 2.4.2 and put this version folder into a subfolder inside ~/.rbenv/versions folder.
# install a specific Ruby version:
$ rbenv install 2.4.2
As an alternative, we can also download and install Ruby manually by installing it in a subdirectory in ~/.rbenv/versions/
. Each subfolder in this directory will be treated as a separate Ruby version.
There can be many ruby version installed on a system but we can only select one ruby version to use globally. Following command is used to select the ruby version globally.
# To select a particular ruby version to be used globally:
$ rbenv global 2.4.2
Now, to check for the ruby version being used type the following command:
# install a specific Ruby version:
$ ruby -v
which will give output :
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-darwin16]
So, ruby 2.4.2 is currently being used. To change the ruby version just type the above command. Lets say to change the current used ruby version to 2.3.1, type the following command but make sure that ruby version is installed:
# To select a particular ruby version to be used globally:
$ rbenv global 2.3.1
Another way to check the ruby version which is currently being used we can look into the file name version present in ~/.rbenv where ruby version which is currently being used is written. To check for all the ruby versions that are installed on the system go to the ~/.rbenv/versions which contains different folder and each folder corresponds to particular ruby version. As shown in the snap below which is showing the ruby versions that are installed currently in the system are 2.3.0, 2.3.1, 2.3.3, 2.4.1 and 2.4.2.
Now, desired version of ruby is installed! Happy Coding!!!!