How to setup a LAMP development environment in Vagrant

In the previous tutorial, we have learned about the basics of Vagrant. In this tutorial, we will move to next level and will learn more about Vagrant and setting up LAMP development environment in Vagrant.

To start with, LAMP stands for: Linux (OS/Kernel), Apache (Web Server), MySQL (Database), PHP (Scripting Language). It is an open-source Web development environment which lets you create web applications. It is generally also referred as Web Stack.

Let us now setup LAMP development environment in Vagrant with below steps:

#1- Create a directory where we would be creating the instance:

mkdir -p ~/Vagrant/lamp
cd  ~/Vagrant/lamp

#2- Now we are required to initialize the Vagrant box. Here we are left with two options.

#2.1- One, we can re-use Ubuntu 12.04 LTS (Precise Pangolin) box, which we have already downloaded in the previous tutorial. To use this, just do:

vagrant init precise32

#2.2- Second option we have is to do a fresh addition of the box by downloading the Ubuntu 12.04 LTS (Precise Pangolin) Vagrant box, as:

vagrant box add precise32

And then do:

vagrant init precise32

Please note- #2.2 is only for those who have not downloaded the Ubuntu 12.04 LTS (Precise Pangolin) Vagrant box previously.

#3- Now do:

vagrant up

This will create a VagrantFile. Edit the VagrantFile as below:

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

# Mentioning the SSH Username/Password:
config.ssh.username = "vagrant"
config.ssh.password = "vagrant"
# Begin Configuring
config.vm.define "lamp" do|lamp|

lamp.vm.hostname = "lamp" # Setting up hostname "private_network", ip: "" # Setting up machine's IP Address
lamp.vm.provision :shell, path: "" # Provisioning with

# End Configuring

Please note- I have deleted the commented lines to avoid confusion.

You can download this file from here.

#4- Let us now start provisioning the LAMP installation. For that, create a simple shell script named using your favorite text editor as:


# Updating repository

sudo apt-get -y update

# Installing Apache

sudo apt-get -y install apache2

# Installing MySQL and it's dependencies, Also, setting up root password for MySQL as it will prompt to enter the password during installation
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password password rootpass'
sudo debconf-set-selections <<< 'mysql-server-5.5 mysql-server/root_password_again password rootpass'
sudo apt-get -y install mysql-server libapache2-mod-auth-mysql php5-mysql
# Installing PHP and it's dependencies sudo apt-get -y install php5 libapache2-mod-php5 php5-mcrypt

You can download this file from here.

#5- After saving the, run:

vagrant up

It will do lot of things. You would be able to see as what it is doing on the stdout. To give you an overview, it will start with importing the precise32 base box, then it will SSH into the box, then it will set the hostname of the machine(we have specified this in the Vagrantfile), then it start updating and installing the LAMP stack.

Please note- While installing mysql-server, it will set it's root password to 'rootpass' since we have mentioned this in the file.

#6- Now after the vagrant is done with installation, you are ready to go. SSH into the vagrant box as:

vagrant ssh

#7- Verify the installations by:

dpkg -l | grep "apache2\|mysql-server-5.5\|php5"

You'll see all these packages listed and this means they have been installed successfully. With this you're done with setting up the LAMP development environment in Vagrant :)

Tip- You can now save this box to it's current state(Ubuntu 12.04 LTS with LAMP) as:

Step 1- Come out of Vagrant box by issuing:


Step 2- Package this box with:

vagrant package --output

This will create a file which you can re-use as a base box with LAMP already installed, by default.

To keep things organized, create a directory where you gonna store all such boxes and move this into it:

mkdir -p ~/Vagrant/boxes
mv ~/Vagrant/boxes

You can anytime import and initialize this box by:

vagrant init ubuntu1204_LAMP file:~/Vagrant/boxes/

Author: Sanchit Jain Rasiya

Share this page:

Suggested articles

6 Comment(s)

Add comment


By: Sumant Dey

Very helpful tutorial..specially I liked the TIP at later part of tutorial. Thanks!!

By: Ryan

Good tutorial


I wonder how can I map a domain.local on my /etc/hosts/ to the vagrantbox. Something like:www.domain.local --> that way I can get www.domain.test/ and behaves the same way as

for example.

By: Flo

You can just update the hosts file of your system and adding something like :

By: Arjay

Great .. Thank you! I wonder how will I insert php file in the folder? Thank you for answering..

By: Tumi

Thank you this was very helpful!!