Working with Environment Variables
1. Using VAGRANT_HOME
| Environment Variable | Value | Description |
|---|---|---|
| VAGRANT_HOME | $HOME/.vagrant.d (default) |
Directory where Vagrant stores global state and plugins |
Note: Changing
VAGRANT_HOME affects all Vagrant environments on the system.
2. Setting VAGRANT_DEFAULT_PROVIDER
| Environment Variable | Value | Description |
|---|---|---|
| VAGRANT_DEFAULT_PROVIDER | virtualbox |
Default provider to use when no provider is specified in Vagrantfile |
Note: This environment variable is read by Vagrant at runtime and affects the default provider used for all commands in the current session.
3. Configuring VAGRANT_CWD
The VAGRANT_CWD environment variable allows you to specify the directory from which Vagrant should operate, regardless of your current working directory. This is particularly useful when running Vagrant commands from a different location than where your Vagrantfile resides.
| Environment Variable | Value | Description |
|---|---|---|
| VAGRANT_CWD | /path/to/vagrantfile/directory |
Specifies the directory containing the Vagrantfile to use for operations. |
Note: Setting
VAGRANT_CWD does not change the current working directory for other processes or shell sessions. It only affects Vagrant's behavior.
4. Using VAGRANT_DOTFILE_PATH
The VAGRANT_DOTFILE_PATH environment variable allows you to customize the location of Vagrant's internal state files (like .vagrant directory) for a specific Vagrant environment.
| Environment Variable | Value | Description |
|---|---|---|
| VAGRANT_DOTFILE_PATH | string |
Path to the directory where Vagrant stores its state files (e.g., .vagrant). |
Example: Setting VAGRANT_DOTFILE_PATH
VAGRANT_DOTFILE_PATH=/home/user/custom-vagrant-state vagrant up
Note: Setting
VAGRANT_DOTFILE_PATH can be useful for organizing Vagrant environments or avoiding conflicts in shared development environments.
5. Setting VAGRANT_LOG
| Environment Variable | Values | Description |
|---|---|---|
| VAGRANT_LOG | debug, info, warn, error |
Sets the log level for Vagrant's output. Defaults to info. |
Note: Setting
VAGRANT_LOG=debug will output detailed logs useful for troubleshooting.
6. Configuring VAGRANT_NO_PARALLEL
| Environment Variable | Value | Description |
|---|---|---|
| VAGRANT_NO_PARALLEL | 1 |
Disables parallel execution of Vagrant commands. When set to 1, Vagrant will run operations sequentially instead of in parallel. |
Note: Setting
VAGRANT_NO_PARALLEL to 1 ensures that Vagrant commands like vagrant up, vagrant provision, and vagrant destroy execute one at a time, which can help avoid resource contention in environments with limited concurrency support.
7. Using VAGRANT_VAGRANTFILE
| Environment Variable | Values | Description |
|---|---|---|
| VAGRANT_VAGRANTFILE | Vagrantfile |
Specifies the path to the Vagrantfile to use. Defaults to Vagrantfile in the current directory. |
Note: The
VAGRANT_VAGRANTFILE environment variable allows you to specify a custom Vagrantfile path, useful for managing multiple configurations or testing different setups without renaming files.
8. Setting VAGRANT_CHECKPOINT_DISABLE
| Environment Variable | Value | Description |
|---|---|---|
| VAGRANT_CHECKPOINT_DISABLE | 1 |
Disables Vagrant's checkpoint feature which checks for updates and sends usage statistics to HashiCorp. |
Note: Setting this variable prevents Vagrant from checking for updates or sending telemetry data. It's useful in air-gapped environments or when you want to suppress update notifications.
9. Configuring Proxy Variables
| Variable | Description | Default Value | Notes |
|---|---|---|---|
| http_proxy | HTTP proxy URL for outbound requests | None | Used by Vagrant to route HTTP traffic through a proxy server |
| https_proxy | HTTPS proxy URL for outbound requests | None | Used by Vagrant to route HTTPS traffic through a proxy server |
| no_proxy | Comma-separated list of hosts to bypass proxy | None | Allows specific hosts or domains to be excluded from proxy routing |
Example: Setting Proxy Variables in Vagrantfile
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
# Set proxy settings
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
# Configure environment variables for the guest VM
config.vm.provision "shell", inline: <<-SHELL
export http_proxy=http://proxy.company.com:8080
export https_proxy=http://proxy.company.com:8080
export no_proxy=localhost,127.0.0.1,10.0.0.0/8
SHELL
end
Note: Proxy settings can also be configured via system environment variables or by setting them in the Vagrant environment using
vagrant env command.
Warning: Ensure that the proxy server supports the protocols used by Vagrant (HTTP/HTTPS) and that it allows connections to required external services like package repositories.
10. Using Custom Environment Variables
| Variable | Value | Description |
|---|---|---|
| VAGRANT_ENV_VAR_NAME | custom_value |
Custom environment variable set in Vagrantfile |
Example: Setting Custom Environment Variable in Vagrantfile
Vagrant.configure("2") do |config|
config.vm.provision :shell, inline: <<-SHELL
echo "Custom variable is set to: $VAGRANT_ENV_VAR_NAME"
SHELL
end
Note: Environment variables defined in Vagrantfile are available during provisioning and can be accessed inside shell scripts.