alonisser
alonisser

Reputation: 12088

Packer: Using npm install in packer script - fails with command not found error

I've been playing with packer lately and found it an amazing tool. But I'm failing to use installed tools in the installation script.

Here's an example packer template I've been playing with (I've know there are better ways to use this with scripts and provisioners. but for simplicity sake I'm learning with this) :

{
  "variables":{
    "aws_access_key": "{{env `AWS_ACCESS_KEY`}}",
    "aws_secret_key": "{{env `AWS_SECRET_KEY`}}"
  },
  "builders":[{
    "type": "amazon-ebs",
    "access_key":"{{user `aws_access_key`}}",
    "secret_key":"{{user `aws_secret_key`}}",
    "region": "us-east-1",
    "source_ami": "ami-de0d9eb7",
    "instance_type": "t1.micro",
    "ssh_username": "ubuntu",
    "ami_name": "packer-example2 {{timestamp}}"

  }],
  "provisioners":[{
    "type": "shell",
    "inline":[
      "sleep 30",
      "echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list",
      "echo 'deb http://www.rabbitmq.com/debian/ testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list",
      "sudo apt-get-repository ppa:chris-lea/node.js",
      "wget http://www.rabbitmq.com/rabbitmq-signing-key-public.asc",
      "sudo apt-key add rabbitmq-signing-key-public.asc",
      "sudo apt-get update",
      "sudo apt-get install -y build-essential python-dev",
      "sudo apt-get build-dep -y python-imaging",
      "sudo apt-get install -y redis-server",
      "sudo apt-get install -y mongodb-org",
      "sudo apt-get install -y nodejs",
      "sudo apt-get install -y nginx",
      "sudo apt-get install -y rabbitmq-server",
      "sudo apt-get install -y git",
      "sudo apt-get -y install postgresql libpq-dev postgresql-contrib",
      "sudo apt-get -y install htop",
      "sudo apt-get -y install sysstat",
      "sudo apt-get install -y curl",
      "sudo apt-get install -y ntp",
      "sudo apt-get install -y wget",
      "npm install -g swamp bower grunt"
    ]
  }]
}

And I'm getting the following error message:

amazon-ebs: sudo: npm: command not found

I know npm installs with nodejs (at least from my experience) so I'm probably missing something. I guess this would also happen when I try to use similar platform install tools as:pip, gem etc...

Upvotes: 3

Views: 2402

Answers (1)

alonisser
alonisser

Reputation: 12088

It's been some time since I asked this question, I revisited and now I'm able to answer this my self (now I use packer to build our production machines, still great) with some pointers to getting a Working Packer solution

  1. Read the actual installation log, it points to the problem! It might be with a different component (as was the case here), but the problem (the apt-repo not added) might appear long before the error.
  2. The actual problem was that sudo apt-add-repository for the node.js repo, never worked, So I really didn't have npm installed, and got an old node.js version that's bundled with ubuntu repository (without chris-lea repository) and doesn't have npm.
  3. In order for apt-add-repository to work, some pre requirements had to be installed in the script before:

sudo apt-get install -y software-properties-common sudo apt-get install -y python-software-properties

  1. A good idea is also to install git, curl, wget, unzip, etc.. before trying to install an npm module, since some node modules/tools, actually depend on them.

  2. Don't use ubuntu node/npm bundle at all.. install nvm and with it install whatever versions of node.js and IO.js you want. Don't forget to source the nvm.sh before trying to use your new npm..

Hope this helps any one who encounters this problem.

Upvotes: 2

Related Questions