DevOps Programming

Setting Up a Ruby on Rails Project on Vagrant

Configuring the Vagrant Machine

First, we need to configure the Vagrant machine. Below is the configuration file that defines the virtual machine:

Vagrant.configure("2") do |config|

config.vm.define "master" do |master|
master.vm.hostname = "master"
master.vm.box = "bento/ubuntu-18.04"
master.vm.network "private_network", ip: "192.168.33.100"

master.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = 2
end

end
end

This script sets up a Vagrant machine named “master” with the following specifications:

  • Hostname: master
  • Box: bento/ubuntu-18.04
  • Private network IP: 192.168.33.100
  • Memory: 2048 MB
  • CPUs: 2

Once the Vagrant machine is up and running, you can access your Rails application at http://192.168.33.100:3000/.

Setting Up the Vagrant Machine

To set up the Vagrant machine, follow these steps:

  1. Update and Upgrade Packages:
    • sudo apt-get update -y
    • sudo apt-get upgrade -y
    • These commands ensure that your package list and installed packages are up-to-date.
    • Install RVM (Ruby Version Manager) and Ruby:
    • gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
    • curl -sSL https://get.rvm.io | bash -s stable --ruby source /home/vagrant/.rvm/scripts/rvm
    • rvm version
    • rvm get stable --autolibs=enable
    • usermod -a -G rvm root
    • rvm list known
    • rvm install ruby-2.5.1
    • rvm --default use ruby-2.5.1
    • ruby -v

  1. Import GPG keys for RVM.
  2. Download and install RVM.
  3. Source the RVM scripts.
  4. Check RVM version.
  5. Enable automatic dependency installation.
  6. Add the root user to the RVM group.
  7. List known Ruby versions.
  8. Install Ruby 2.5.1 and set it as default.
  9. Verify the Ruby installation.

  1. Install Node.js and Required Packages:
    • curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
    • sudo apt-get install git -y
    • sudo apt-get install gcc g++ make -y
  2. Install Rails and PostgreSQL:
    • gem update --system
    • gem -v
    • gem update --system
    • echo "gem: --no-document" >> ~/.gemrc
    • gem install rails
    • rails -v
    • sudo apt install postgresql postgresql-contrib libpq-dev -y

  1. Update RubyGems.
  2. Check RubyGems version.
  3. Install Rails.
  4. Check Rails version.
  5. Install PostgreSQL and development libraries.

  1. Additional Setup Commands:
    • irb
    • ip addr
    • rails new blog
    • sudo apt-get install nodejs -y
    • gem uninstall yarn
    • npm install yarn
    • rails webpacker:install
    • yarn --version
    • npm install brew -g
    • brew install yarn
    • rails s -b 192.168.33.100 -p 3000
    • sqlite3 --version

  1. Start the interactive Ruby shell (IRB).
  2. Check IP addresses.
  3. Create a new Rails application.
  4. Install Node.js.
  5. Uninstall and reinstall Yarn.
  6. Install Webpacker.
  7. Check Yarn version.
  8. Install Brew and Yarn using NPM.
  9. Start the Rails server.
  10. Check SQLite3 version.
  11. Add NodeSource repository.
  12. Install Git.
  13. Install GCC, G++, and Make.

Git Initialization and Remote Setup

git init
git remote add origin https://github.com/xxx/sample-ruby1.git
git remote -v
git add .
commit -m "new app"
git branch
git pull
git pull origin master --allow-unrelated-histories
git status
git push
git push --set-upstream origin master
ssh-keygen
cat ~/.ssh/id_rsa.pub
git remote -v
git remote add ssh git@github.com:xxx/sample-ruby1.git
git remote -v
  • Initialize Git repository.
  • Add a remote repository.
  • Verify remote repository.
  • Stage all changes.
  • Commit changes with a message.
  • Create and check branches.
  • Pull changes from the remote repository.
  • Push changes to the remote repository.
  • Generate SSH keys.
  • Display the SSH public key.
  • Add SSH remote repository.

Deploying to Heroku

  1. Install Heroku CLI:
    • sudo snap install --classic heroku
    • curl https://cli-assets.heroku.com/install-ubuntu.sh | sh

  1. Install Heroku CLI using Snap.
  2. Alternatively, install Heroku CLI using a shell script.
  3. Login and Create Heroku App:
    • heroku login -i
    • heroku create

  1. Login to Heroku.
  2. Create a new Heroku application.

Updating the Gemfile for Heroku

Add the following to your Gemfile:

group :production do
gem 'pg'
end

Move the sqlite3 gem to the development and test groups:

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'sqlite3', '~> 1.4'
end

Then, run:

bundle install --without production
git add .
git commit -m "heroku deployment"
git push heroku master
heroku open
heroku logs
heroku logs --tail
heroku run rails console
heroku run rake db:migrate
heroku rename sample-ruby1-blog
  • Install dependencies without the production group.
  • Stage and commit changes.
  • Push to Heroku.
  • Open the Heroku app.
  • Check logs.
  • Tail logs.
  • Run Rails console on Heroku.
  • Run database migrations on Heroku.
  • Rename the Heroku app.

Running the Rails Server on the Vagrant Machine

rails s -b 192.168.33.100 -p 3000
  • Start the Rails server bound to IP 192.168.33.100 on port 3000.

Setting Up Bootstrap

  1. Add Bootstrap, jQuery, and Popper.js:
  2. yarn add bootstrap@4.4.1 jquery popper.js
  3. Edit app/assets/stylesheets/application.cssAdd the following line:
  4. code*= require bootstrap
  5. Edit config/webpack/environment.js

const { environment } = require('@rails/webpacker') const webpack = require("webpack") environment.plugins.append("Provide", new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery', Popper: ['popper.js', 'default'] })) module.exports = environment

  1. Edit app/javascript/packs/application.js
  2. Add the following line at the bottom:
    • import "bootstrap"

Ali Imran
Over the past 20+ years, I have been working as a software engineer, architect, and programmer, creating, designing, and programming various applications. My main focus has always been to achieve business goals and transform business ideas into digital reality. I have successfully solved numerous business problems and increased productivity for small businesses as well as enterprise corporations through the solutions that I created. My strong technical background and ability to work effectively in team environments make me a valuable asset to any organization.
https://ITsAli.com

Leave a Reply