Deploying a Rails App on DigitalOcean Droplet

Karim Marabet
Level Up Coding
Published in
3 min readJan 18, 2022

--

Digital Ocean provides an easy-to-use, developer-centric platform, has lower costs, and has more data centers available. DigitalOcean Droplets are simple, scalable virtual machines. Each Droplet you create is a new server you can use, either standalone or as part of a larger, cloud-based infrastructure.

First of all, we create simple Ruby on Rails application and then create a droplet, install necessary packages and gems, set up Postgres and host the application.

Creating Rails App

rails new app -d postgresql
cd app

Configure database.yml file

database.yml

As you can see I use environment variables and use it you need to install the dotenv-rails gem

gem 'dotenv-rails', groups: [:development, :test]
bundle

and create a .env file in the root directory and paste your data

DATABASE_USERNAME=<username>
DATABASE_PASSWORD=<password>
DATABASE_HOST=localhost

For more visibility let’s generate a scaffold and migrate the database

rails g scaffold posts title:string content:text
rails db:setup
rails s

I will place my app in GitHub. For that, I create a new repository, link it with my project, add secret information to .gitignore and push a commit.

git remote add origin <your_repo_link>
git branch -M main
echo ".env" >> .gitignore
git add .
git commit -m "Init commit"
git push -u origin main

Setup DigitalOcean Droplet

I guess you register the account in DigitalOcean and are ready to create a droplet. So, I chose Ubuntu 20.04 image and the most basic plan.

To speed up the creation process I set a password instead of SSH keys, but it is not recommended.

In a few minutes, the droplet will finish the installation and we can open the console.

When the console opens let’s start to set up the environment. Note that you have selected the required Ruby and Rails versions. You can check with ruby -v and rails -v commands in your project.

Installing Ruby using RVM

Enter the following commands in the droplet’s console.

sudo apt update
sudo apt-get install software-properties-common
sudo apt-add-repository -y ppa:rael-gc/rvm
sudo apt-get update
sudo apt-get install rvm

⚠️ Reboot the console.

rvm install <ruby_version>
rvm --default use <ruby_version>

Installing Rails and dependencies

sudo apt install nodejs npm
npm install --global yarn
gem install bundler
gem install rails -v <rails_version>

Installing Postgres

Note that you have selected the same database username as in the .env file

sudo apt install postgresql postgresql-contrib libpq-dev
sudo -u postgres createuser --interactive
# Enter name of role to add: <database_username>
# Shall the new role be a superuser? (y/n)y
sudo -u postgres psql -c "ALTER USER <username> PASSWORD '<database_password>';"# ALTER ROLE

Cloning and launching the project

Clone the project and create a .env file in the root directory

git clone <your_repo_link>
cd <your_repo_name>
nano .env

and paste your data again

DATABASE_USERNAME=<username>
DATABASE_PASSWORD=<password>
DATABASE_HOST=localhost

The final step is creating a database, precompiling assets (optionally), and starting the server with binds to the droplet’s IP.

bundle
rails db:setup
rails assets:precompile
rails s -b 0.0.0.0

Check your ipv4 and paste it into your browser with 3000 port. In my case: 147.182.136.41:3000

Hooray! 🎉 It was not quite easy, but I hope that you extracted something useful.

--

--