Using the Advanced Vagrant Features in PhpStorm
This tutorial describes how to use the advanced features of Vagrant integration in PhpStorm. The details of using Vagrant and Oracle VirtualBox are out of scope of this tutorial. Refer to the respective documentation for more information.
Pay attention to prerequisites mentioned in the Vagrant: Working with Reproducible Development Environments page. Also make sure that a Vagrant box is created and initialized.
Using the built-in SSH terminal to connect to a Vagrant machine
PhpStorm features a built-in SSH terminal which can be used to connect to a remote machine.
Starting connection
To connect to a Vagrant machine, choose
on the main menu. This opens a list of hosts we can connect to.The configured Vagrant machine is added to this list automatically. Clicking it opens connection to the SSH endpoint exposed by this Vagrant machine. The menu item allows entering connection information manually. However, we'll confine here to using the virtual box.
Working with SSH
So, after choosing Vagrant, PhpStorm connects to the Vagrant machine using SSH server and shows a terminal to work with:
![Vargant int he terminal Vargant int he terminal](https://resources.jetbrains.com/help/img/idea/2022.1/py_vagrant_ssh_terminal.png)
In the SSH terminal you can do the following:
Scroll through the history of commands using up and down arrow keys
Perform clipboard operations
Working with shared folders
Vagrant allows sharing folders between the host machine and the Vagrant machine. You can use these folders, for example, to automatically map web root contents from the current PhpStorm project to the Apache virtual host directory on the Vagrant machine.
Adding a path mapping
Open Vagrantfile for editing (F4, and add a configuration entry for path mapping:
Reloading Vagrant
Reload Vagrantfile by choosing on the main menu:
![Reload vagrant Reload vagrant](https://resources.jetbrains.com/help/img/idea/2022.1/vagrant_reload.png)
The results of command execution show up in the Run tool window:
![Vagrant reloading results Vagrant reloading results](https://resources.jetbrains.com/help/img/idea/2022.1/vagrant_reload_result.png)
Once the Vagrant machine is reloaded, a new path mapping becomes available. For example, when connecting to the Vagrant machine using the built-in SSH terminal, we see the contents of the /vagrant folder that maps to the PhpStorm local project folder. Be careful: deleting files from this folder will delete files on both ends!
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 desirable, the instance folder where PhpStorm should look for Vagrantfile can be configured through the Vagrant page of the settings/preferences:
![Specifying the Vagrant instance folder Specifying the Vagrant instance folder](https://resources.jetbrains.com/help/img/idea/2022.1/vagrant_instance_folder.png)
Managing Vagrant plugins through settings
In the Vagrant page of the settings/preferences, you can also manage Vagrant plugins. In the tab Plugins, use the toolbar buttons to install, uninstall and update plugins.
![Adding a Vagrant plugin Adding a Vagrant plugin](https://resources.jetbrains.com/help/img/idea/2022.1/vagrant_pl.png)
You can also install licenses, for example, for the VMWare Fusion Provider which allows running Vagrant machines on VMWare.
Providers support
Vagrant works with Oracle VirtualBox as the virtualization platform by default. However, you can change the virtualization platform using providers, so the virtual machines can be run by a system other than VirtualBox, for example, VMWare or Amazon EC2. Find available providers at the Vagrant plugins list.
For each command, you should pass the provider to be used to Vagrant. To simplify this process and have PhpStorm automatically add the provider name to every Vagrant command, specify the provider in the Vagrant page of the settings/preferences. All providers installed on your machine, are available from the settings. Once selected, a provider will be used to execute all Vagrant commands in PhpStorm:
![Redefining the vagrant provider Redefining the vagrant provider](https://resources.jetbrains.com/help/img/idea/2022.1/vagrant_provider.png)
(Re-)provisioning a Vagrant machine
A Vagrantfile can contain a series of provisioners which can launch installation and configuration routines once a virtual machine is running.
The
main menu command invokes the configured provisioners on an already running Vagrant machine, without having to first destroy the virtual machine.Refer to the provisioning documentation for details.
Working with Environment variables
Environment variables in Vagrantfile are can be used for Puppet nodes, Puppet environments, custom facts, AWS keys.
In the Vagrant page of the Settings/Preferences dialog, specify project-specific environment variables to be passed to Vagrantfile:
![Adding an environment variable Adding an environment variable](https://resources.jetbrains.com/help/img/idea/2022.1/vagrant_env_var.png)
Once set, these environment variables are referred to in a Vagrantfile, using the syntax #{ENV['name_of_variable']}
:
![Newly added environmental variable in the Vagrantfile Newly added environmental variable in the Vagrantfile](https://resources.jetbrains.com/help/img/idea/2022.1/vagrant_env_var_in_vagrantfile.png)