
How to Install Ruby on Rails with rbenv on Ubuntu 24
Ruby on Rails remains one of the most popular web frameworks for rapid application development, and proper environment management is crucial for maintaining clean, isolated development setups. Using rbenv to manage Ruby versions provides developers with the flexibility to work on multiple projects with different Ruby requirements without conflicts. This guide will walk you through installing Ruby on Rails with rbenv on Ubuntu 24, covering the complete setup process, troubleshooting common issues, and implementing best practices for a production-ready development environment.
How rbenv Works and Why Use It
rbenv operates by intercepting Ruby commands using shim executables injected into your PATH, then determining which Ruby version to execute based on your project’s requirements. Unlike RVM, rbenv doesn’t override shell commands or require sourcing scripts, making it lightweight and less intrusive to your system.
The tool works through a simple precedence hierarchy:
- RBENV_VERSION environment variable
- .ruby-version file in current and parent directories
- ~/.rbenv/version global version file
- System Ruby installation
Feature | rbenv | RVM | asdf |
---|---|---|---|
Memory overhead | Minimal | High | Low |
Shell integration | PATH only | Heavy modification | PATH + hooks |
Multiple languages | Ruby only | Ruby only | Multiple |
Learning curve | Simple | Moderate | Moderate |
Prerequisites and System Preparation
Before installing rbenv, ensure your Ubuntu 24 system has the necessary development tools and dependencies. Start by updating your package index and installing essential build tools:
sudo apt update
sudo apt install -y git curl libssl-dev libreadline-dev zlib1g-dev \
autoconf bison build-essential libyaml-dev \
libreadline-dev libncurses5-dev libffi-dev libgdbm-dev
These packages provide the foundation for compiling Ruby from source and supporting various gems that require native extensions. The build-essential package includes GCC and other compilation tools, while the various lib packages provide headers for Ruby’s optional features.
Installing rbenv and ruby-build
Clone rbenv directly from the official repository to ensure you get the latest stable version:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
Add rbenv to your PATH by modifying your shell profile. For bash users:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
For zsh users:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
Reload your shell configuration:
source ~/.bashrc # or source ~/.zshrc
Install ruby-build as an rbenv plugin to enable Ruby installation capabilities:
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
Verify the installation by checking rbenv status:
rbenv doctor
This command will diagnose common installation issues and verify that rbenv is properly configured.
Installing Ruby and Rails
List available Ruby versions to choose an appropriate release:
rbenv install -l
Install Ruby 3.2.0 (or your preferred version):
rbenv install 3.2.0
The compilation process typically takes 5-15 minutes depending on your system specifications. Set the newly installed Ruby as your global default:
rbenv global 3.2.0
rbenv rehash
Verify the installation:
ruby -v
which ruby
The output should show your installed Ruby version and a path within ~/.rbenv/shims/.
Now install Rails using gem:
gem install rails -v 7.1.0
rbenv rehash
The rehash command updates rbenv’s shims to include newly installed executables.
Configuration Best Practices
Configure gem installation to skip documentation generation, which significantly speeds up the process:
echo 'gem: --no-document' >> ~/.gemrc
For production environments, consider installing gems to a project-specific location using Bundler:
bundle config set --local path 'vendor/bundle'
Set up project-specific Ruby versions by creating .ruby-version files in your project directories:
cd /path/to/your/project
echo "3.2.0" > .ruby-version
This ensures consistent Ruby versions across team members and deployment environments.
Testing Your Installation
Create a simple Rails application to verify everything works correctly:
rails new test_app
cd test_app
bundle install
rails server
Navigate to http://localhost:3000 to see the Rails welcome page. For VPS deployments, you’ll need to bind to all interfaces:
rails server -b 0.0.0.0
Common Issues and Troubleshooting
Several issues commonly arise during rbenv and Rails installation:
Missing development headers: If Ruby compilation fails with errors about missing headers, install additional development packages:
sudo apt install -y libssl-dev libreadline-dev zlib1g-dev
PATH issues: If rbenv commands aren’t found, ensure your shell profile contains the correct PATH modifications and you’ve reloaded your configuration.
Permission errors: Never use sudo with rbenv or gem commands, as this installs software in system locations rather than your user directory.
Slow gem installation: Network connectivity or gem mirror issues can slow installations. Try switching to a different gem source:
gem sources --remove https://rubygems.org/
gem sources --add https://gems.ruby-china.com/
Native extension compilation failures: Some gems require specific system libraries. Install the ubuntu-dev-tools package for comprehensive development support:
sudo apt install -y ubuntu-dev-tools
Performance Optimization and Advanced Configuration
Optimize Ruby compilation by utilizing multiple CPU cores:
export MAKE_OPTS="-j$(nproc)"
rbenv install 3.2.0
For faster Rails development, enable bootsnap and spring gems in your Gemfile:
gem 'bootsnap', require: false
gem 'spring', group: :development
Configure Rails for development with PostgreSQL on your dedicated server:
sudo apt install -y postgresql postgresql-contrib libpq-dev
gem install pg
Set up database configuration in config/database.yml:
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password: secure_password
host: localhost
Real-World Use Cases and Integration
rbenv excels in several production scenarios:
Multi-project development: Agencies managing multiple client applications can maintain separate Ruby versions per project without conflicts.
Continuous Integration: rbenv integrates seamlessly with CI/CD pipelines, allowing testing against multiple Ruby versions:
#!/bin/bash
# CI script example
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
rbenv install $(cat .ruby-version)
rbenv local $(cat .ruby-version)
bundle install
bundle exec rspec
Production deployment: Use rbenv with deployment tools like Capistrano for consistent Ruby versions across environments.
For containerized deployments, rbenv works well in Docker environments:
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y git curl build-essential
RUN git clone https://github.com/rbenv/rbenv.git /root/.rbenv
ENV PATH="/root/.rbenv/bin:$PATH"
RUN eval "$(rbenv init -)" && rbenv install 3.2.0
Security Considerations
Maintain security best practices when using rbenv:
- Regularly update rbenv and ruby-build plugins to receive security patches
- Verify Ruby installations using checksums when available
- Use bundler-audit to scan for vulnerable gems
- Keep Rails updated to the latest patch versions
Update rbenv and its plugins periodically:
cd ~/.rbenv && git pull
cd ~/.rbenv/plugins/ruby-build && git pull
Install bundler-audit for dependency vulnerability scanning:
gem install bundler-audit
bundle audit
This comprehensive setup provides a robust foundation for Ruby on Rails development on Ubuntu 24. The combination of rbenv’s simplicity and Rails’ productivity makes this stack ideal for both development and production environments. For additional information about Ruby version management, consult the official rbenv documentation and the Rails Guides for framework-specific guidance.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.