Share This Article
In this tutorial, we will learn how to install Ruby with rbenv.
rbenv is a tool that helps you manage & switch between different ruby versions. It is similar to Virtualenv for python or RVM for Ruby. It’s handy because it’s easy to use, it’s fast, & it’s cross-platform (Linux, macOS, & Windows).
It is useful when you want to:
- Work on multiple projects that use different ruby versions.
- Upgrade or downgrade the ruby version on your machine.
- Install multiple Ruby versions on the same machine.
How to install rbenv
The first thing you need to do is to install rbenv. The easiest way to do this is using the Homebrew package manager for macOS. If you’re on another operating system, you can follow the instructions on the rbenv website.
With Homebrew installed, you can install rbenv with the following command:
brew install rbenv
Once rbenv is installed, you must add the rbenv initialization lines to your shell configuration file.
If you’re using zsh, you need to open the .zshrc
file & add these lines:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
If you’re using bash, you need to open the .bash_profile
file & add these lines:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
If you’re using fish shell, you need to open the config.fish
file & add these lines:
set PATH $HOME/.rbenv/bin $PATH
status --is-interactive; and . (rbenv init -|psub)
Save the file & restart your shell.
Installing rubies with rbenv
rbenv provides a handy command called ‘rbenv install’ that helps you install rubies.
For example, to install ruby 2.6.1, you can run this command:
rbenv install 2.6.1
rbenv will compile & install ruby 2.6.1 in your home directory.
You can list all the rubies that rbenv knows about with the following command:
rbenv install --list
You can also install specific versions of rubies:
rbenv install 2.6.1-debug
You just need to specify a version & rbenv will download, compile & install it for you.
Uninstalling rubies with rbenv
What about uninstalling rubies?
rbenv will not delete the Ruby version for you. You need to do that manually if you want to eliminate it.
To do that, you need to remove the Ruby directory from your home directory.
How to Use Rbenv in Ruby
The best way to use rbenv is to install all the Ruby versions you need in your project & then use the rbenv local command to set the Ruby version for your project.
For example, if you have Ruby 2.6.1 and Ruby 2.5.3 installed, you can set the ruby version for your project with this command:
rbenv local 2.5.3
This will create a file called .ruby-version
in your project with the ruby version you have selected.
Now, when you open the project in your text editor, rbenv will automatically switch to the correct Ruby version.
If you have different ruby versions in other projects, you can use the rbenv global command to set the global Ruby version that will be used when there’s no project-specific ruby version set.
Switching Between Ruby Versions
rbenv provides a bunch of commands to help you manage your rubies:
rbenv versions
– List the installed ruby versionsrbenv global
– Set the global ruby versionrbenv local
– Set the ruby version for the current projectrbenv shell
– Set the ruby version for the current shell session
You can learn more about these commands on the rbenv website.
Conclusion
rbenv is an excellent tool for managing ruby versions, it’s cross-platform & it’s easy to use. You can use it to manage multiple ruby versions on your computer & specify the ruby version for each project.
You can learn more about rbenv on the rbenv website.
Resources
Here are some valuable resources if you want to learn more about rbenv:
That’s it for now! Thanks for reading! Until next time!