Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

This tutorial describes how to get started with Vagrant integration in PhpStorm.


Vagrant is a tool which helps us create reproducible development environments. By scripting a virtual machine, Vagrant allows us to share a development machine by sharing a simple script. Other developers can then launch an identical machine by running the vagrant up command. PhpStorm comes with support for Vagrant within the IDE.

1. Ensure Vagrant and Oracle VirtualBox are on your system

To be able to work with Vagrant in PhpStorm, both Vagrant and Oracle VirtualBox have to be installed on your system.

1.1. Download and install Vagrant

Download and install Vagrant from their official website.

1.2. Download and install Oracle VirtualBox

From the VirtualBox website, download and install the latest version of VirtualBox.

2. Configure Vagrant project settings

From the Settings | Project Settings | Vagrant window, specify the required parameters for Vagrant integration. The path to the Vagrant executable must be provided, as well as the instance folder (can be empty to use the project directory).

Using the Add button, we can add our first Vagrant Box. PhpStorm will provide the name and URL to the lucid32 box by default; other boxes can be specified as well.

3. Initialize VagrantFile

The VagrantFile contains all information about the virtual machine. It contains details about the virtual machine such as the virtual IP address, port mappings and memory to assign. Next to that, it can specify which folders are shared and which third-party software should be installed on the machine.

Icon

Note: the PuPHPet website has a great tool for configuring a Vagrant Box and generating a VagrantFile.

The VagrantFile can be created manually or by using the Tools | Vagrant | Init in Project Root menu.

This will trigger creation of a default VagrantFile.

4. Vagrant up

The Vagrant Box can be started from the Tools | Vagrant | Up menu. This will configure the virtual machine in VirtualBox if this hasn't been done yet, as well as boot it.

Progress can be seen from the Run tool window.

Once completed, the Vagrant Box will be ready for use.

5. (optional) Advanced features

The following features can also be used whenworking with Vagrant in PhpStorm.

5.1. Using the built-in SSH terminal to connect to the Vagrant Box

PhpStorm features a built-in SSH terminal which can be used to connect to a remote machine.

5.1.1. Start a connection

From the Tools | Start SSH session... menu, we can connect to the Vagrant Box.

This will open a list of hosts we can connect to.

Our Vagrant box should automatically be added to this list. Clicking it will open a connection to the SSH endpoint exposed by our Vagrant box.
The Edit credentials... item allows us to provide connection information manually.

5.1.2. Provide connection information

Next, we have to provide connection information. For the default lucid32 box, we can use the following values. Note that for other Vargrant Boxes this information may be different.

  • Host: localhost
  • Port: 2222 (which is forwarded to the Vagrant Box)
  • User name: vagrant
  • Password: vagrant

5.1.3. Working with SSH

After we click OK, PhpStorm will connect to the Vagrant Box using SSH server and show us a terminal to work with.

In the SSH terminal, we can run commands remotely as well as copy/paste data back and forth.

5.2. Working with shared folders

Vagrant allows sharing folders between the host machine and the Vagrant Box. They can be used, for example, to automatically provide web root contents from the current PhpStorm project to the Apache virtual host directory on the Vagrant Box.

5.2.1. Add a path mapping

From the VagrantFile, we can add path mappings by adding a configuration entry for it:

5.2.2. Vagrant reload

Reloading the VagrantFile can be done using the Tools | Vagrant | Reload menu. Once the Vagrant Box has been reloaded, a new path mapping will be available.

For example, when connecting to the Vagrant Box using the built-in SSH terminal, we can see the contents of the /vagrant folder which map to the PhpStorm local project folder. Be careful: deleting files from this folder will delete files on both ends!

5.3. Specifying Vagrant instance folder

By default, the Vagrantfile and all other Vagrant specifics (like Puppet manifests) are placed in the root of a PhpStorm project. Since this is not always desired, the instance folder where the IDE should look for Vagrantfile can be configured through Project Settings | Vagrant.

5.4. Manage Vagrant plugins through settings

Through the Project Settings | Vagrant, Vagrant plugins can be managed. Use the toolbar buttons to install, uninstall and update plugins. Licenses can also be installed, for example for the VMWare Fusion Provider which allows running Vagrant boxes on VMWare.

5.5. Providers support

Vagrant works with Oracle VirtualBox as the virtualization platform by default. Using providers, the virtualization platform can be changed and so virtual machines can be run by a system other than VirtualBox, such as VMWare or Amazon EC2. A list of available providers can be found on the Vagrant plugins list.

The provider to be used has to be passed to Vagrant for every command. To make this easier and have PhpStorm automatically add the provider name to every Vagrant command, we can specify the provider through Project Settings | Vagrant. All providers installed on our machine will be available from the settings. Once selected, PhpStorm will execute all Vagrant commands using the provider configured.

5.6. (Re-)provisioning a Vagrant box

A Vagrantfile (the Vagrant configuration file) can contain a series of provisioners that can launch installation and configuration routines once a virtual machine is running. The Provision command invokes the configured provisioners on an already running Vagrant box, without having to first destroy the virtual machine.

Using the Tools | Vagrant menu, we can run provisioning on a running environment.

5.7. Working with Environment variables

Working with environment variables in Vagrantfile is very useful for doing several things, like:

  • setting the Puppet node
  • setting the Puppet environment
  • setting custom facts
  • setting AWS keys
  • ...

From the Project Settings | Vagrant we can specify project-specific environment variables that will be passed into the Vagrantfile.

Once set, we can make use of them in our Vagrantfile using the #{ENV['name_of_variable']} syntax:

  • No labels