ChatGPT解决这个技术问题 Extra ChatGPT

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?


M
Matthew Herbst

Stumbled onto this, was strangely hard to find again later. Putting here for posterity:

sudo yum install nodejs npm --enablerepo=epel

EDIT 3: As of July 2016, EDIT 1 no longer works for nodejs 4 (and EDIT 2 neither). This answer (https://stackoverflow.com/a/35165401/78935) gives a true one-liner.

EDIT 1: If you're looking for nodejs 4, please try the EPEL testing repo:

sudo yum install nodejs --enablerepo=epel-testing

EDIT 2: To upgrade from nodejs 0.12 installed through the EPEL repo using the command above, to nodejs 4 from the EPEL testing repo, please follow these steps:

sudo yum rm nodejs
sudo rm -f /usr/local/bin/node
sudo yum install nodejs --enablerepo=epel-testing

The newer packages put the node binaries in /usr/bin, instead of /usr/local/bin.

And some background:

The option --enablerepo=epel causes yum to search for the packages in the EPEL repository.

EPEL (Extra Packages for Enterprise Linux) is open source and free community based repository project from Fedora team which provides 100% high quality add-on software packages for Linux distribution including RHEL (Red Hat Enterprise Linux), CentOS, and Scientific Linux. Epel project is not a part of RHEL/Cent OS but it is designed for major Linux distributions by providing lots of open source packages like networking, sys admin, programming, monitoring and so on. Most of the epel packages are maintained by Fedora repo. Via http://www.tecmint.com/how-to-enable-epel-repository-for-rhel-centos-6-5/


This is definitely the fastest approach I've seen, but a warning may be useful -- the EPEL repository isn't in sync with the current stable node, and you can't use "n" to fix that when it's been installed this way (at least, not without some kind of magic that's beyond me). The tedious git clone / make install approach is the only way I've found to ensure a consistent and current install.
I got node 0.10.36 and npm 1.3.6 out of this. These are very out of date.
@Semicolon you can get around that by only installing npm, then sudo npm install -g n and sudo n v0.12 or whatever other version you like.
n update as expected, but it does not update the node link properly. You'll have to run the extra command : sudo ln -sf /usr/local/n/versions/node/<VERSION>/bin/node /usr/bin/node
this answer didn't work since sudo yum install nodejs --enablerepo=epel-testing returns the error: "No package nodejs available." while sudo yum install nodejs --enablerepo=epel only gave very old versions...
M
Matthew Herbst

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_16.x | bash -
$ yum -y install nodejs

You can also replace the 16.x with another version, such as 18.x, 14.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.


This is what I needed to use on AWS Elastic Beanstalk to get a more recent version than that already installed.
Works just perfect while I need to run npm install for package.json,
I needed to run this with curl --silent --location https://rpm.nodesource.com/setup_4.x | sudo bash -
If you get permission denied, you'll need to add some sudo. curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash - and sudo yum -y install nodejs
if you wanna install Node.js 8.x, shoud execute curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -
D
Darpan

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'.


This is actually the best answer, as you get exactly the version you want
Wish I'd read this answer first - this should be the accepted answer because you can choose which version you want to install. -- thank you @voltrevo
@voltrevo thank you for the answer. Yes, there are other ways to install node on Linux. The accepted answer is a one-liner, which happens to plug into the YUM package system for automated and managed updates. The packages in the YUM repos do tend to be a little bit older, and are also a bit better tested with wider deployments. Personally, I recommend leaving the latest and greatest to local development environments, and use something more like the accepted answer for production environments. Cheers!
for old centos versions this is the best way to go
Error after running node -v now is: 'cannot execute binary file'. Any ideas?
f
fuzzysearch

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


This totally works. Takes less time than @goredwards answer too. Tried on an Amazon ECS optimized AMI.
The accepted answer and all of the EDITs 1-3 didn't work for me, but this worked. Thanks!
neat and compact.
This should be the answer. It's best best by far unless someone is looking to build from source.
This will not work if you are using userdata via cloudformation's AWS::EC2::LaunchTemplate.. it wil lwork if you SSH into your EC2 and run it. I'm still stuck on getting it to work via userdata
g
goredwards

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:

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. 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) 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). 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 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


@Gio plenty of other answers to choose from if this one doesn't please you ;-) ...'in one command' wasn't the most important part of the question IMHO.
This is very useful after I made a mistake with the accept answer. Thank you
@talentedandrew the etc/sudoers file controls who can run what commands as what users on what machines - but will only run / find / look for commands that are in directories listed on its secure_path Node should be installed in /usr/local/bin and if it's there sudo should find it - see stackoverflow.com/a/31734090/3092596 If sudo doesn't find it, then that path needs to be added to sudo's secure_path - see also: superuser.com/a/927599/404543
I would like to add that you might have to fix your symbolic links after you install using this method: sudo ln -s /usr/local/bin/node /usr/bin/node sudo ln -s /usr/local/lib/node /usr/lib/node sudo ln -s /usr/local/bin/npm /usr/bin/npm sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf
N
Niklas Rosencrantz

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

