How to Ruby on Rails 7 on Ubuntu 24.04
You install Ruby on Rails 7 on Ubuntu 24.04 by setting up your Ruby environment and then adding the Rails gem.
Ruby on Rails (often called Rails) is a widely-used, open-source web application framework written in Ruby. It simplifies web development by providing standard solutions for common tasks like database management and routing.
First, you’ll use a Ruby version manager, such as rbenv, to install a specific Ruby version. For Rails 7, Ruby 3.2.1 is recommended.
After ensuring your Ruby is set up, you install the Rails framework itself via RubyGems, the standard package manager for Ruby libraries.
Install Ruby using `apt install ruby3.2`, then install Rails with `gem install rails -N –version=’~> 7.0, < 8.0'`. Finally, configure your database and create a new Rails application using `rails new myapp -d mysql`.
Install Ruby
To install Ruby on Rails, you first need to install Ruby. In this tutorial, you’ll install Ruby version 3.
Run the command below to install Ruby.
sudo apt update
sudo apt install ruby3.2
After installing Ruby, run the command below to see the version you’ve installed.
ruby -v
Install Rails
Now that Ruby is installed, let’s proceed with installing Rails. For this tutorial, you’ll be using Rails version 7.
Run the command below to install necessary packages that support Rails.
sudo apt install ruby-dev libmysqlclient-dev gcc make yarnpkg libxml2 libxml2-dev libxslt-dev libyaml-dev nodejs git
After installing the packages above, use gem to install Rails.
sudo gem install bundler
sudo gem install nokogiri -- --use-system-libraries
sudo gem install rails -N --version='~> 7.0, < 8.0'
After Rails is installed, run the command below to see the version you’ve installed.
rails -v
Configure Rails app
After installing Ruby and Rails, set up your environment to begin building your app.
You’ll likely need a database to store your application’s data.
Run the command below to install MariaDB.
sudo apt install mariadb-server
Once installed, create a blank database.
First, log on to the database server.
sudo mariadb
Then, create a blank database called [myapp].
CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
Create a user called [myuser] with password.
CREATE USER myuser@localhost IDENTIFIED BY 'type_your_password_here';
Then, grant the user full access to the database.
GRANT ALL ON myapp.* TO myuser@localhost WITH GRANT OPTION;
Save changes and exit.
FLUSH PRIVILEGES;
exit
Once you have set up your database, proceed to the next steps to configure it for your application.
Run the command below to connect your database. Then, create a simple application called [simpleapp].
sudo gem install mysql2 -- --with-mysql-config=/usr/bin/mysql_config
rails new simpleapp -d mysql
Change into the simpleapp folder and create a database configuration file.
cd simpleapp
nano config/database.yml
Copy the content below into the file. Enter the database user name and password you created above.
#
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: myuser
password: type_password_here
socket: /var/run/mysqld/mysqld.sock
development:
<<: *default
database: myapp
Save and exit.
Next, open the application file.
nano config/application.rb
Copy the content below into the file. The highlighted line allows you to specify a domain name. If you do not specify a domain name, the app can only be accessed using localhost or an IP address.
require "rails/all"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Simpleapp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails versio>
config.load_defaults 7.2
# add domain name if you want user accessing via domain otherwise, only localhost or IP address be used.
config.hosts << ".example.com"
#
.......
end
end
Finally, run the command below to create your application.
rails generate scaffold testapp name:string title:string body:text
rails db:migrate
Start the app by running the command below.
rails server --binding=0.0.0.0
Once the app runs, open your browser and navigate to the server IP address or hostname followed by port 3000.
http://srv1.example.com:3000
Test your app.
http://srv1.example.com:3000/testapps/
That should do it!
Conclusion:
So, to recap, installing Ruby on Rails 7 on Ubuntu 24.04 involves these main steps:
- Installation of Ruby: Ensure you have Ruby version 3.2 installed as it’s essential for Rails.
- Rails Installation: Use the Gem package manager to install Rails version 7.
- Database Setup: Install MariaDB, create a database, and set up a user for your Rails application.
- Application Configuration: Create a new Rails application, configure the database connections, and specify additional settings like domain name if needed.
- Run Your Application: Start the Rails server and access your application via a web browser.
By following these steps, you will have a fully functional Rails environment ready for development!
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!