[node.js] How to yum install Node.JS on Amazon Linux

I've seen the writeup on using yum to install the dependencies, and then installing Node.JS & NPM from source. While this does work, I feel like Node.JS and NPM should both be in a public repo somewhere.

How can I install Node.JS and NPM in one command on AWS Amazon Linux?

This question is related to node.js amazon-web-services npm yum amazon-linux

The answer is


https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions

curl --silent --location https://rpm.nodesource.com/setup_10.x | sudo bash - sudo yum -y install nodejs


sudo yum install nodejs npm --enablerepo=epel works for Amazon Linux AMI. curl --silent --location https://rpm.nodesource.com/setup_6.x | bash - yum -y install nodejs works for RedHat.


Seems no one is mentioning this. On Amazon Linux 2, official way to load EPEL is:

  • sudo amazon-linux-extras install epel

...then you may:

  • sudo yum install nodejs

     

See Extras Library (Amazon Linux 2)


As mentioned in official documentation , simple below 2 steps -

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
sudo apt-get install -y nodejs

For those who want to have the accepted answer run in Ansible without further searches, I post the task here for convenience and future reference.

Accepted answer recommendation: https://stackoverflow.com/a/35165401/78935

Ansible task equivalent

tasks:
  - name: Setting up the NodeJS yum repository
    shell: curl --silent --location https://rpm.nodesource.com/setup_10.x | bash -
    args:
      warn: no
  # ...

As others mentioned using epel gives a really outdated version, here is a little script I just wrote instead to add to the CI pipeline or pass it to ec2 user-data to install the latest version of node, simply replace the version with what you want, and the appropriate distro of Linux you are using.

The following example is for amazon-Linux-2-AMI

#!/bin/bash

version='v14.13.1'
distro='linux-x64'
package_name="node-$version-$distro"
package_location="/usr/local/lib/"

curl -O https://nodejs.org/download/release/latest/$package_name.tar.gz
tar -xvf $package_name.tar.gz -C $package_location
rm -rfv $package_name.tar.gz

echo "export PATH=$package_location/$package_name/bin:\$PATH" >> ~/.profile

if you want to test it in the same shell simply run

. ~/.profile

The easiest solution is this( do these as root)

sudo su root
cd /etc
mkdir node
yum install wget
wget https://nodejs.org/dist/v9.0.0/node-v9.0.0-linux-x64.tar.gz
tar -xvf node-v9.0.0-linux-x64.tar.gz
cd node-v9.0.0-linux-x64/bin
./node -v
ln -s /etc/node-v9.0.0-linux-x64/bin/node node

enter image description here


The accepted answer gave me node 0.10.36 and npm 1.3.6 which are very out of date. I grabbed the latest linux-x64 tarball from the nodejs downloads page and it wasn't too difficult to install: https://nodejs.org/dist/latest/.

# start in a directory where you like to install things for the current user
(For noobs : it downloads node package as node.tgz file in your directlry)
curl (paste the link to the one you want from the downloads page) >node.tgz

Now upzip the tar you just downloaded -

tar xzf node.tgz

Run this command and then also add it to your .bashrc:

export PATH="$PATH:(your install dir)/(node dir)/bin"

(example : export PATH ="$PATH:/home/ec2-user/mydirectory/node/node4.5.0-linux-x64/bin")

