Prerequisites
- For setting up Compute Engine, see the Setting up Compute Engine Instance.
- Domain name is pointed to your virtual machine.
- For setting up Cloud DNS, see the Setting up Google Cloud DNS for your domain.
- 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

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.

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