Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ammarshah/b78f09fcd526a7c5db3a7273d29d0032 to your computer and use it in GitHub Desktop.
Save ammarshah/b78f09fcd526a7c5db3a7273d29d0032 to your computer and use it in GitHub Desktop.
Setup rbenv on Ubuntu 24.04 and use .ruby-version file to install the version your project requires

How to setup rbenv on Ubuntu 24.04

This guide will not install a specific Ruby version you want, although, it will setup rbenv so you can manage multiple Ruby versions.

It assumes that the project that requires a specific Ruby version has a file .ruby-version in the root of the project. So, simply running rbenv install from project root directory will use the version specified in that file and install it.

But if you still want to install a specific version of Ruby without using .ruby-version file, you can do so using:

$ rbenv install 3.4.1

1. Installing rbenv

This will get you going with the latest version of rbenv without needing a system-wide install.

  1. Clone rbenv into ~/.rbenv.

    $ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
  2. Set up your shell to load rbenv.

    $ ~/.rbenv/bin/rbenv init
  3. Restart your shell so that these changes take effect. (Opening a new terminal tab will usually do it.)

2. Installing ruby-build

The rbenv install command does not ship with rbenv out-of-the-box, but is provided by the ruby-build plugin.

To install ruby-build, run the following command:

$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build

3. Installing all requisite libraries

Before attempting to install Ruby, check that your build environment has the necessary tools and libraries.

$ sudo apt-get install autoconf patch build-essential rustc libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libgmp-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev uuid-dev

4. Installing Ruby

Let's say, you are in the root of your project directory (my_app) and a file .ruby-version exists with the following version:

# my_app/.ruby-version
3.4.1

Now, running the following command will automatically look for the version specified in .ruby-version and install it:

$ rbenv install

5. Verifying Ruby version

From your project directory (my_app), run the following command to verify the version rbenv is currently using for this project among other rubies you have previously installed (if any):

$ rbenv versions
  3.4.0
* 3.4.1 (set by /home/ammar/workspace/projects/my_app/.ruby-version)

The asterisk (*) in the above output indicates the currently active version.

Bonus

Setting a specific Ruby version as global

$ rbenv global 3.4.1

Setting a specific Ruby version in current shell (temporary)

$ rbenv shell 3.4.1

Removing a specific Ruby version

To remove old Ruby versions, simply rm -rf the directory of the version you want to remove. You can find the directory of a particular Ruby version with the rbenv prefix command, e.g. rbenv prefix 3.4.0.

$ rbenv versions
  3.4.0
  3.4.1

$ rbenv prefix 3.4.0
/home/ammar/.rbenv/versions/3.4.0

$ rm -rf /home/ammar/.rbenv/versions/3.4.0

$ rbenv versions
  3.4.1 

Upgrading rbenv

$ git -C "$(rbenv root)" pull

Upgrading ruby-build

$ git -C "$(rbenv root)"/plugins/ruby-build pull
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment