How to Install Ruby with rbenv on RHEL 7
Ruby is a dynamic, expressive programming language favored for its clean syntax, productivity-oriented design, and thriving ecosystem. While RHEL 7’s system repositories provide a version of Ruby, it is typically outdated and may conflict with modern applications and gems that require a specific Ruby release. The recommended approach for developers is to manage Ruby versions using rbenv, a lightweight version management tool that allows you to install multiple Ruby versions and switch between them per-project or globally — without touching system Ruby. This tutorial covers installing rbenv from source on RHEL 7, compiling and installing a modern Ruby version, and setting up Bundler for dependency management.
Prerequisites
- RHEL 7 system with
sudoaccess or root login. - Git installed (
sudo yum install -y git). - Development Tools group and Ruby build dependencies installed (covered below).
- Internet access to clone rbenv from GitHub and download Ruby source packages.
- At least 500 MB of free disk space for the Ruby build.
Step 1: Install Build Dependencies
Ruby is compiled from source by rbenv’s ruby-build plugin, so your system needs a full C compiler toolchain plus the development headers for several libraries that Ruby links against.
sudo yum groupinstall -y "Development Tools"
sudo yum install -y git curl zlib-devel bzip2 bzip2-devel
readline-devel sqlite sqlite-devel openssl-devel
libffi-devel libyaml-devel gdbm-devel ncurses-devel
Each of these packages corresponds to a feature Ruby provides: openssl-devel enables HTTPS in the standard library, readline-devel enables the interactive REPL, and libyaml-devel is required for YAML parsing used heavily by Rails and other frameworks.
Step 2: Install rbenv
rbenv is installed by cloning its repository into ~/.rbenv. It works purely through PATH manipulation and shell hooks — there is no daemon or root-level component required.
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Optionally compile the dynamic bash extension for faster rbenv performance:
cd ~/.rbenv && src/configure && make -C src
Add rbenv to your PATH and initialize it by appending the following lines to ~/.bashrc:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
Reload your shell configuration:
source ~/.bashrc
Verify rbenv is working:
rbenv --version
rbenv 1.2.0
Step 3: Install the ruby-build Plugin
ruby-build is an rbenv plugin that provides the rbenv install command, handling the download, configuration, and compilation of any Ruby version. Clone it into rbenv’s plugins directory:
git clone https://github.com/rbenv/ruby-build.git
~/.rbenv/plugins/ruby-build
The plugin is immediately available — no further configuration is needed.
Step 4: List Available Ruby Versions
To see all Ruby versions that ruby-build can install:
rbenv install --list
For only stable release versions (filtering out dev/preview builds):
rbenv install --list | grep -v - | grep '^s*[0-9]'
This shows versions like 3.1.4, 3.2.3, 3.3.1, etc. Choose the latest 3.1.x release for compatibility with most current gems.
Step 5: Install Ruby 3.1.x
Compile and install Ruby 3.1.4 (replace the version number with the latest available in your --list output):
rbenv install 3.1.4
The compilation process takes 5–15 minutes depending on your server’s CPU. You will see output as each component compiles:
Downloading ruby-3.1.4.tar.gz...
Installing ruby-3.1.4...
Installed ruby-3.1.4 to /home/user/.rbenv/versions/3.1.4
If the build fails, the most common cause is a missing development header. Review the error message and install the corresponding -devel package with yum.
Step 6: Set a Global Ruby Version
Set Ruby 3.1.4 as the default version for all shells and projects that do not have a project-specific override:
rbenv global 3.1.4
Verify the active version:
ruby --version
ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [x86_64-linux]
Run the rbenv doctor to confirm the installation is healthy:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-doctor | bash
Step 7: Use rbenv versions and Per-Project Overrides
To list all installed Ruby versions and see which is currently active:
rbenv versions
2.7.8
* 3.1.4 (set by /home/user/.rbenv/version)
3.2.3
To set a different Ruby version for a specific project directory, create a .ruby-version file in the project root:
cd /var/www/myapp
rbenv local 3.2.3
cat .ruby-version
3.2.3
When you cd into this directory, rbenv automatically switches to 3.2.3. Outside of it, your global version applies.
Step 8: Install Bundler
Bundler is the standard Ruby dependency manager. It reads a Gemfile in your project directory and ensures consistent gem versions across all environments. Install it as a gem:
gem install bundler
After installing any gem that provides a command-line executable, run rbenv rehash to update rbenv’s shims directory:
rbenv rehash
Verify Bundler is available:
bundler --version
Bundler version 2.4.22
Step 9: Working with a Gemfile
A Gemfile declares your application’s gem dependencies. Create a new project directory and initialize a Gemfile:
mkdir ~/myrubyapp && cd ~/myrubyapp
bundle init
Edit the generated Gemfile to add dependencies:
source "https://rubygems.org"
gem "sinatra", "~> 3.0"
gem "puma", "~> 6.0"
gem "rake"
Install all declared gems:
bundle install
Bundler resolves version constraints and writes a Gemfile.lock that pins exact versions — this lock file should be committed to version control so all developers and deployment environments use identical gem versions.
To run a command in the context of your bundle (using only the gems declared in the Gemfile):
bundle exec ruby app.rb
bundle exec rake db:migrate
Step 10: Updating rbenv and ruby-build
Because rbenv and ruby-build are git repositories, updating them is a simple pull:
cd ~/.rbenv && git pull
cd ~/.rbenv/plugins/ruby-build && git pull
After updating ruby-build, new Ruby versions become available for installation via rbenv install.
You now have a complete, production-ready Ruby version management setup on RHEL 7. rbenv gives you the flexibility to run different Ruby versions for different projects on the same server without interference, while ruby-build handles the compilation details transparently. Combined with Bundler’s precise dependency locking, this setup provides a reliable and reproducible foundation for Ruby application development and deployment. As a next step, consider installing Rails or Sinatra and exploring the broader Ruby gem ecosystem on crates.io’s Ruby equivalent, RubyGems.org.