Deployment of Ruby on Rails app using Capistrano Locally(From one user to another on the same PC)

Sabita Nepal
4 min readMay 14, 2024

--

deployment locally the rails app using capistrano (img src:itnext)

Introduction

In software development, deploying applications is a crucial step towards making them accessible to users. Capistrano is a popular tool used for automating the deployment process, making it efficient and reliable.

In this guide, we’ll walk through the steps of deploying a Ruby on Rails application locally using Capistrano which I did while learning about the deployment of the Rails app and these are the steps which I took.

Prerequisites

Before we begin, ensure you have the following prerequisites:

  • Basic knowledge of Ubuntu terminal and Capistrano file structure.
  • Ruby on Rails is properly set up on your PC.
  • Both users have the necessary permissions to access the Rails app’s directory and execute commands.
  • SSH key properly set up.
  • Familiarity with setting up users and SSH keys on Ubuntu.

User Creation and SSH Setup

  • Create a new user:
        sudo adduser newuser
  • After creating the user, switch to that user
 sudo su - newuser

. Now we have to create a .ssh directory in the new user as we need the ssh key in order to deploy the Rails app.

 mkdir -p ~/.ssh
  • Now we have to Copy the SSH authorized keys from the existing user to the new user’s .ssh directory as ssh is being used here for communication between two users.
sudo cp /home/existing-user/.ssh/authorized_keys /home/newuser/.ssh
  • Also, give the ownership to the new user
sudo chown -R newuser:newuser /home/newuser/.ssh
  • Grant sudo privilege to new user(you have to be logged in as a user who has sudo privileges)
sudo usermod -aG sudo newuser
  • Now we are able to switch to a new user
su - newuser
  • We can now check whether SSH is configured in the new user or not
ssh localhost
  • If ssh is connected then this will be shown now :
ssh localhost
  • Now let's start the deployment process. Open up the user from which you want to deploy the Rails app in a new user:

Gemfile Setup

  • The first step will be to set up the Capistrano gems in rails.

Add the following to your Gemfile under the development group

gem "capistrano", "~> 3.10", require: false
gem "capistrano-rails", "~> 1.6", require: false
gem 'capistrano-rbenv', require: false
gem 'capistrano-puma', require: false
  • Now run the following command from the command line to install the additional bundle
bundle install
  • Run the generator to create a basic set of configuration files
bundle exec cap install

Capfile Configuration

  • Uncomment the following plugins in Capfile located at the root of the project

require "capistrano/rbenv"
require "capistrano/bundler"
require "capistrano/rails/assets"
require "capistrano/rails/migrations"
require "capistrano/rails"
require "capistrano/bundler"
require "capistrano/puma"
  • Note: The above plugins vary according to the requirements of your app. For example, if you're using passenger here you will need to add require "capistrano/passenger". too. Since I'm not using it I do not have to add it.

Configuration Files

  1. Update config/deploy.rb with appropriate values., here we are using cap loc version 3.18.0 and deploying using staging.rb
  2. Ensure to customize configurations according to your project requirements
lock "~> 3.18.0" set :stage, :staging 
set :rails_env, 'test'
set :application, '{your app name}'
set :repo_url, 'git@github.com:your/github/url.git'
set :deploy_to, '/home/{your local username}/{your deployed application name}'
set :branch, '{the branch which you want to deploy}'
set :rbenv_ruby, '2.7.7'
set :default_env, { 'PATH' => "#{fetch(:rbenv_path)}/shims:#{fetch(:rbenv_path)}/bin:$PATH", 'RBENV_VERSION' => fetch(:rbenv_ruby), }

Deployment

  • Add a localhost ip in your staging.rb file
server 'localhost', user: '{your local username}', roles: %w{app db web}
  • Now you can deploy the API locally using the below command
cap staging deploy
  • Now your ruby on rails API is deployed locally and you can check the version by navigating to your app folder. You can run the below command after navigating to the current directory to run the rails server.
bin/rails s
  • If there occurs a certain gem installation error remember to make sure there is no conflict in any version between ruby and rails.

Now your file structure looks like this :

deployed app file structure

Conclusion

Congratulations! You have successfully deployed your Ruby on Rails application locally using Capistrano. You can now run your Rails server and access your application

Tips

  • Always verify your deployment configuration before executing the deployment command.
  • Troubleshoot any errors encountered during deployment by checking logs and configurations.
  • Customize Capistrano configurations according to your project’s needs.

--

--