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:
- 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
- Import GPG keys for RVM.
- Download and install RVM.
- Source the RVM scripts.
- Check RVM version.
- Enable automatic dependency installation.
- Add the root user to the RVM group.
- List known Ruby versions.
- Install Ruby 2.5.1 and set it as default.
- Verify the Ruby installation.
- 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
- 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
- Update RubyGems.
- Check RubyGems version.
- Install Rails.
- Check Rails version.
- Install PostgreSQL and development libraries.
- 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
- Start the interactive Ruby shell (IRB).
- Check IP addresses.
- Create a new Rails application.
- Install Node.js.
- Uninstall and reinstall Yarn.
- Install Webpacker.
- Check Yarn version.
- Install Brew and Yarn using NPM.
- Start the Rails server.
- Check SQLite3 version.
- Add NodeSource repository.
- Install Git.
- 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
- Install Heroku CLI:
sudo snap install --classic heroku
curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
- Install Heroku CLI using Snap.
- Alternatively, install Heroku CLI using a shell script.
- Login and Create Heroku App:
heroku login -i
heroku create
- Login to Heroku.
- 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
- Add Bootstrap, jQuery, and Popper.js:
yarn add bootstrap@4.4.1 jquery popper.js
- Edit
app/assets/stylesheets/application.css
Add the following line: - code
*= require bootstrap
- 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
- Edit
app/javascript/packs/application.js
- Add the following line at the bottom:
import "bootstrap"