In a previous post we configured the Vagrant File in preparation of using the vagrant box to host a rails application. In this post I will walk through how to configure he vagrant box and include your rails application to it.
After the vagrant file has been created you can simply run vagrant up to start it. To ssh into it simply use the command vagrant ssh and you will see that you are now in your vagrant box. We are now in a fully functioning yet isolated operating system.
Configuring The Vagrant Box
Since I plan to run a rails application in this environment I need to add everything needed to run rails: git, rvm, ruby, rails, nodejs, and Postgresql. I took my instruction for setting up git, rvm, ruby, and rails from this article.
Install git:
sudo apt-get update
sudo apt-get install git
Install curl:
sudo apt-get install curl
\curl -sSL https://get.rvm.io | bash
Load rvm:
source /home/vagrant/.rvm/scripts/rvm
rvm requirements
Install whatever version of Ruby you choose:
rvm install 2.1
Set your Ruby default version:
rvm use 2.1 --default
Verify your Ruby version:
ruby -v
Install rails:
gem install rails
Check your rails version:
rails -v
Install bundler:
gem install bundler
Bundle your gems:
bundle install
Install node js:
sudo apt-get install nodejs
Install Your Database(s) - I used Postgresql
$ sudo apt-get install postgresql libpq-dev
$ sudo su - postgres
$ psql
postgres=# CREATE USER vagrant;
postgres=# CREATE DATABASE your_database_name;
postgres=# GRANT ALL PRIVILEGES ON DATABASE "your_database_name" to vagrant;
postgres=# \q
$ exit
The database(s) that you created above should have the same name as the database(s) used in your rails application. Database names are set in the database.yml file.
For example, my application's database.yml file has the following setup:
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
adapter: postgresql
username: vagrant
database: main_rest_development
test:
adapter: postgresql
username: vagrant
database: main_rest_test
production:
<<: *default
database: main_rest_production
Please keep in mind that the alignment of your text in this file must be perfect or it won't be read correctly!
Once all of this is done your vagrant environment is configured and ready to run your application.
Creating An Application From Scratch
If you will be creating an application from scratch you will need to do that on your local machine.
Performed on you local machine - Not Vagrant
You should open a new tab in your terminal to the same directory where you created your Vagrant file.
To create a new application simply run rails new your-app-name --database=postgresql.
This will create a new application in this directory and the database should be configured with the names and permissions you assigned above.
Performed in Vagrant
To run this application you must go into the other terminal tab where the vagrant box is running. Move (cd) into the '/vagrant_file' directory (or whichever directory you designated as the synced folder in the Vagrant file). Move (cd) into the new app's directory. Run 'bundle install'. Then start your rails server using rails s. Your application should now be running on the port or url you designated.
Using An Existing Application
This must also be performed on the local machine.
Performed on you local machine - Not Vagrant
You should open a new tab in your terminal to the same directory where you created your Vagrant file. If you have an existing application that you want to use you can simply clone the code from github to your local machine:
git clone your_existing_app
You will need to update the database.yml file to have the correct database, and database names.
Performed in Vagrant
To run this application you must go into the other terminal tab where the vagrant box is running.
Move (cd) into the '/vagrant_file' directory (or whichever directory you designated as the synced folder in the Vagrant file).
Move (cd) into the new app's directory.
Run 'bundle install'.
Then start your rails server using rails s. Your application should now be running on the port or url you designated.
When you you are finished running your application you can stop it. Move into the Vagrant's root file and type exit. This will put you back into you local machine.
On your local machine type vagrant destroy and your box is now stopped.
It is quite alot of work to configure the Vagrant box to run your application. However, once configured this box can be used for any other rails application that needs the same setting in the future. It can also be copied and moved to other systems if necessary. Vagrant provides total isolation which will give your apps much needed stability.
Top comments (1)
Excelent! Thanks, but what about the password for the database for enviroment production, Heroku for example