2

I'm using Vagrant boxes for several purposes, and in most cases the prepared boxes I can download are totally sufficient. However, sometimes I need one or a few additional packages installed on the box—for regular activities, not one-time use.

For example, I have a Vagrant box on my Mac (Linux guest on Mac host) which I primarily want for easy access to built-in Linux documentation (man pages, info pages, etc.) However, the box I am using doesn't have the man package installed when I first vagrant up.

If I just install the package manually, it will only last until the next vagrant destroy. If I use vagrant halt instead, I'll build up cruft on my system, which defeats the purpose of using vagrant as opposed to simply a Virtual Box.

I know the general answer to this is vagrant provisioning, and it could be as simple as a script with a single command sudo yum install man. (Although actually I don't even know specifically how to do that.) However, rather than downloading the man package and all dependencies each time I start the vagrant box, I would rather download those packages once, store them on my host system, and load them into the vm each time I vagrant up.

I have already run sudo yum install man --downloadonly on the guest, to get the packages I need, and sudo cp -r /var/cache/yum/x86_64/6/base/packages/ /vagrant/ to put the package files on my host.

So my question is really twofold:

  1. What is the vagrant provisioning syntax to use for: (a) calling a script (to be stored in the same directory as the Vagrantfile), or (b) if possible, embedding a single command in the Vagrantfile (such as sudo yum install man)?

  2. How can I give yum the option flags needed to install a package from a local directory (/vagrant) and to install any needed dependencies from that same local directory rather than from an online repository?

1

For example, I have a Vagrant box on my Mac (Linux guest on Mac host)

It would be interesting to know which distribution this is... Given that you're trying to use yum I imagine this is some RH/CentOS/.. stuff?

which I primarily want for easy access to built-in Linux documentation (man pages, info pages, etc.) However, the box I am using doesn't have the man package installed when I first vagrant up.

I don't know any distribution that doesn't install manby default.

If I were you, I'd use one of the official boxes on ATLAS, Debian comes to mind: https://vagrantcloud.com/debian/

Reply to Q1

Add this to your vagrantfile:

$script = <<SCRIPT
echo I am provisioning...
date > /etc/vagrant_provisioned_at
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: $script
end

Example taken from http://docs.vagrantup.com/v2/provisioning/shell.html#inline-scripts

This would execute the script at the top, resulting in the creation of /etc/vagrant_provisioned_at

Further adapted to your question:

$script = <<SCRIPT
yum install man
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: $script
end

The provisioning script is run as UID0, so no need for sudo.

Reply to Q2

I'm a Debian user and as such RedHat illiterate ;-)

IMHO this would be rpm -hiv <filename>(i =install, h = hashmarks, v = verbose).

Related manpage

  • Perfect! Yes, I'm using CentOS; the vm box is "bento/centos-6.7" and it doesn't come with man. (Which is an odd setup; my physical CentOS install does.) rpm isn't smart enough to handle dependencies and just exits with an error if given the single file, BUT it is smart enough to put a list of filename arguments in dependency sequence. Since I already downloaded the dependencies as described in my question, I just used rpm -i /vagrant/packages/* and it puts them in order and installs them all! (No need for -hv as it's just run during provisioning.) – Wildcard Oct 9 '15 at 13:08
  • 1
    I found the puppetlabs base boxes quite "vanilla", try one of those: atlas.hashicorp.com/puppetlabs They have a good naming scheme btw... – Jan Oct 9 '15 at 13:14
  • Aha! Even better; I've just switched over. Thanks! Still great to have the provisioning data/know-how; I will use that for my own setup and package installs in the future (rather than stuff like man which should already be there....) – Wildcard Oct 9 '15 at 13:28
0

I just encountered this use case again; I will make a succinct record here of how I handled it.

I wanted to download (but not install) Git and PostgreSQL, and then install them during the vagrant provision process.

I used the following command from a fresh VM to download the packages with all dependencies (after sudo mkdir /vagrant/packages):

sudo yumdownloader --destdir=/vagrant/packages/ --resolve -y git postgresql

The trick with this command is, it downloads way too much. The next step was to establish the RPMs that I actually needed for the install. I did this by attempting:

sudo yum install /vagrant/packages/git-1.7.1-4.el6_7.1.x86_64.rpm /vagrant/packages/postgresql-8.4.20-6.el6.x86_64.rpm

And pressing "No" at the confirmation prompt because I could see it was planning to download dependencies. I took the listed dependencies and added them to my command:

sudo yum install /vagrant/packages/git-1.7.1-4.el6_7.1.x86_64.rpm /vagrant/packages/postgresql-8.4.20-6.el6.x86_64.rpm /vagrant/packages/perl-* /vagrant/packages/postgresql-libs-8.4.20-6.el6.x86_64.rpm

This would successfully work without downloading anything, so I then ran:

cd /vagrant/packages/
mkdir save
mv /vagrant/packages/git-1.7.1-4.el6_7.1.x86_64.rpm /vagrant/packages/postgresql-8.4.20-6.el6.x86_64.rpm /vagrant/packages/perl-* /vagrant/packages/postgresql-libs-8.4.20-6.el6.x86_64.rpm save/
rm *.rpm
mv save/* .
rmdir save

Result: A "packages" directory with only the dependencies actually needed from the fresh Vagrant image.

Then by adding the shell provisioning command to my Vagrantfile:

  config.vm.provision "shell", inline: <<-SHELL
    yum install -C -y /vagrant/packages/*
  SHELL

...I have Git and PostgreSQL installed and ready to use after any vagrant up, even if vagrant destroy was run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.