How to yum install Node.JS on Amazon Linux

170,301

Solution 1

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/

Solution 2

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.

Solution 3

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

Solution 4

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

Solution 5

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
Share:
170,301

Related videos on Youtube

Tim Fulmer
Author by

Tim Fulmer

I am a super-senior software / platform engineer looking to get hands on with your toughest technical challenges. At Domuso I scaled a poorly engineered Groovy application by breaking problem areas out into a new event based serverless architecture in Node.JS running on AWS Lambda. At HopSkipDrive I solved a technical challenge with measuring actual miles driven for the CPUC on a very tight deadline, passing a regulatory hurdle some other ride sharing companies could not.

Updated on July 08, 2022

Comments

  • Tim Fulmer
    Tim Fulmer almost 2 years

    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?

  • Semicolon
    Semicolon about 9 years
    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.
  • voltrevo
    voltrevo over 8 years
    I got node 0.10.36 and npm 1.3.6 out of this. These are very out of date.
  • unboundev
    unboundev over 8 years
    @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.
  • RandomDeduction
    RandomDeduction over 8 years
    If using AWS linux and this method, do NOT use n. Instead use nvm to upgrade to the latest version. n doesn't upgrade as expected.
  • Neta
    Neta over 8 years
    @RandomDeduction's comment fixed it for me. Tried n before and didn't work. nvm's github page - for AWS linux (Amazon Linux) use cURL and then simply nvm install 5.0 (latest as of now).
  • Mariusz
    Mariusz over 8 years
    This is actually the best answer, as you get exactly the version you want
  • user115014
    user115014 over 8 years
    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
  • Masadow
    Masadow over 8 years
    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
  • Zeno Popovici
    Zeno Popovici over 8 years
    @Masadow You shall live 100 years for this kind gesture Sir! Thanks a lot.
  • Tim Fulmer
    Tim Fulmer over 8 years
    @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!
  • Jon Burgess
    Jon Burgess over 8 years
    This is what I needed to use on AWS Elastic Beanstalk to get a more recent version than that already installed.
  • eQ19
    eQ19 about 8 years
    Works just perfect while I need to run npm install for package.json,
  • user1336321
    user1336321 about 8 years
    for old centos versions this is the best way to go
  • Dakotah North
    Dakotah North about 8 years
    I needed to run this with curl --silent --location https://rpm.nodesource.com/setup_4.x | sudo bash -
  • David Webber
    David Webber about 8 years
    This is a fantastic answer. It no longer takes me 20 minutes to install Node anymore.
  • goredwards
    goredwards about 8 years
    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...
  • Marek Jalovec
    Marek Jalovec almost 8 years
    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/1ccee0c2254e65fc5d82eb35c7da82a‌​e
  • goredwards
    goredwards almost 8 years
    @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.
  • nackjicholson
    nackjicholson almost 8 years
    Another option. I did sudo yum install nodejs npm --enablerepo=epel and then I installed github.com/creationix/nvm and used it to install other versions of node.
  • Jayant Bhawal
    Jayant Bhawal almost 8 years
    This totally works. Takes less time than @goredwards answer too. Tried on an Amazon ECS optimized AMI.
  • seantomburke
    seantomburke over 7 years
    The accepted answer and all of the EDITs 1-3 didn't work for me, but this worked. Thanks!
  • Daron Tancharoen
    Daron Tancharoen over 7 years
    This is very useful after I made a mistake with the accept answer. Thank you
  • Jos Faber
    Jos Faber over 7 years
    Error after running node -v now is: 'cannot execute binary file'. Any ideas?
  • voltrevo
    voltrevo over 7 years
    @JosFaber Hmm this might happen if you download node for a different architecture. You probably want the one that ends in -linux-x64.tar.gz .
  • goredwards
    goredwards over 7 years
    @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
  • Stephen Tetreault
    Stephen Tetreault over 7 years
    neat and compact.
  • Abhinav
    Abhinav over 7 years
    simple and awesome
  • Kirkland
    Kirkland about 7 years
    This should be the answer. It's best best by far unless someone is looking to build from source.
  • BusyProgrammer
    BusyProgrammer about 7 years
    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.
  • sampoh
    sampoh about 7 years
    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
  • JK.
    JK. almost 7 years
    Two issues: 1 - you forgot sudo. 2 - this requires two commands, but your text makes it look like one command. The correct answer is: $ curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash - and then $ sudo yum -y install nodejs. Don't enter the $ sign obviously, thats just your command prompt.
  • Argun
    Argun over 6 years
    if you wanna install Node.js 8.x, shoud execute curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -
  • BuffMcBigHuge
    BuffMcBigHuge over 6 years
    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
  • PK.
    PK. over 6 years
    how do i install 6 to install 7 or 8 if i have already use this method to install 6? i tried install adding the 7 nodesource and uninstalling nodejs and npm and reinstalling but it still installs 6.
  • Miao ZhiCheng
    Miao ZhiCheng about 6 years
    very old node version, not recommended
  • newbreedofgeek
    newbreedofgeek about 5 years
    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
  • Stretch
    Stretch over 4 years
    well this seems like the best answer!
  • GabrielBB
    GabrielBB about 4 years
    @Stretch Not really. It installs Node 6. Too old.
  • Exelian
    Exelian over 3 years
    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.
  • IAM_AL_X
    IAM_AL_X about 3 years
    But can I get a global install? I want "node" in /usr/bin, or the like. Everything I have read, (plus some non-definitive experience) suggests "nvm" wants to install node locally, and then modify my personal PATH so that I personally can find it. But for me, a "global" install, when referring to a CLI, means that all users have access to the executable, without a special PATH modification. (Contrast w regards to a lib in node_modules for "nvm/npm", the word "global"can mean "global to one user".) I am not confident that the "nvm" solution allows for system global. Anybody know for sure?
  • X.X
    X.X over 2 years
    this worked perfectly on Amazon Linux 1 (not the amazon linux2). The curl setup_12.X method doesn't work since it doesn't update my existing node 10.x to 12.x as desired. But this nvm method works out great.
  • Zahid Hassan Alvi
    Zahid Hassan Alvi over 2 years
    this is the best answer actually!
  • Richard Tyler Miles
    Richard Tyler Miles about 2 years
    note you can use bash rather than zsh as bash is preinstalled.