DEV Community

Cover image for Setup Mojolicious using Vagrant
Ashutosh
Ashutosh

Posted on

Setup Mojolicious using Vagrant

In this article, we cover how to set up the Mojolicious development environment using the Vagrant.

We also cover:

  • Installation of VirtualBox
  • Installation of Vagrant
  • Create Vagrantfile
  • Execute vagrant

Vagrant Introduction

A Vagrant is a tool focused on giving a uniform development environment across different operating systems. It manages virtual machine environments and lowers the development environment setup time. It also helps in getting rid of the "works on my machine" phrase commonly used by software developers.

Installation

Before installing Vagrant, we need to install VirtualBox on our operating systems.

If you are using MacOS,

brew update && brew upgrade
brew install virtualbox # Virtualbox installation
brew install vagrant    # Vagrant Installation
Enter fullscreen mode Exit fullscreen mode

On Debian/Ubuntu OS,

sudo apt update
sudo apt install virtualbox
sudo apt install vagrant
Enter fullscreen mode Exit fullscreen mode

On RHEL/CentOS

yum install virtualbox
yum install vagrant
Enter fullscreen mode Exit fullscreen mode

To verify that the installation, run the following command that will print the Vagrant version:

vagrant --version
Enter fullscreen mode Exit fullscreen mode
#Output
Vagrant 2.2.14
Enter fullscreen mode Exit fullscreen mode

Getting Started

Setting up the vagrant is fairly easy. We need to create a new directory to setup the root of the application.

mkdir vagrant_mojo
cd vagrant_mojo
Enter fullscreen mode Exit fullscreen mode

Setup the Vagrantfile

vagrant init
Enter fullscreen mode Exit fullscreen mode

Above command creates Vagrantfile in the current diretory i.e. vagrant_mojo

#Output
A Vagrantfile has been placed in this directory. You are now
ready to vagrant up your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
vagrantup.com for more information on using Vagrant.
Enter fullscreen mode Exit fullscreen mode

Clear all the content of the 'Vagrantfile'

Vagrant.configure("2") do |config|
    config.vm.box = "generic/ubuntu2004"
    config.disksize.size = '25GB'
    config.vm.network "forwarded_port", guest: 80, host: 8080
    config.vm.network "forwarded_port", guest: 443, host: 8443
    # Can access 3000 port from parent OS
    config.vm.network "forwarded_port", guest: 3000, host: 3000

    # mysql
  config.vm.network "forwarded_port", guest: 3306, host: 30306

  # If you are using postgres uncheck this
  # config.vm.network "forwarded_port", guest: 5432, host: 54320

  # For SSH
  config.ssh.forward_agent = true

  # Current folder is synced with the Vagrant VM /vagrant_data directory
  config.vm.synced_folder "./", "/vagrant_data"

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
    vb.memory = "5192"
  end

  # IMPORTANT: Vagrant run this shell script setup.sh during the configuration.
  config.vm.provision:shell, path: "setup.sh"
end

Enter fullscreen mode Exit fullscreen mode

Create a new file setup.sh and update the following content

#!/bin/bash

install_mojolicious() {
  echo "Installing Mojolicious"
  sudo curl -L https://cpanmin.us | perl - -M https://cpan.metacpan.org -n Mojolicious
}

install_mysql() {
  echo "Installing MariaDB on vagrant machine"
  sudo apt update
  sudo apt install mariadb-server -y
}

install_perl_module() {
    echo "Installing the Perl Module"
  sudo cpan install DateTime -y
  sudo cpan install DBIx::Class -y
  sudo cpan install Crypt::PBKDF2 -y
  sudo cpan install Mouse -y
  sudo cpan install Email::Valid -y
  sudo cpan install Mojolicious::Plugin::Authentication -y
  sudo cpan install Mojolicious::Plugin::StaticCache -y
}

setup_mojolicious() {
# Setting up the Mojolicious APP
    cd /vagrant_data
    mojo generate app vagrant_mojo
}

main() {
  install_mojolicious
  install_mysql
  install_perl_module
  setup_mojolicious
}
main
Enter fullscreen mode Exit fullscreen mode

Save and exit the file. And run the following command.

vagrant up
Enter fullscreen mode Exit fullscreen mode

This will take a little time to finish it up. Once the installation is done. Mojo app is already created under the "/vagrant_data".

vagrant ssh
cd /vagrant_data/vagrant_mojo
morbo script/vagrant_mojo
Enter fullscreen mode Exit fullscreen mode

It will start the webserver at http://127.0.0.1:3000. Go to you browser of the operating system and type http://localhost:3000.

Now, you can do any changes from your main OS under the synced directory. It will change the app by default in ubuntu OS (VirtualBox). It also preserves the environment from regular OS updates.

Top comments (0)