Compute Engine Google Cloud

Install Ruby on Rails on Ubuntu 18.04 with Google Cloud SQL

Prerequisites

  1. For setting up Compute Engine, see the Setting up Compute Engine Instance.
  2. Domain name is pointed to your virtual machine.
  3. For setting up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
  4. Google Cloud SQL Setup, see Setup Cloud SQL and connect with Compute Engine.

Install Ruby on Rails with RVM

SSH to your Compute Engine Instance and execute the following commands to install RVM. Replace username with your username

\curl -sSL https://get.rvm.io | bash
source /home/username/.rvm/scripts/rvm

Install Dependencies

rvm requirements

Wait for the dependencies installation to complete

Install Ruby

Once RVM and dependencies are installed successfully you can install Ruby with RVM.

rvm install ruby
rvm --default use ruby
ruby -v

Bundler is a tool that manages gem dependencies for all projects. Install the Bundler gem next as Rails depends on it.

gem install bundler

Install Nodejs, ImageMagick, MySQL Client, and some required packages

sudo apt-get install -y nodejs imagemagick libcurl4-openssl-dev mysql-client libmysqlclient-dev

Install Rails

gem install rails
rails -v

Now install MySQL adapter for Rails

gem install mysql2

Create a new Rails project

rails new myproject -d mysql
cd myproject

Edit the database configuration to connect to Cloud SQL

sudo nano config/database.yml

Replace the username with your username, password with your password, host with your Cloud SQL IP address

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: username
  password: password
  host: Cloud_SQL_IP_Address

development: 
  <<: *default 
  database: database_name

Now Ruby on Rails is installed and configured to connect to your Cloud SQL on Google Cloud

Enable Firewall on Google Cloud

When you run rails application it runs on port 3000, so you need to create a firewall rule for that port.

In your Google Cloud Console go to VPC Networks >> Firewall rules

Click Create Firewall rule

Enter Name

Set Targets to All instances in the network

In Source IP ranges enter 0.0.0.0/0

In Protocols and ports check tcp and enter 3000

Click Create

Firewall rule

Once your firewall rule is created you can start your rails server by executing this command

rails s

Visit your domain name on your browser with port 3000 (yourdomainname.com:3000)

Now you can view the Ruby on Rails welcome page.

Rails on Google Cloud

Enjoy your installation of Ruby on Rails on Google Cloud with Cloud SQL

Cloudbooklet builds a large collection of Linux based guides and tutorials on Cloud platforms like Google Cloud, AWS, Azure, DigitalOcean and more

Write A Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

If you find this helpful? Support me!
Buy me a coffee Donation Please buy me a coffee