😎 my Vagrantfile

a vagrantfile

  • windows 10 + virtualbox@Hype-V (the docker need Hype-V 😆)
  • ssh-agent
  • React Native + Expo run on Linux (I run them on win now… 😋 )
  • Android Emulator run on windows 10
  • vagrant ssh -- -R 5037:localhost:5037
    • port forward, linux to win at port 5037 🤩
  • ruby, rvm
    • set ruby version in file /vagrant/.ruby-version
    • /vagrant/ in linux mount from windows dir.
  • node, nvm
    • set node version in vagrantfile
    • replace dubnium
  • you should check the path carefully, if you want use it. 😴
# -*- coding: utf-8 -*-
# -*- mode: ruby -*-
# vi: set ft=ruby :

ENV["PORT"] ||= "3000"

$provision = <<SCRIPT

echo ">>>>>"
echo ">>>>> debian 10 buster64"
echo ">>>>> cd /vagrant"
echo ">>>>>"
cd /vagrant
echo ">>>>> pwd"
pwd

echo ">>>>>"
echo ">>>>>"
echo ">>>>> # >>>>> # >>>>> # >>>>> #------->>>------------- Add packages "
echo ">>>>>"
echo ">>>>>"
sudo apt-get update
sudo apt-get install \
  build-essential \
  git-core \
  g++ \
  libpq-dev \
  libxml2-dev \
  libxslt1-dev \
  imagemagick \
  redis-server \
  redis-tools \
  postgresql \
  postgresql-contrib \
  protobuf-compiler \
  libicu-dev \
  libidn11-dev \
  libprotobuf-dev \
  libreadline-dev \
  libpam0g-dev \
  tmux \
  httpie \
  gnupg2 \
  curl \
  -y


echo ">>>>>"
echo ">>>>>"
echo ">>>>> ------->>>------------- # Install nvm"
echo ">>>>>"
echo ">>>>>"

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"

echo ">>>>> This loads nvm bash_completion"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

echo ">>>> verify that nvm has been installed"
command -v nvm

echo ">>>>>"
echo ">>>>>"
echo ">>>>>  ------->>>-------------------->>>------------- Install node 10 Dubnium by nvm"
echo ">>>>>"
echo ">>>>>"
nvm install --lts=dubnium

# echo ">>>>>"
# echo ">>>>>"
# echo ">>>>> ------->>>-------------------->>>------------- Install yarn"
# echo ">>>>>"
# echo ">>>>>"
echo ">>>>>"
# echo ">>>>> # Add the yarn repo + yarn repo keys, not install yarn"
echo ">>>>>"
# curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
# echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
# sudo apt-get update
# sudo apt-get install yarn
npm -g install yarn

echo ">>>>>"
echo ">>>>>"
echo ">>>>> ------->>>-------------------->>>------------- Install rvm"
echo ">>>>>"
echo ">>>>>"

echo ""
echo ""
echo ""
read RUBY_VERSION < .ruby-version
echo ">>>>> ruby version:"
echo $RUBY_VERSION

echo ""
echo ""
echo ""
echo ">>>>> install GPG keys used to verify installation package"
#####gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

echo ""
echo ""
echo ""
echo ">>>>> Installing the stable release version"
curl -sSL https://get.rvm.io | bash -s stable
source /home/vagrant/.rvm/scripts/rvm

echo ""
echo ""
echo ""
echo ">>>>>"
echo ">>>>> Install Ruby by rvm"
echo ">>>>>"
rvm reinstall ruby-$RUBY_VERSION --disable-binary
echo ">>>>> ruby version:"
ruby -v

echo ">>>>>"
echo ">>>>>  ------->>>-------------------->>>------------- Configure database"
echo ">>>>>"

# fix permissions
echo "------->>>------------- fixing listen_addresses on postgresql.conf"
sudo sed -i "s/#listen_address.*/listen_addresses '*'/" /etc/postgresql/11/main/postgresql.conf

echo "------->>>------------- fixing postgres pg_hba.conf file"
echo "# Accept all IPv4 connections - FOR DEVELOPMENT ONLY!!!" | sudo tee -a /etc/postgresql/11/main/pg_hba.conf >/dev/null
echo "host    all             all             all                     md5" | sudo tee -a "/etc/postgresql/11/main/pg_hba.conf" >/dev/null

echo "------->>>------------- creating postgres vagrant role with password vagrant"
# Create Role and login
sudo -u postgres psql -c "CREATE ROLE vagrant SUPERUSER LOGIN PASSWORD 'vagrant';"

echo "------->>>------------- creating vagrant database"
# Create database
sudo su postgres -c "createdb -E UTF8 -T template0 --locale=en_US.utf8 -O vagrant hengha_backend_development"

echo ""
echo ""
echo ""
echo ">>>>>"
echo ">>>>> ------->>>-------------------->>>------------- Install gems and node modules"
echo ">>>>>"
gem install bundler foreman tmuxinator
######bundle install
######yarn install

echo ""
echo ""
echo ""
echo ">>>>>"
echo ">>>>> # config tmuxinator"
echo ">>>>>"
mkdir /home/vagrant/.bin/
wget https://raw.githubusercontent.com/tmuxinator/tmuxinator/master/completion/tmuxinator.bash -P /home/vagrant/.bin
######echo "source ~/.bin/tmuxinator.zsh" >> /home/vagrant/.zshrc
echo "source ~/.bin/tmuxinator.bash" >> /home/vagrant/.bashrc

