Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

vagrant-exec Gem Version

Vagrant plugin to execute commands within the context of VM synced directory.


You will probably use the plugin if you don't want to SSH into the box to execute commands simply because your machine environment is already configured (e.g. I use ZSH and TextMate bundles to run specs/features).


➜ vagrant plugin install vagrant-exec


➜ vagrant exec pwd


vagrant-exec has only one configuration option for Vagrantfile, which allows you to alter the behavior of all or specific commands.

Vagrant.configure('2') do |config| = 'precise32'
  config.exec.commands '*', directory: '/tmp'

Commands can either be:

  • "*" (wildcard) - apply options to all the commands
  • "command" (string) - apply options for specific commands
  • %w(command1 command2) (array) - apply options to all commands in array

Configuration options are merged, so if you specify single command in several places, all the option will be applied. The only exception is :directory, which is applied only once and in reverse order (i.e. the last set is used).


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

  # Make /tmp working directory for all the commands:
  #   ➜ vagrant exec pwd
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /tmp && pwd"
  config.exec.commands '*', directory: '/tmp'

  # Make /etc working directory for env command:
  #   ➜ vagrant exec env
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /etc && env"
  config.exec.commands 'env', directory: '/etc'


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

  # Automatically prepend apt-get command with sudo:
  #   ➜ vagrant exec apt-get install htop
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /vagrant && sudo apt-get install htop"
  config.exec.commands 'apt-get', prepend: 'sudo'

  # Automatically prepend rails and rspec commands with bundle exec:
  #   ➜ vagrant exec rails c
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /vagrant && bundle exec rails c"
  #   ➜ vagrant exec rspec spec/
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /vagrant && bundle exec rspec spec/"
  config.exec.commands %w[rails rspec], prepend: 'bundle exec'

Environment variables

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

  # Automatically export environment variables for ruby command:
  #   ➜ vagrant exec ruby -e 'puts 1'
  #   # is the same as
  #   ➜ vagrant ssh -c "cd /vagrant && export RUBY_GC_MALLOC_LIMIT=100000000 && ruby -e 'puts 1'"
  config.exec.commands 'ruby', env: {'RUBY_GC_MALLOC_LIMIT' => 100000000}


It is possible to generate binstubs for all your configured commands. You might want to do this to avoid typing vagrant exec every time before command, or if you want integrate your flow in editor (e.g. running tests from editor).

Assuming you have the following configuration:

Vagrant.configure('2') do |config| = 'precise32'
  config.exec.commands 'bundle'
  config.exec.commands %w[rails rake], prepend: 'bundle exec'
  config.exec.commands %w[rspec cucumber], prepend: 'spring'

You can generate binstubs for each command:

➜ vagrant exec --binstubs
Generated binstub for bundle in bin/bundle.
Generated binstub for cucumber in bin/cucumber.
Generated binstub for rails in bin/rails.
Generated binstub for rake in bin/rake.
Generated binstub for rspec in bin/rspec.

Now you can use bin/cucumber instead of vagrant exec cucumber to execute commands in VM. All your configuration (directory, environment variables, prepend) will still be used.

Since binstubs use plain SSH to connect to VM, it creates connection much faster because Vagrant is not invoked:

time bin/echo 1
        0.28 real
➜ time vagrant exec echo 1
        2.84 real

To make plain SSH work, it saves current SSH configuration of Vagrant to temporary file when you run vagrant exec --binstubs. This means that if your VM network configuration has changed (IP address, port), you will have to regenerate binstubs again. So far I had no problems with this, but it's not very convenient for sure.

Moving forward, you can use projects like direnv to add bin/ to PATH and completely forget that you have VM running.

It is also possible to configure binstubs directory (e.g. default bin/ collides with Rails binstubs):

Vagrant.configure('2') do |config| = 'precise32'
  config.exec.binstubs_path = 'vbin'


Before running features, you'll need to bootstrap box.

➜ bundle exec rake features:bootstrap

To run features, execute the following rake task.

➜ bundle exec rake features:run

After you're done, remove Vagrant box.

➜ bundle exec rake features:cleanup

To show stdout, add @announce-stdout tag to scenario/feature.

Known issues

vagrant-exec cannot properly handle -v in command args (it's caught somewhere before plugin), so executing vagrant exec ruby -v will return Vagrant version rather than Ruby. As a workaround, wrap it in quotes: vagrant exec "ruby -v".

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.


Copyright (c) 2013-2015 Alex Rodionov. See for details.


Execute commands in Vagrant synced folder





No packages published


You can’t perform that action at this time.