Installing Ruby on Rails on Debian 12 Using Mise
Rails is a free and open-source framework for web applications, developed using the Ruby language. It is based on the Model-View-Controller (MVC) pattern and is designed for creating efficient and dynamic websites. Rails emphasizes the “Convention over Configuration” (CoC) principle and supports features such as routing, data handling, and asset management.
This guide walks you through the process of installing Ruby on Rails on a Debian 12 system. You’ll set up Ruby along with all essential dependencies required to run a basic Rails project.
Prerequisites
- You have access to a Debian 12 machine where Ruby on Rails will be installed.
- You have configured a domain A record that points to your server’s IP address, for example:
app.example.com
.
Installing Mise and Ruby Dependencies
Mise is a tool designed to manage Ruby versions and their related dependencies. The following steps explain how to install Mise and set up all necessary components for Ruby.
Step 1: Update the APT Package Index
Start by refreshing your system’s package index:
$ sudo apt update
Step 2: Install Required System Libraries
Install the libraries and tools that Ruby relies on:
$ sudo apt install build-essential zlib1g-dev libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev libffi-dev git -y
Step 3: Download and Install Mise
Use the following command to download and install Mise:
$ curl https://mise.run | sh
This installs Mise into the .local/bin/mise
directory within your user’s home folder.
Step 4: Enable Mise in Your Bash Session
Append the following command to your ~/.bashrc
file to activate Mise:
$ echo "eval \"\$(/home/user/.local/bin/mise activate bash)\"" >> ~/.bashrc
Apply the changes in your current session by reloading the shell configuration:
$ source ~/.bashrc
Step 5: Confirm Mise Installation
Check if Mise was installed correctly and see its version:
$ mise -v
Example output:
____ ___ (_)_______ ___ ____ ____ / /___ _________ / __ `__ \/ / ___/ _ \______/ _ \/ __ \______/ __ \/ / __ `/ ___/ _ \ / / / / / / (__ ) __/_____/ __/ / / /_____/ /_/ / / /_/ / /__/ __/ /_/ /_/ /_/_/____/\___/ \___/_/ /_/ / .___/_/\__,_/\___/\___/ /_/ 2024.12.24 linux-x64 (b04359f 2024-12-31)
Step 6: Run Mise Diagnostic
Finally, use the following command to test the installation and ensure everything is configured correctly:
$ mise doctor
This command outputs information about the Mise environment, including shell, build, and other configuration details. Look for a confirmation line that says:
No problems found
How to Install Ruby and Rails on Debian 12
RubyGems, also known as gem
, is the default package manager for Ruby. It helps install necessary libraries and modules on your server. When Ruby is installed via Mise, RubyGems comes bundled by default, enabling you to install and manage frameworks like Rails. The following steps guide you through installing Ruby using Mise, setting up RubyGems, and configuring Rails on your Debian 12 machine.
Step 1: Install Ruby with Mise
Use the following command to install the latest version of Ruby 3:
$ mise use -g ruby@3
This command installs the most recent Ruby 3 release. To confirm which version will be installed, you can reference the official Ruby release page.
Expected output:
mise ruby@3.3.6 ✓ installed
mise ~/.config/mise/config.toml
tools: ruby@3.3.6
Step 2: Check Ruby Version
Verify Ruby was successfully installed by running:
$ ruby -v
Sample output:
ruby 3.3.6 (2024-11-05 revision 75015d4c1f) [x86_64-linux]
Step 3: Install Bundler
Next, install the Bundler gem, which manages project dependencies in Ruby applications:
$ gem install bundler
Expected output:
Fetching bundler-2.6.2.gem
Successfully installed bundler-2.6.2
Parsing documentation for bundler-2.6.2
Installing ri documentation for bundler-2.6.2
Done installing documentation for bundler after 0 seconds
1 gem installed
A new release of RubyGems is available: 3.5.22 → 3.6.2!
Run `gem update --system 3.6.2` to update your installation.
Step 4: Update RubyGems
Upgrade RubyGems to the most recent version:
$ gem update --system
Step 5: Install Rails
Now, install the Rails framework using RubyGems:
$ gem install rails
Sample output from a successful installation:
Done installing documentation for zeitwerk, thor, rack, rackup, concurrent-ruby, tzinfo, i18n, connection_pool, activesupport, useragent, nokogiri, crass, loofah, rails-html-sanitizer, rails-dom-testing, rack-test, rack-session, erubi, builder, actionview, actionpack, railties, marcel, activemodel, activerecord, globalid, activejob, activestorage, actiontext, mini_mime, mail, actionmailer, actionmailbox, websocket-extensions, websocket-driver, nio4r, actioncable, rails after 37 seconds
38 gems installed
Step 6: Confirm Rails Installation
Use the command below to check which Rails version is installed:
$ rails -v
Sample output:
Rails 8.0.1
Step 7: Install Node.js for Rails Applications
Install Node.js version 22 with Mise to serve JavaScript in your Rails apps:
$ mise use -g node@22
You can verify the latest Node.js versions from the official releases page. Example output:
gpg: Signature made Tue 03 Dec 2024 08:37:27 PM UTC
gpg: using RSA key 108F52B48DB57BB0CC439B2997B01419BD92F80A
gpg: Good signature from "Ruy Adorno <ruyadorno@hotmail.com>" [unknown]
mise node@22.12.0 ✓ installed
mise ~/.config/mise/config.toml
tools: node@22.12.0
Step 8: Verify Node.js Version
Run this command to view the Node.js version:
$ node -v
Expected output:
v22.12.0
Step 9: Install Yarn with Npm
Install Yarn globally using Npm:
$ npm install -g yarn
Example output when Yarn installation completes:
added 1 package in 907ms
npm notice
npm notice New major version of npm available! 10.9.0 -> 11.0.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v11.0.0
npm notice To update run: npm install -g npm@11.0.0
npm notice
Reshimming mise 22.12.0...
Step 10: Check Yarn Version
Confirm Yarn was installed by running:
$ yarn -v
Expected result:
1.22.22
If you’re using an Ubuntu system, you can also install Ruby on Rails on Ubuntu 24.04 using similar steps.
Bootstrap and Run a Ruby on Rails Application on Debian 12
Rails offers a command-line interface that allows you to generate and manage applications efficiently. The following instructions guide you through the process of initializing and launching a Rails application.
Step 1: Generate a New Rails Project
To start a new project named myrailsapp
, execute the following command:
$ rails new myrailsapp
This command creates a new directory named myrailsapp
with all essential files and folders. The structure looks like this:
.
├── app
├── bin
├── config
├── config.ru
├── db
├── Dockerfile
├── Gemfile
├── Gemfile.lock
├── lib
├── log
├── public
├── Rakefile
├── README.md
├── script
├── storage
├── test
├── tmp
└── vendor
13 directories, 6 files
Step 2: Overview of Application Structure
- config.ru: Rack configuration file that acts as the entry point for the Rails app.
- Dockerfile: Instructions to containerize the app using Docker.
- Gemfile: Lists all required Ruby gems.
- Gemfile.lock: Locks the gem versions to ensure consistency.
- Rakefile: Defines tasks that can be executed with Rake.
- README.md: Contains instructions for running the app.
- app: Holds the application’s core code—controllers, views, models, etc.
- bin: Contains Rails and other executable scripts.
- config: Stores configuration settings, routes, and environment files.
- db: Holds database schema and migrations.
- lib: Custom Ruby modules and libraries.
- log: Application logs for development and production.
- public: Static assets such as images, stylesheets, and JavaScript.
- script: Additional custom scripts for the application.
- storage: Stores persistent files and uploads.
- test: Testing files organized by type (model, controller, etc.).
- tmp: Caches, PID files, and temporary data.
- vendor: External dependencies and libraries.
Step 3: Navigate to the Application Directory
Change into the project directory to begin working inside the app:
$ cd myrailsapp
Step 4: Open Rails Port in the Firewall
Allow incoming traffic on port 3000, which is the default for Rails development servers:
$ sudo ufw allow 3000/tcp
Step 5: Reload UFW Configuration
Apply the new firewall rule by reloading UFW:
$ sudo ufw reload
Step 6: Start the Rails Development Server
Launch the Rails server and bind it to all network interfaces (0.0.0.0):
$ rails server --binding=0.0.0.0
Sample output when the server starts successfully:
=> Booting Puma
=> Rails 8.0.0.1 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) [x86_64-linux]
* Min threads: 3
* Max threads: 3
* Environment: development
* PID: 30417
* Listening on http://0.0.0.0:3000
Use Ctrl-C to stop
Step 7: Access the Application
In your web browser (such as Chrome), go to the following URL to load the default Rails page:
http://Server-IP:3000
Confirm that the Rails welcome screen displays correctly.
Step 8: Stop the Rails Server
To shut down the running Rails server, press Ctrl + C
in the terminal.
Modify a Rails Application to Set a Custom Root Route
By default, Rails displays its standard welcome page through the root route. This tutorial demonstrates how to modify your myrailsapp
project by creating a new controller, setting up a view, and configuring a custom root path that displays a Greetings message.
Step 1: Generate a New Controller with an Action
Use the Rails CLI to generate a Home
controller with an index
action:
$ rails generate controller Home index
Sample output:
create app/controllers/home_controller.rb
route get "home/index"
invoke erb
create app/views/home
create app/views/home/index.html.erb
invoke test_unit
create test/controllers/home_controller_test.rb
invoke helper
create app/helpers/home_helper.rb
invoke test_unit
Step 2: Backup the Default View File
Preserve the default view file before making changes by renaming it:
$ mv app/views/home/index.html.erb app/views/home/index.html.erb.ORIG
This file is responsible for rendering HTML output with embedded Ruby code when users navigate to the index
action of the Home
controller.
Step 3: Create a Custom View File
Use a text editor like nano
to create a new version of the view file:
$ nano app/views/home/index.html.erb
Add the following content:
<h1>Greetings</h1>
Save the file and exit the text editor.
Step 4: Configure the Root Route
Edit the routes.rb
file to set the root route of your application to the new view:
$ nano config/routes.rb
Inside the Rails.application.routes.draw do
block, insert the following line:
root "home#index"
Save your changes and close the editor.
Step 5: Restart the Rails Server
Restart the Rails server and bind it to all interfaces to make the app accessible:
$ rails server --binding=0.0.0.0
Step 6: Access Your Modified Rails Application
Open a browser and go to the following address, replacing Server-IP
with your actual server IP:
http://Server-IP:3000
You should see your new homepage with the message:
Greetings
Conclusion
You’ve now successfully installed Ruby on Rails on Debian 12, created a sample application, and customized its default route and homepage. To enhance your setup further, consider configuring Rails to run as a system service and using Nginx as a reverse proxy to handle traffic securely. For more in-depth guidance, visit the official Ruby on Rails documentation.