And update npm (only once, don't add to .bashrc):

npm install -g npm

Note that the -g there which means global, really means global to that npm instance which is the instance we just installed and is limited to the current user. This will apply to all packages that npm installs 'globally'.


You can update/install the node by reinstalling the installed package to the current version which may save us from lotta of errors, while doing the update.

This is done by nvm with the below command. Here, I have updated my node version to 8 and reinstalled all the available packages to v8 too!

nvm i v8 --reinstall-packages-from=default

It works on AWS Linux instance as well.


Official Documentation for EC2-Instance works for me: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-up-node-on-ec2-instance.html

 1. curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash
 2. . ~/.nvm/nvm.sh
 3. nvm ls-remote (=> find your version x.x.x =>) nvm install  x.x.x
 4. node -e "console.log('Running Node.js ' + process.version)"

Like others, the accepted answer also gave me an outdated version.

Here is another way to do it that works very well:

$ curl --silent --location https://rpm.nodesource.com/setup_14.x | bash -
$ yum -y install nodejs

You can also replace the 14.x with another version, such as 12.x, 10.x, etc.

You can see all available versions on the NodeSource Github page, and pull from there as well if desired.

Note: you may need to run using sudo depending on your environment.


I just came across this. I tried a few of the more popular answers, but in the end, what worked for me was Amazon's quick setup guide.

Tutorial: Setting Up Node.js on an Amazon EC2 Instance

The gist of the tutorial is:

  1. Make sure you are ssh'd onto the instance.
  2. Grab nvm: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash
  3. Active . ~/.nvm/nvm.sh
  4. Install node using nvm nvm install 4.4.5 (NOTE: You can choose a different version. Check out the remote versions first by running $ nvm ls-remote)
  5. Finally, test that you have installed node Node correctly by running $ node -e "console.log('Running Node.js' + process.version)"

Hopefully this helps the next person.


I usually use NVM to install node on server. It gives me option to install multiple version of nodejs. Commands are given below

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

then check if it's install properly

command -v nvm

after that, run this to install latest version

nvm install node

or

nvm install 11


I had Node.js 6.x installed and wanted to install Node.js 8.x.

Here's the commands I used (taken from Nodejs's site with a few extra steps to handle the yum cached data):

  1. sudo yum remove nodejs: Uninstall Node.js 6.x (I don't know if this was necessary or not)
  2. curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
  3. sudo yum clean all
  4. sudo yum makecache: Regenerate metadata cache (this wasn't in the docs, but yum kept trying to install Node.jx 6.x, unsuccessfully, until I issued these last two commands)
  5. sudo yum install nodejs: Install Node.js 8.x

For the v4 LTS version use:

curl --silent --location https://rpm.nodesource.com/setup_4.x | bash -
yum -y install nodejs

For the Node.js v6 use:

curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
yum -y install nodejs

I also ran into some problems when trying to install native addons on Amazon Linux. If you want to do this you should also install build tools:

yum install gcc-c++ make

Simple install with NVM...

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install node

To install a certain version (such as 12.16.3) of Node change the last line to

nvm install 12.16.3

For more information about how to use NVM visit the docs: https://github.com/nvm-sh/nvm


The procedure that worked for me (following these rather old instructions with a few updates):

  • check git is installed git --version or install it via:
    sudo yum install git
  • install gcc and openssl:
    sudo yum install gcc-c++ make
    sudo yum install openssl-devel
  • clone the git repo into a directory called node (which you can remove later):
    git clone https://github.com/nodejs/node.git
  • decide which version of node you want at https://github.com/nodejs/node/releases
  • go to the node directory just created and install node
    cd node
    git checkout v6.1.0 - put your desired version after the v
    ./configure
    make
    sudo make install
  • test that node is installed / working with either node --version or simply node (exit node via process.exit() or ^C x 2 or ^C + exit)
  • check the npm version: npm --version and update if necessary via sudo npm install -g npm
  • Optional: remove the node directory with rm -r node

Notes:

  1. The accepted answer didn't work since sudo yum install nodejs --enablerepo=epel-testing returns the error: No package nodejs available.
    ...and sudo yum install nodejs --enablerepo=epel (ie without -testing) only gave very old versions.
  2. If you already have an old version of node installed you can remove it with:
    sudo npm uninstall npm -g ...since npm can uninstall itself
    sudo yum erase nodejs
    sudo rm -f /usr/local/bin/node
    (sudo yum rm nodejs in the accepted answer won't work as rm is not a valid yum command see yum --help)
  3. It's possible to clone the node repo via git clone git://github.com/nodejs/node.git rather than git clone https://github.com/nodejs/node.gitbut you may get a various errors (see here).
  4. If you already have a /node dir from a previous install, remove it before using the git clone command (or there'll be a conflict):
    rm -r node
  5. If you have trouble with any sudo npm... command - like sudo: npm: command not found and/or have permissions issues installing node packages without sudo, edit sudo nano /etc/sudoers and add :/usr/local/bin to the end of the line Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin so that it reads Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to amazon-web-services

How to specify credentials when connecting to boto3 S3? Is there a way to list all resources in AWS Access denied; you need (at least one of) the SUPER privilege(s) for this operation Job for mysqld.service failed See "systemctl status mysqld.service" What is difference between Lightsail and EC2? AWS S3 CLI - Could not connect to the endpoint URL boto3 client NoRegionError: You must specify a region error only sometimes How to write a file or data to an S3 object using boto3 Missing Authentication Token while accessing API Gateway? The AWS Access Key Id does not exist in our records

Examples related to npm

What does 'x packages are looking for funding' mean when running `npm install`? error: This is probably not a problem with npm. There is likely additional logging output above Module not found: Error: Can't resolve 'core-js/es6' Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist` ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean` What exactly is the 'react-scripts start' command? On npm install: Unhandled rejection Error: EACCES: permission denied Difference between npx and npm?

Examples related to yum

How to Install gcc 5.3 with yum on CentOS 7.2? Completely remove MariaDB or MySQL from CentOS 7 or RHEL 7 RHEL 6 - how to install 'GLIBC_2.14' or 'GLIBC_2.15'? CentOS 7 and Puppet unable to install nc How to yum install Node.JS on Amazon Linux yum error "Cannot retrieve metalink for repository: epel. Please verify its path and try again" updating ContextBroker How to install latest version of git on CentOS 7.x/6.x Upgrading PHP on CentOS 6.5 (Final) Yum fails with - There are no enabled repos. Why does configure say no C compiler found when GCC is installed?

Examples related to amazon-linux

Amazon Linux: apt-get: command not found How to yum install Node.JS on Amazon Linux