command line interface (CLI) for creating and managing virtual machine instances. Vagrant can be used with several VM "providers" which provide the VM infrastructure. VirtualBox is the default provider, but more can be added via the plugin system (e.g. VMWare, AWS, Rackspace Cloud). It gives us the vital ability to create and configure virtual machines in a consistent manner. It is perfect for sharing server environments amongst the team, all packaged neatly in a few text files.
Vagrant (with VirtualBox) to create and configure individual development environments for each project. Having project-specific VMs allows us to install only those dependencies (and specific versions of those dependencies) required for a project. These virtual environments mirror the live, production environments for each project as closely as possible. We run our applications and tests locally within these virtual environments during development.
( ) Setup Vagrant for a new project: Edit the resulting V a g r a n t F i l e (more on this later ...) Load up the virtual machine (headless): SSH into the machine as needed: (A keypair is automatically created for the v a g r a n t user and made available to all VMs created through Vagrant) virtualbox.org vagrantup.com $ c d / p a t h / t o / m y / a p p $ v a g r a n t i n i t $ v a g r a n t u p $ s s h v a g r a n t @ 1 9 2 . 6 8 . 5 0 . 1 0 0
L E The V a g r a n t F i l e is a simple Ruby script that tells Vagrant how to configure our virtual machine(s). You can configure several virtual machines within the same V a g r a n t F i l e . This is useful for applications that make use of a cluster of different servers. We bundle this file with the project's Mercurial repostory so that others in the team can easily spin up an identical virtual environment on their local machine.
A N T F I L E The flavour of OS to install (find bare bones boxes at or , or package your own) The IP address of the VM A shared folder between your host machine and the virtual machine A provisioning script or manifest files vagrantcloud.com vagrantbox.es
L E V A G R A N T F I L E _ A P I _ V E R S I O N = " 2 " V a g r a n t . c o n f i g u r e ( V A G R A N T F I L E _ A P I _ V E R S I O N ) d o | c | # T h e b a s e " b a r e b o n e s " O S i m a g e c . v m . b o x = " u b u n t u / t r u s t y 3 2 " c . v m . b o x _ u r l = " h t t p s : / / v a g r a n t c l o u d . c o m / u b u n t u / . . . / v i r t u a l b o x . b o x " # A h o s t n a m e f o r t h i s m a c h i n e c . v m . h o s t n a m e = " m y - v a g r a n t - b o x " # A l o c a l I P ( u s e p r i v a t e a d d r e s s s p a c e , 1 9 2 . 1 6 8 . * . * ) # T h e h o s t m a c h i n e w i l l b e o n 1 9 2 . 1 6 8 . 5 0 . 1 c . v m . n e t w o r k " p r i v a t e _ n e t w o r k " , i p : " 1 9 2 . 1 6 8 . 5 0 . 1 0 0 " # S e t u p s h a r e d f o l d e r ( n e e d e d f o r O S X ) c . v m . s y n c e d _ f o l d e r " . / " , " / v a g r a n t " , i d : " v a g r a n t - r o o t " , o w n e r : " v a g r a n t " , g r o u p : " w w w - d a t a " , m o u n t _ o p t i o n s : [ " d m o d e = 7 7 5 " , " f m o d e = 6 6 4 " ] # A p r o v i s i o n i n g s h e l l s c r i p t ( r u n o n c e ; m o r e o n t h i s l a t e r ) c . v m . p r o v i s i o n : s h e l l , : p a t h = > " p r o v i s i o n . s h " e n d
one-time setup of the virtual machine. This may include the installation of dependant software packages, creation of required project directories, configration of service daemons, etc. Vagrant supports a few ways to provision machines, such as Ansible, Puppet and Chef (all cross platform) or a simple a shell script. Importantly, provisioning is non-interactive - you cannot (and should not) need to interact with the process. Here's an example of the p r o v i s i o n . s h shell script that we earlier defined in our example V a g r a n t F i l e ...
a secure machine from scratch: # I n s t a l l s o m e p a c k a g e s ( i n c l u d e s s o m e m y s q l - s p e c i f i c h a c k e r y ) # N o t e t h a t t h e s c r i p t r u n s a s r o o t b y d e f a u l t a p t - g e t u p d a t e d e b c o n f - s e t - s e l e c t i o n s < < < ' m y s q l - s e r v e r - < v e r s i o n > m y s q l - s e r v e r / r o o t _ p a s s w o r d p a s s w o r d r o o t ' d e b c o n f - s e t - s e l e c t i o n s < < < ' m y s q l - s e r v e r - < v e r s i o n > m y s q l - s e r v e r / r o o t _ p a s s w o r d _ a g a i n p a s s w o r d r o o t ' a p t - g e t i n s t a l l n g i n x m y s q l n o d e j s c p - f / v a g r a n t / p r o v i s i o n i n g / l o c a l / m y . c n f / e t c / m y s q l / m y . c n f s e r v i c e m y s q l r e s t a r t # C r e a t e s o m e l o g g i n g d i r e c t o r i e s m k d i r / v a r / l o g s / m y l o g s / e r r o r . l o g c h o w n r o o t : w w w - d a t a / v a r / l o g s / m y l o g s / e r r o r . l o g c h m o d 0 6 4 4 / v a r / l o g s / m y l o g s / e r r o r . l o g # C o p y m y u p s t a r t s c r i p t f r o m s h a r e d f o l d e r t o O S d i r e c t o r y c p - f / v a g r a n t / m y - d a e m o n - u p s t a r t . c o n f / e t c / i n i t . d / s t a r t m y - d a e m o n - u p s t a r t # S e t u p D B e c h o " c r e a t e d a t a b a s e m y d b " | m y s q l - u r o o t - p r o o t m y s q l - u r o o t - p r o o t - e " g r a n t a l l p r i v i l e g e s o n * . * t o r o o t @ ' % ' i d e n t i f i e d b y ' r o o t ' " ; m y s q l - u r o o t - p r o o t m y d b < / v a g r a n t / d b - s c h e m a . s q l # S y m l i n k w e b s i t e d o c r o o t t o o u r s h a r e d f o l d e r l n - s / v a r / w w w / v a g r a n t / p u b l i c _ h t m l http://plusbryan.com/my-first-5-minutes-on-a-server-or-essential-security-for-linux-servers
a few minutes the first time whilst the provisioning script is executed, but thereafter it'll be snappier) And we can load our app into a browser: $ c d / p a t h / t o / m y / a p p $ v a g r a n t u p h t t p : / / 1 9 2 . 1 6 8 . 5 0 . 1 0 0 /
i n i t Creates a new V a g r a n t F i l e with a few basic settings v a g r a n t u p < a l i a s > Spins up the <alias> virtual machine or all machines and triggers provisioning if needed. v a g r a n t r e l o a d - - p r o v i s i o n Reboots (and re-provisions) the VM. v a g r a n t h a l t < a l i a s > Shuts down the <alias> virtual machine or all machines. (Use this rather than s u d o h a l t from within the VM)
n t s s h < a l i a s > Shells into the virtual machine (or p u t t y - s s h v a g r a n t @ i p - a d d r e s s on Windows) v a g r a n t d e s t r o y < a l i a s > Destroys the <alias> virtual machine instance or all machines v a g r a n t h e l p List all other commands
project-specific VMs helps us accurately mirror the live environment, minimizing WTFs when it comes to deployment. We bundle the V a g r a n t F i l e , and any accompanying files required for provisioning, with the project repository, so others in the team simply need to run: Running tests within the VMs ensures tests are carried out in an identical environment. Again minimizing WTF moments. $ h g p u l l - u ; v a g r a n t u p