How to Install Ruby on Rails on Ubuntu 24.04

Updated on January 17, 2025
 How to Install Ruby on Rails on Ubuntu 24.04 header image

Introduction

Install Ruby on Rails on Ubuntu 24.04 to develop scalable and maintainable web applications with ease. Ruby on Rails is a robust web application framework that utilizes the Model-View-Controller (MVC) architecture and adheres to the principle of convention over configuration, minimizing repetitive setup tasks by enforcing standard coding and file organization practices.

Ruby on Rails also integrates with custom tools for tasks such as database migrations and asset management with a wide range of community-developed libraries to run production-ready applications.

The article explains how to install Ruby on Rails on Ubuntu 24.04 and set up a development environment to run web applications.

Install Mise and Required Dependencies for Ruby

Mise is a Ruby version manager used to install and manage packages in Ruby environments. It offers a simplified setup process, enabling you to switch between Ruby versions without complex configurations. Ruby requires a set of dependencies that include essential libraries for encryption, file compression, and other critical functionalities to run applications. Follow the steps below to install Mise and all required dependency packages for Ruby.

  1. Update the server's package index.

    console
    $ sudo apt update
    
  2. Install all required dependency packages for compiling Ruby.

    console
    $ sudo apt install build-essential rustc libssl-dev libyaml-dev zlib1g-dev libgmp-dev -y
    
  3. Download and run the latest Mise installation script.

    console
    $ curl https://mise.run | sh
    
  4. Run the following command to append the .bashrc file and enable Mise in your shell environment.

    console
    $ echo 'eval "$(~/.local/bin/mise activate)"' >> ~/.bashrc
    
  5. Reload the .bashrc shell configuration to activate Mise.

    console
    $ source ~/.bashrc
    
  6. View the installed Mise version.

    console
    $ mise --version
    

    Your output should be similar to the one below.

    2024.12.7 linux-x64 (d21597c 2024-12-12)

Install Ruby and Rails on Ubuntu 24.04

RubyGems is a Ruby package manager that installs and distributes Ruby libraries (gems) efficiently with all required dependencies on a server. Follow the steps below to install RubyGems using Mise, Ruby, and Rails. In addition, install Node.js to handle JavaScript runtime execution for tasks like asset compilation and front-end integration.

  1. Install and activate Ruby globally using the Mise version manager.

    console
    $ mise use --global ruby@3
    
  2. Verify the installed Ruby version.

    console
    $ ruby --version
    

    Output:

    ruby 3.3.6 (2024-11-05 revision 75015d4c1f) [x86_64-linux]
  3. Update RubyGems to the latest version.

    console
    $ gem update --system
    
  4. Install and activate a Node.js version such as 22.11.0 globally on the system using Mise.

    console
    $ mise use --global node@22.11.0
    
  5. View the installed Node.js version.

    console
    $ node -v
    

    Output:

    v22.11.0
  6. Install Rails using RubyGems.

    console
    $ gem install rails
    
  7. View the installed Rails version.

    console
    $ rails -v
    

    Your output should be similar to the one below:

    Rails 8.0.0

Bootstrap and Run a Ruby on Rails application

Ruby on Rails supports command line interface (CLI) options to create, configure, and manage Rails applications on the server. The rails new command initializes a new Rails application while the rails server command starts a local web server to run the application and listen for incoming connection requests. Follow the steps below to bootstrap and run a Ruby on Rails application on your server.

  1. Create a new demo-app Rails application.

    console
    $ rails new demo-app
    

    The above rails new command creates a new demo-app project directory with the following structure:

    demo-app/
        ├── app
        ├── bin
        ├── config
        ├── config.ru
        ├── db
        ├── Dockerfile
        ├── Gemfile
        ├── Gemfile.lock
        ├── lib
        ├── log
        ├── public
        ├── Rakefile
        ├── README.md
        ├── script
        ├── storage
        ├── test
        ├── tmp
        └── vendor
  2. Switch to the demo-app directory.

    console
    $ cd demo-app
    
  3. List files in the directory and verify the available application files.

    console
    $ ls
    
  4. View the UFW status and verify that the firewall is active.

    console
    $ sudo ufw status
    
  5. Allow the default Rails development server port 3000 through the firewall configuration.

    console
    $ sudo ufw allow 3000/tcp
    
  6. Reload UFW to apply the firewall configuration changes.

    console
    $ sudo ufw reload
    
  7. Start the Rails application server and allow connections from all network addresses.

    console
    $ rails server --binding=0.0.0.0
    

    You output should be similar to the one below when successful.

    => Booting Puma
    => Rails 8.0.0 application starting in development 
    => Run `bin/rails server --help` for more startup options
    Puma starting in single mode...
    * Puma version: 6.5.0 ("Sky's Version")
    * Ruby version: ruby 3.3.6 (2024-11-05 revision 75015d4c1f) +YJIT [x86_64-linux]
    *  Min threads: 3
    *  Max threads: 3
    *  Environment: development
    *          PID: 23851
    * Listening on http://0.0.0.0:3000
    Use Ctrl-C to stop
  8. Access port 3000 using your server's IP address in a web browser such as Chrome.

    http://SERVER-IP:3000

    Verify that the default Rails page displays in your browser.

    Default Rails Landing page

    Press Ctrl + C in your SSH session to stop the Rails application server.

Update the Ruby on Rails application

Follow the steps below to update the sample demo-app application you created with a new route, view, and controller to display a Greetings from Vultr prompt when accessed.

  1. Run the following command to generate a new Home controller with index as the action.

    console
    $ rails generate controller Home index
    
  2. Back up the default index.html.erb file.

    console
    $ mv app/views/home/index.html.erb app/views/home/index.html.ORIG
    
  3. Create the app/views/home/index.html.erb file again using a text editor such as nano.

    console
    $ nano app/views/home/index.html.erb
    
  4. Add the following contents to the index.html.erb file.

    html
    <h1>Greetings from Vultr</h1>
    

    Save and close the file.

  5. Back up the default config/routes.rb file.

    console
    $ mv config/routes.rb config/routes.ORIG
    
  6. Create the config/routes.rb file again.

    console
    $ nano config/routes.rb
    
  7. Add the following contents to the config/routes.rb file to enable Home#index as the default route.

    ruby
    Rails.application.routes.draw do
        root "home#index"
    end
    

    Save and close the file.

  8. Start the Rails server again to load the application.

    console
    $ rails server --binding=0.0.0.0
    
  9. Access port 3000 using your server's IP address in a new web browser window and verify that a Greetings from Vultr message displays.

    Custom Rails Application with a greeting

Conclusion

You have installed Ruby on Rails on Ubuntu 24.04 and run a sample application on the server. You can run Rails as a system service and set up a reverse proxy such as Nginx to securely deliver web applications using a domain on your server. Ruby on Rails also integrates with other services such as databases to enable additional functionalities such as caching in your web applications. For more information and configuration options, please visit the Ruby on Rails guides page.