###### echo ">>>>>"
###### echo ">>>>> Build Mastodon"
######export $(cat ".env.vagrant" | xargs)
######bundle exec rails db:setup

echo ">>>>>"
echo ">>>>>" ###### Configure automatic loading of environment variable
echo ">>>>>"
echo 'export $(cat "/vagrant/.env.vagrant" | xargs)' >> /home/vagrant/.bash_profile

# echo ">>>>> golang"
# bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)

SCRIPT

$start = <<SCRIPT

cd /vagrant

##echo 'To start server'
##echo '  $ vagrant ssh -c "cd /vagrant && foreman start"'

SCRIPT

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

  # config.vm.box = "debian/buster64"
  config.vm.box = "bento/debian-10"


      # Load custom vbguest installer
    # if defined?(VagrantVbguest::Installers::Debian)
    #     require_relative 'utility/vbg-installer'
    #     config.vbguest.installer = Utility::DebianCustom
    # end

    # config.vbguest.auto_update = false

  #config.ssh.insert_key = false

  config.ssh.private_key_path = ["C:\\Users\\....\\.vagrant\\machines\\default\\virtualbox\\private_key",
                                 "C:\\Users\\...\\.ssh\\id_rsa"]
  config.ssh.forward_agent = true

  config.vm.provider :virtualbox do |vb|
    vb.name = "debian-10"
    vb.customize ["modifyvm", :id, "--memory", "6144", "--cpus", "4"]

    # Disable VirtualBox DNS proxy to skip long-delay IPv6 resolutions.
    # https://github.com/mitchellh/vagrant/issues/1172
    vb.customize ["modifyvm", :id, "--natdnsproxy1", "off"]
    vb.customize ["modifyvm", :id, "--natdnshostresolver1", "off"]

    # Use "virtio" network interfaces for better performance.
    vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
    vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
  end

#   $ vagrant plugin install vagrant-hostsupdater
  if defined?(VagrantPlugins::HostsUpdater)
    config.vm.network :private_network, ip: "192.168.42.8", nictype: "virtio"
    config.vm.hostname = "x.dev"
    # config.hostsupdater.remove_on_suspend = false
    # config.hostsupdater.aliases = ["y.dev", "z.dev"]
  end

  config.vm.network :public_network, brideg: "Microsoft Network Adapter Multiplexor Driver", use_dhcp_assigned_default_route: true
    # just 1 line
    # ip: "192.168.1.99",



# Otherwise, you can access the site at http://localhost:3000 and http://localhost:4000 , http://localhost:8080
  config.vm.network :forwarded_port, guest: 3000, host: 3000
  # config.vm.network :forwarded_port, guest: 3999, host: 3999
  config.vm.network :forwarded_port, guest: 4000, host: 4000
  config.vm.network :forwarded_port, guest: 4567, host: 4567
#  config.vm.network :forwarded_port, guest: 5037, host: 5037
  config.vm.network :forwarded_port, guest: 5432, host: 5433

  config.vm.network :forwarded_port, guest: 8000, host: 8000
  config.vm.network :forwarded_port, guest: 8080, host: 8080
  config.vm.network :forwarded_port, guest: 9292, host: 9292

  # config.vm.network :forwarded_port, guest: 19000, host: 19000
  # config.vm.network :forwarded_port, guest: 19001, host: 19001
  # config.vm.network :forwarded_port, guest: 19002, host: 19002
  # config.vm.network :forwarded_port, guest: 50051, host: 50051


  #  config.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh", disabled: true
  # ssh login
  config.vm.network :forwarded_port, guest: 22, host: 2212, auto_correct: true

  # On Windows we must check if the plugin vagrant-winnfsd if installed. This plugin must be installed to be able to use NFS
  if Vagrant::Util::Platform.windows? then
    unless Vagrant.has_plugin?("vagrant-winnfsd")
      raise  Vagrant::Errors::VagrantError.new, "vagrant-winnfsd plugin is missing. Please install it using 'vagrant plugin install vagrant-winnfsd' and rerun 'vagrant up'"
    end
  end

  config.winnfsd.uid = 1
  config.winnfsd.gid = 1

  # install vagrant plugin winnfs
  # use NFS for Windows, OSX and Linux
  config.vm.synced_folder "../data_pool", "/vagrant",
    type: "nfs",
    mount_options: %w{rw,async,fsc,nolock,vers=3,udp, actimeo=2},
    # mount_options: %w{rw,async,fsc,nolock,vers=3,udp,rsize=32768,wsize=32768,hard,noatime,actimeo=2},
    rsync__exclude: [".git/", "tmp/", "public/uploads/", "public/assets/", "log/"]


  # Full provisioning script, only runs on first 'vagrant up' or with 'vagrant provision'
  config.vm.provision :shell, inline: $provision, privileged: false

  # Start up script, runs on every 'vagrant up'
  config.vm.provision :shell, inline: $start, run: 'always', privileged: false
end
Zane Zheng
Zane Zheng
Full Stack (Ruby on Rails) / Devops

An Full Stack / Devops with JS, Ruby, Linux, JAMStack, MicroServices.