Installing Ruby using Rbenv

Rbenv is a lightweight Ruby version management tool which allows you to easily switch Ruby versions. By default Rbenv doesn’t handle installing Ruby versions so we also need to install ruby-build which is a tool that helps you to install any version of Ruby you may need. It is available as a standalone program and as a plugin for rbenv.

To install Ruby using the Rbenv script, follow these steps:

  1. First, update the packages index and install the packages required for the ruby-build tool to build Ruby from source:
    sudo apt update
    sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev
  2. Next, run the following curl command to install both rbenv and ruby-build:
    curl -sL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash -

    The script will clone both rbenv and ruby-build repositories from GitHub to ~/.rbenv directory. The installer script also calls another script which will try to verify the installation. The output of the script will look something like below:

  3. Add $HOME/.rbenv/bin to the user PATH.If you are using Bash, run:
    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(rbenv init -)"' >> ~/.bashrc
    source ~/.bashrc

    Copy

    If you are using Zsh run:

    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
    echo 'eval "$(rbenv init -)"' >> ~/.zshrc
    source ~/.zshrc

    Copy

  4. Install the latest stable version of Ruby and set it as a default version with:
    rbenv install 2.5.1
    rbenv global 2.5.1

    To list all available Ruby versions you can use: rbenv install -l

    Verify that Ruby was properly installed by printing the version number:

    ruby -v
    ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

Leave a Reply

Your email address will not be published. Required fields are marked *