Install Ruby on Rails with rbenv on AlmaLinux 9 or Rocky Linux 9
Install rbenv and ruby-build on AlmaLinux 9 or Rocky Linux 9, compile a current Ruby, install Rails 8 and test several Ruby versions side by side.
rbenv lets one user account hold several Ruby versions and switch between them per project, which is useful when you need to test a Rails application against more than one Ruby. This guide installs rbenv and its ruby-build plugin on AlmaLinux 9 or Rocky Linux 9 (it also applies to RHEL 9 and CentOS Stream 9), compiles a current Ruby, installs Rails 8 and runs a test application. CentOS 7 reached end of life in June 2024; the RHEL 9 rebuilds replace it.
Requirements
- AlmaLinux 9, Rocky Linux 9, CentOS Stream 9 or RHEL 9.
- A regular user with
sudorights, calleddeployhere. rbenv lives in that user's home directory; do not install it as root. - About 2 GB of RAM is comfortable for compiling Ruby. On smaller servers the build works but takes longer.
- Rails 8.1 requires Ruby 3.2 or newer. The Ruby 3.0 shipped in the RHEL 9 base repositories is too old, which is the main reason to build your own.
Step 1: Install the build dependencies
ruby-build compiles Ruby from source, so it needs a compiler and development headers. Update the system and install the packages that ruby-build lists for RHEL 9, plus Git and a C++ compiler for gems with native extensions:
sudo dnf update -y
sudo dnf install -y git make autoconf gcc gcc-c++ patch bzip2 openssl-devel libffi-devel readline readline-devel zlib-devel gdbm ncurses-devel tar perl-FindBinRuby's YAML support needs libyaml-devel, which sits in the CRB repository that is disabled by default:
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --set-enabled crb
sudo dnf install -y libyaml-develWithout it, the build ends with an error about the psych extension. Optionally install the Rust compiler, which Ruby needs to build its YJIT just-in-time compiler:
sudo dnf install -y rustStep 2: Install rbenv and ruby-build
Clone rbenv into your home directory and let it add itself to your shell start-up file:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
~/.rbenv/bin/rbenv initFor Bash, this appends an eval "$(~/.rbenv/bin/rbenv init - --no-rehash bash)" line to your profile. Load it by logging out and in again, or by starting a new login shell:
exec bash -lThe rbenv install command is provided by the ruby-build plugin. Install it into rbenv's plugin directory:
git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-buildOlder tutorials clone from git://github.com/sstephenson/... and add PATH lines by hand. GitHub no longer serves the unauthenticated git:// protocol and both projects now live under the rbenv organisation, so use the HTTPS URLs above.
Step 3: Install Ruby
List the latest stable release of each supported series:
rbenv install -lPick a version from that list and build it. This example uses 3.4.12; substitute the version you see. Compiling takes several minutes:
rbenv install 3.4.12Make it the default for your user and check it:
rbenv global 3.4.12
ruby --versionIf the build fails, the last lines of the output name a log file under /tmp; a missing -devel package is the usual cause.
Step 4: Install Rails
Install Rails as a gem. No sudo is needed because the gems are stored inside ~/.rbenv:
gem install rails
rbenv rehash
rails --versionrbenv rehash regenerates the shims, the small wrapper scripts in ~/.rbenv/shims that pass commands such as rails to the active Ruby version.
Step 5: Create and run a test application
Generate an application. Rails uses SQLite by default, so no database server is needed for a first test:
cd ~
rails new testapp
cd testapp
bin/rails db:createStart the development server on all interfaces and allow its port through firewalld for the duration of the test:
sudo firewall-cmd --add-port=3000/tcp
bin/rails server -b 0.0.0.0Open http://203.0.113.10:3000 in a browser; the Rails welcome page shows the Rails and Ruby versions in use. Stop the server with Ctrl+C. The firewall rule was added without --permanent, so it disappears on the next sudo firewall-cmd --reload. The development server is not meant to be exposed permanently.
Step 6: Work with several Ruby versions
Install a second Ruby next to the first. The gems are separate for each version, so install Rails again under it:
rbenv install 4.0.9
rbenv shell 4.0.9
gem install rails
rbenv shell --unsetChoose which version a project uses by writing a .ruby-version file in its directory:
cd ~/testapp
rbenv local 4.0.9
bundle install
bin/rails testUseful commands while switching:
| Command | Effect |
|---|---|
rbenv versions | Lists installed Rubies and marks the active one |
rbenv version | Shows the active version and what selected it |
rbenv global 3.4.12 | Sets the default for your user |
rbenv local 4.0.9 | Sets the version for the current directory |
rbenv uninstall 3.4.12 | Removes a version and its gems |
Updating rbenv and ruby-build
New Ruby releases only appear in rbenv install -l after you update ruby-build. Both tools are plain Git checkouts:
git -C ~/.rbenv pull
git -C "$(rbenv root)"/plugins/ruby-build pullTo move to a new Ruby patch release, install it with rbenv install, switch with rbenv global or rbenv local, and run bundle install in each project.
Frequently asked questions
Why not use the Ruby packages from dnf?
RHEL 9 provides Ruby as a base package and as AppStream module streams, but only one stream can be enabled system-wide at a time. That is fine for a server running one application, and less convenient when you need to compare versions. rbenv keeps every version in your home directory and switches per directory.
Do I need Node.js?
Not for a default Rails 8 application, which uses import maps and Propshaft instead of a JavaScript bundler. Install Node.js only if you generate the app with a bundler option.
Does rbenv work for a service started by systemd?
Yes, but systemd does not read your shell profile, so the shims are not on its path. Point the unit at the shim directly, for example /home/deploy/.rbenv/shims/bundle exec puma, and set WorkingDirectory to the application so its .ruby-version file is honoured.
How do I enable YJIT?
Install the rust package before running rbenv install; Ruby's build then includes YJIT. Check with ruby --yjit -v, which prints +YJIT when it is available. Rails enables YJIT automatically on recent Ruby versions when it is compiled in.