Last active
August 21, 2016 21:39
-
-
Save cdl/9a6c91738bf16250f2abfffb2b101ee3 to your computer and use it in GitHub Desktop.
Ruby & PostgreSQL Vagrantfile. Constants at the top are meant to be configured on a per-project basis, with defaults that are explained in comments above each of them.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby + PostgreSQL Vagrantfile | |
# Written by Colby Ludwig, @cdl | |
# Vagrantfile for setting up a base Ruby + PostgreSQL environment on | |
# Ubuntu 14.04 with the latest deps. Sets up the following: | |
# - rbenv for installing Ruby with the default `vagrant` user | |
# - shares the current directory for the project to ~/{dir-name} | |
# - sets up Postgres with a given database name and password (user is `postgres`) | |
# - forwards given ports | |
# | |
# Thanks to http://stackoverflow.com/a/30106828 for helping me get the | |
# base of the file going. | |
# The Ruby version to install with rbenv. Use `rbenv install -l` to list | |
# all versions available to rbenv. | |
$ruby_version = "2.3.1" | |
# Project name - infers it's name from the current directory (that this Vagrantfile | |
# is in), but you can change the below to just a raw string if you'd like. | |
$project_name = File.basename(Dir.getwd) | |
# Locale environment to use. Change the below to match the locale used on your | |
# host machine. (When in doubt, stick with the default.) | |
$locale = "en_US.UTF-8" | |
# The ports to open. Points each port from the guest to the same port on the server. | |
# So, setting $ports to [ 8080 ] will point the VM :8080 -> host :8080. | |
$ports = [ 8080 ] | |
# The database name and password to use for Postgres. User is `postgres`. | |
$db_name = "db_name" | |
$db_password = "db_password" | |
# The provisioning script. Uses the variables above to properly set things up | |
# for a Ruby environment. | |
$provision_script = <<SCRIPT | |
echo "\n\nmaking sure everything's up to date..." | |
sudo sh -c "echo 'LC_ALL=#{$locale}\nLANG=#{$locale}' >> /etc/environment" | |
sudo apt-get update | |
sudo apt-get upgrade -y | |
echo "\n\ninstalling dependencies..." | |
sudo apt-get -y install curl git-core python-software-properties ruby-dev libpq-dev build-essential libsqlite3-0 libsqlite3-dev libxml2 libxml2-dev libxslt1-dev postgresql postgresql-contrib libreadline-dev | |
echo "\n\ninstalling rbenv..." | |
git clone https://github.com/rbenv/rbenv.git ~/.rbenv | |
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc | |
echo 'eval "$(rbenv init -)"' >> ~/.bashrc | |
source ~/.bashrc | |
echo "\n\ninstalling ruby-build..." | |
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build | |
echo "\n\ninstalling ruby..." | |
sudo -H -u vagrant bash -i -c 'rbenv install #{$ruby_version}' | |
sudo -H -u vagrant bash -i -c 'rbenv rehash' | |
echo "\n\nsetting up ruby..." | |
sudo -H -u vagrant bash -i -c 'rbenv global #{$ruby_version}' | |
sudo -H -u vagrant bash -i -c 'gem install bundler --no-ri --no-rdoc' | |
sudo -H -u vagrant bash -i -c 'rbenv rehash' | |
echo "\n\nsetting up postgres..." | |
sudo -u postgres createdb #{$db_name} | |
echo "ALTER USER postgres WITH PASSWORD \'#{$db_password}\';" | sudo -u postgres psql | |
SCRIPT | |
# Start the config! | |
Vagrant.configure(2) do |config| | |
# Use the ubuntu/trusty64 box (for Ubuntu 14.04 LTS). | |
config.vm.box = "ubuntu/trusty64" | |
# Share each port in $ports from the VM to the guest (under the same port). | |
$ports.each do |port| | |
config.vm.network "forwarded_port", guest: port.to_s, host: port.to_s | |
end | |
# Share the project directory to ~/{current-dir-name}. | |
config.vm.synced_folder "./", "/home/vagrant/#{$project_name}" | |
# Provider-specific settings for Virtualbox. Just setting the RAM to 1GB here. | |
config.vm.provider "virtualbox" do |vb| | |
# Customize the amount of memory on the VM: | |
vb.memory = "1024" | |
end | |
# Provision the VM with the script at the top of the Vagrantfile. | |
config.vm.provision :shell, privileged: false, inline: $provision_script | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment