(Last Updated On: May 30, 2019)

How to install Packer on Ubuntu 18.04?. Packer is an open source tool for creating identical machine images for multiple platforms from a single source configuration. It is a cross-platform command-line and lightweight application capable of generating and highly machine images for multiple platforms in parallel.

Check Packer use cases if you’re new to it and trying to figure out how you benefit from this tool.

What is Packer Machine Image?

A machine image is a single static unit that contains a pre-configured operating system and installed software which is used to quickly create new running machines. Machine image formats change for each platform. Some examples include AMIs for EC2, VMDK/VMX files for VMware, OVF exports for VirtualBox, etc.

How to Install Packer on Ubuntu 18.04?

Packer may be installed from a precompiled binary or from source. The easy and recommended method for all users is binary installation method. This is the one used in this article.

First, check the latest release of Packer on the Downloads page. Then download the recent version for your platform. Since we’re doing this on Ubuntu 18.04, download the Linux version.

export VER="1.4.1"
wget https://releases.hashicorp.com/packer/${VER}/packer_${VER}_linux_amd64.zip

Uncompress downloaded file.

unzip packer_${VER}_linux_amd64.zip

Move the packer binary to /usr/local/bin directory.

sudo mv packer /usr/local/bin

After installing Packer, verify the installation is working by checking that the packer is available:

$ packer
Usage: packer [--version] [--help]
[]
Available commands are:
build build image(s) from template
fix fixes templates from old versions of packer
inspect see components of a template
validate check that a template is valid
version Prints the Packer version

If you placed the packer bi in a different directory, the PATH variable should contain this directory.

How to Use Packer

Builders are used to generate images and create machines for various platforms from templates. You can see a full list of suppported builders. A template is a configuration file used to define what image is built and its format is JSON.

In our example below, we will use VirtualBox Builder to create an Ubuntu 18.04 VirtualBox Virtual Machine and export it in the OVA or OVF format.

Let’s create our working directory.

mkdir projects/packer/
cd projects/packer/

Create a file called build.json with below contents

{
    "variables": {
        "hostname": "ubuntu",
        "domain": "local",
        "ssh_user": "packer",
        "ssh_pass": "[email protected]"
    },
    "builders": [{
        "type": "virtualbox-iso",
        "guest_os_type": "Ubuntu_64",
        "vm_name": "ubuntu-18.04-vbox-template",
        "iso_url": "http://cdimage.ubuntu.com/releases/18.04/release/ubuntu-18.04.2-server-amd64.iso",
        "iso_target_path": "/home/jmutai/iso/ubuntu-18.04.2-server-amd64.iso",
        "iso_checksum": "34416ff83179728d54583bf3f18d42d2",
        "iso_checksum_type": "md5",
        "memory": "1024",
        "cpus": "1",
        "disk_size": "20000",
        "format": "ova",
        "guest_additions_mode": "upload",
        "headless": "false",
        "http_directory": "http",
        "ssh_username": "{{user `ssh_user`}}",
        "ssh_password": "{{user `ssh_pass`}}",
        "ssh_wait_timeout": "20m",
        "shutdown_command": "sudo /sbin/halt -p",
        "boot_command": [
            "<enter><wait><f6><esc><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "<bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs><bs>",
            "/install/vmlinuz<wait>",
            " auto<wait>",
            " console-setup/ask_detect=false<wait>",
            " console-setup/layoutcode=us<wait>",
            " console-setup/modelcode=pc105<wait>",
            " debconf/frontend=noninteractive<wait>",
            " debian-installer=en_US<wait>",
            " fb=false<wait>",
            " initrd=/install/initrd.gz<wait>",
            " kbd-chooser/method=us<wait>",
            " keyboard-configuration/layout=USA<wait>",
            " keyboard-configuration/variant=USA<wait>",
            " locale=en_US<wait>",
            " netcfg/get_domain={{user `domain`}}<wait>",
            " netcfg/get_hostname={{user `hostname`}}<wait>",
            " grub-installer/bootdev=/dev/sda<wait>",
            " noapic<wait>",
            " preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg",
            " -- <wait>",
            "<enter><wait>"
        ]
    }],

    "provisioners": [{
        "type": "shell",
        "inline": [
            "sleep 30",
            "sudo apt update",
            "sudo apt -y install bash-completion wget vim php php-fpm php-mysql"
        ]
    }]
}

Change the settings to fit ypur deployment design. We also need a preseed file to automate installation of Ubuntu 18.04 on VirtualBox with Packer.

Create a http directory as specified in JSON build file.

mkdir http

Then paste below to http/preseed.cfg file.

# Language and Locale
d-i debian-installer/language string en
d-i debian-installer/locale string en_US.UTF-8
d-i localechooser/preferred-locale string en_US.UTF-8
d-i localechooser/supported-locales en_US.UTF-8

# Hostname / domain
d-i netcfg/get_hostname string ubuntu
d-i netcfg/get_domain string local


# Keyboard
d-i console-setup/ask_detect boolean false
d-i keyboard-configuration/layout select USA
d-i keyboard-configuration/variant select USA
d-i keyboard-configuration/modelcode string pc105

# Timezone / Time
d-i time/zone string UTC
d-i clock-setup/utc-auto boolean true
d-i clock-setup/utc boolean true

# Server
tasksel tasksel/first multiselect standard, ubuntu-server

# No proxy
d-i mirror/http/proxy string

# Packages Policy
d-i pkgsel/install-language-support boolean false
d-i pkgsel/update-policy select none
d-i pkgsel/upgrade select full-upgrade
d-i pkgsel/include string openssh-server cryptsetup build-essential libssl-dev libreadline-dev zlib1g-dev

# Partitioning
d-i partman-auto/method string lvm
d-i partman-auto-lvm/guided_size string max
d-i partman-auto/choose_recipe select atomic
d-i partman-auto/disk string /dev/sda
d-i partman-lvm/confirm boolean true
d-i partman-lvm/confirm_nooverwrite boolean true
d-i partman-lvm/device_remove_lvm boolean true
d-i partman/confirm_nooverwrite boolean true
d-i partman/confirm boolean true
d-i partman/confirm_write_new_label boolean true
d-i partman/choose_partition select finish


# Create packer user account.
d-i passwd/user-fullname string packer
d-i passwd/username string packer
d-i passwd/user-password password [email protected]
d-i passwd/user-password-again password [email protected]
d-i user-setup/allow-password-weak boolean true
d-i user-setup/encrypt-home boolean false
d-i passwd/user-default-groups packer sudo

If using this example, you’ll need to change username and password for user to be created in the preseed file.

To build your image by running the following command.

packer build build.json

Since we had turned off installation in headless mode, you should be able to watch the installation on VirtualBox UI.

I’ll cover the usage of other providers such as Docker, AWS, Cloudstack, Qemu e.t.c in the following guides. Subscribe to our newsletter to get instant updates.

Also check:

How to Create CentOS / Fedora / RHEL VM Templates on KVM

Provisioning of VMs on KVM with Terraform