Skip to content

Instantly share code, notes, and snippets.

@komuw
Created November 20, 2014 17:40
Show Gist options
  • Save komuw/b3b5d24977d4df7bd549 to your computer and use it in GitHub Desktop.
Save komuw/b3b5d24977d4df7bd549 to your computer and use it in GitHub Desktop.
Ansible task to install nvm and nodeJS
#first seen here: http://www.snip2code.com/Snippet/63701/Ansible-task-to-install-nvm-and-node
# Here is how to install nvm and node in an Ansible task.
# I tried a bunch of different things, and as usual it's simple, but you have to get it right.
# The important part is that you have to shell with /bin/bash -c and source nvm.sh
---
- name: Install nvm
shell: >
curl https://raw.githubusercontent.com/creationix/nvm/v0.7.0/install.sh | sh
creates=/home/{{ ansible_user_id }}/.nvm/nvm.sh
- name: Install node and set version
shell: >
/bin/bash -c "source ~/.nvm/nvm.sh && nvm install 0.10 && nvm alias default 0.10"
creates=/home/{{ ansible_user_id }}/.nvm/alias
@navmed
Copy link

navmed commented Sep 5, 2022

This is a combination of the posted solutions above that worked for to install a specific version for the current (non-root) user:

- name: Install nodejs
  hosts: all
  tasks:
    - name: Install nvm
      become: no
      ansible.builtin.shell: >
        curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.1/install.sh | bash
      args:
        executable: /bin/bash
        chdir: "$HOME"
        creates: "$HOME/.nvm/nvm.sh"

    - name: Install node
      become: no
      shell: >
        . {{ ansible_env.HOME }}/.nvm/nvm.sh && nvm install {{ item }}
      args:
        executable: /bin/bash
        chdir: "{{ ansible_env.HOME }}"
        creates: "{{ ansible_env.HOME }}/.nvm/versions/{{ item }}"
      loop:
        - 14.20.0

@felipe-rodriguez-parra
Copy link

@navmed This works great for non-root environments. Thanks for share! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment