How to Ruby on Rails 7 on Ubuntu 24.04

This article provides a guide for installing Ruby on Rails 7 on Ubuntu 24.04. It covers the installation of Ruby, Rails, and MariaDB, as well as steps to create a new application and configure its database. By following these instructions, users can set up a functional Rails development environment.

This article explains how to install Ruby on Rails version 7 on Ubuntu 24.04.

Ruby on Rails, commonly known as Rails, is a web application framework developed using the Ruby programming language. It streamlines the process of creating web applications by offering a structured approach to organizing code, handling database interactions, and managing the routing of web requests.

To install Ruby on Rails on Ubuntu, you must first install and set up Ruby, often using a version manager like RVM or rbenv.

After setting up Ruby, you can install Rails using the Gem package manager.

Install Ruby

To install Ruby on Rails, you first need to install Ruby. In this tutorial, we will be installing 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 show the version installed.

ruby -v

Install Rails

Now that Ruby is installed, let’s proceed with installing Rails. For this tutorial, we will be using Rails version 7.

Run the command below to install necessary packages to 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 show the version installed.

rails -v

Configure Rails app

After installing Ruby and Rails, set up your environment to begin building your app.

In most cases, a database will be needed.

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 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 browse 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:

In summary, installing Ruby on Rails on Ubuntu 24.04 involves several key 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!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version