This is a fantastic answer. It no longer takes me 20 minutes to install Node anymore.
if you run a php application with some npm compiled frontend on elastic beanstalk, you may use this. it's part of my install.config file that made it work -- gist.github.com/marekjalovec/1ccee0c2254e65fc5d82eb35c7da82ae
d
dz902

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)


well this seems like the best answer!
@Stretch Not really. It installs Node 6. Too old.
this is the best answer actually!
t
troxwalt

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:

Make sure you are ssh'd onto the instance. Grab nvm: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash Active . ~/.nvm/nvm.sh 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) 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.


You might want to explain te content in that link. A good answer only uses a link as a reference, not the main topic of the answer.
B
Brad W

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):

sudo yum remove nodejs: Uninstall Node.js 6.x (I don't know if this was necessary or not) curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash - sudo yum clean all 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) sudo yum install nodejs: Install Node.js 8.x


V
Vaibhav Gidde

RHEL, CentOS, CloudLinux, Amazon Linux or Fedora:

# As root
curl -fsSL https://rpm.nodesource.com/setup_12.x | bash -

# No root privileges
curl -fsSL https://rpm.nodesource.com/setup_12.x | sudo bash -

sudo yum install -y nodejs

D
Devs love ZenUML

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.


g
grepit

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

https://i.stack.imgur.com/uLwoq.jpg


S
Sunil Buddala

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


T
Taner Melikoglu

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)"

w
whoami - fakeFaceTrueSoul

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 installed properly using :

command -v nvm

after that, run this to install latest version :

nvm install node 

or

nvm install 11 

Thanks for your answer! Please be careful piping code retrieved from the internet directly into bash, you may expose yourself to exploits doing this. I would suggest downloading the source first and making sure it contains the script you expect.
R
Richard Tyler Miles

MAY 2022

I spent way too long on this. My Amazon Linux 2 configuration, running as root.

#!/usr/bin/env zsh

# https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users
echo "=================================N=O=D=E========================================"

cd /usr/local/bin || exit 90

git clone https://github.com/nvm-sh/nvm.git .nvm

\. "/usr/local/bin/.nvm/nvm.sh"

nvm install --lts

node -e "console.log('Running Node.js ' + process.version)"

cat << "EOF" > /etc/profile.d/npm.sh
#!/usr/bin/env bash
export NVM_DIR="/usr/local/bin/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm'}

EOF

chmod 755 /etc/profile.d/npm.sh

npm install -g npm

June 2022 - The system really hates when things arn't linked in the bin. Here's a small update to help if you need things accessible by other users. Admittedly adding /etc/profile.d/npm.sh is just what nvm suggests, but I find it over-rated. I think it could be removed in place of purely the ln -s. happy hacking

#!/bin/zsh

# https://stackoverflow.com/questions/11542846/nvm-node-js-recommended-install-for-all-users
echo "=================================N=O=D=E========================================"

cd /usr/local/bin || exit 90

git clone https://github.com/nvm-sh/nvm.git .nvm

# this uncontrolled script has an unbound variable $HOME
# @link https://github.com/Drop-In-Gaming/dropingaming.com/runs/6437329820?check_suite_focus=true
\. "/usr/local/bin/.nvm/nvm.sh" || true

# todo - try to install 18
nvm install --lts

nvm install 17

node -e "console.log('Running Node.js ' + process.version)"

cat << "EOF" > /etc/profile.d/npm.sh
#!/usr/bin/env bash
export NVM_DIR="/usr/local/bin/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm'}

EOF

echo 'source /etc/profile.d/npm.sh' >> /root/.bashrc

echo 'source /etc/profile.d/npm.sh' >> /root/.zshrc

echo 'source /etc/profile.d/npm.sh' >> /home/ssm-user/.bashrc

echo 'source /etc/profile.d/npm.sh' >> /home/ssm-user/.zshrc

echo 'source /etc/profile.d/npm.sh' >> /home/www-data/.bashrc

echo 'source /etc/profile.d/npm.sh' >> /home/www-data/.zshrc

chmod 755 /etc/profile.d/npm.sh

npm install -g npm

echo "===========================WHERE==IS==NODE==========================="

which node

which npm

echo "symlinking to /usr/bin/"

if [ -e /usr/bin/node ]; then

  sudo rm -f /usr/bin/node

fi

if [ -e /usr/bin/npm ]; then

  sudo rm -f /usr/bin/npm

fi

sudo ln -s "$(which node)" /usr/bin/

sudo ln -s "$(which npm)" /usr/bin/



note you can use bash rather than zsh as bash is preinstalled.
V
Vivek Chaturvedi

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

A
Andrei Cioara

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
  # ...

S
Soundharya AM

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.


M
Mo Hajr

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