Ruby on Rails is a rapid development framework. It’s used by over a million websites,
including Airbnb, Bloomberg, GitHub, GitLab, CouchSurfing, and many more.
The Ruby language is easy to understand as it’s close to English, making it great for
beginners. In this tutorial, we’ll show you how to get started with a new Ruby on Rails
project and how to bring your existing Ruby on Rails application to Oracle Cloud
Set up environment automatically
Don’t have time to read the whole article? Use the “Deploy Button” to provision the Ruby on
Rails environment with a few clicks. In this environment, you can build a new application or
bring your existing Ruby on Rails application to OCI.
The button takes you to Oracle Resource Manager which runs Terraform code that will provision
the full Ruby on Rails environment.
The following resources will be created:
Virtual Cloud Network
VM - Ubuntu
1 OCPU, 8 Gb Memory 1 OCPU. can be scaled up to 1024
GB Memory and 128 OCPUs
MySQL Database Service
can be scaled up to run in HA with up to 3 nodes
can optionally be replaced with bastion service
Load balancer 10 mpbs
flex loadbalancer upto 4000 mbps
The public subnet will contain the following resources:
The private subnet will contain the following resources:
Ruby on Rails VM(s)
Managed MySQL Database
Software packages installed on the VMs:
RBENV Package manager for ruby to make it easy to manage different ruby versions and gem
The bootstrap script in the GitHub repo shows how the VMs are configured and all the packages
that are installed:
You can check out
script on GitHub.
If you have deployed the environment using the deploy button, you can skip the next section.
The bootstrap script has been run automatically when the VMs were provisioned.
How to setup environment automatically
Click the “Deploy to Oracle Cloud"
If you aren’t already signed in, when prompted, enter the tenancy and user credentials.
Review and accept the terms and conditions.
Select the region where you want to deploy the stack.
Follow the on-screen prompts and instructions to create the stack.
After creating the stack, click
Terraform Actions, and select
Wait for the job to be completed, and review the plan. To make any changes, return to
the Stack Details page, click
Edit Stack, and make the required
changes. Then, run the Plan action again. If no further changes are necessary, return to the Stack Details page, click
Terraform Actions, and select Apply.
How to setup environment manually
If you prefer to setup the environment manually you need to provision the resources and then
install the Ruby on Rails environment
Provisioning the VM and MySQL database is not covered in this guide.
Once you have a VM and MySQL database ready you can proceed to the next step to install Ruby
VM running Ubuntu 20.04
How to install Ruby on Rails on
Ubuntu 20.04 with rbenv
SSH to your VM and run the following commands. Make sure to run the command with a user that
has sudo rights. On Ubuntu the
Ubuntu user is usually fine:
Install ruby manager, mysql-client and other dependencies:
sudo apt-get install -y build-essential git libsqlite3-dev libssl-dev libzlcore-dev mysql-client libmysqlclient-dev git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs npm
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install -y yarn
Install the ruby package manager to help install and manage different ruby versions:
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin: $PATH "' >> ~/.bash_profile
echo 'eval "$ (rbenv init - bash)" ' >> ~/.bash_profile
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin: $PATH "' >> ~/.bash_profile
Now we are ready to install ruby. If you need a different version, change the version number:
Check that the correct version of ruby is installed:
Use version 3.0.1 and make it default:
rbenv global 3.0.1
rbenv local 3.0.1
Reload the environment variables to ensure the correct ruby binaries are run:
Let’s install Rails framework and bundler gem:
gem install rails
gem install bundler
Congratulations, you have now installed Ruby on Rails on your VM!
Create your Ruby on Rails application
In this section we will create a rails application using the built-in scaffolding methods of
rails. We will connect the rails app to the database provisioned.
Let’s create a directory where our app will reside and make the
Ubuntu user the owner of the
sudo mkdir /opt/apps
sudo chown ubuntu:ubuntu /opt/apps
Create a new app that is preconfigured with a mysql adapter:
rails new myapp -d mysql
Open up port 8080 for incoming HTTP requests:
sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 8080 -j ACCEPT
Edit the database config file and add your MySQL username and MySQL database name and MySQL
Create a database schema. If you have already created a database schema you can skip this:
Run all migration scripts to create the tables in the DB. Every time
you makes change to table definitions:
Start the rails server as a background process, listen on port 8080, bind all interfaces, and
send log output to startup.log:
rails s -p 8080 -b 0.0.0.0 > > ./log/startup.log &
Accessing the application
If you configured your VM manually you should now be able to access your new rails
If you provisioned your environment automatically using the “Deploy Button” you can access
the application through the load balancer, e.g.
Check Oracle Resource Manager output for the IP or check the load balancer page.
The load balancer is configured to listen on port 80 and forwards all traffic to the VM where
the Ruby on Rails application is running.
The load balancer will forward traffic to all the VMs if you provisioned more than one VM.
A new Rails App is created in the directory
A database schema is created on the MySQL server.
Database schema name:
The app is configured to connect to the database. Configuration can be found in
How to deploy your existing application
Before deleting the app we will take a backup of the database.yml file. We’ll need it
cp /opt/apps/myapp/config/database.yml /opt/apps/bck_database.yml
Delete the existing vanilla Ruby on Rails app created in
Copy your application to the server using scp
scp myapp.zip ubuntu@server-ip:/opt/apps/
Unpack the application
Copy the generated database config file
cp /opt/apps/bck_database.yml ./myapp/config/database.yml
Or edit the existing database config file (
Migrate your data to the MySQL Database
Run Rails migrations
Start the server
rails s -b 0.0.0.0 -p 8080
How to change Ruby version
The default ruby version installed is 3.0.1. If you need a different ruby version simply run:
You can see available Ruby version by running: