我已经看到了使用yum安装依赖项,然后从源代码安装Node.JS和NPM的文章。虽然这是可行的,但我觉得Node.JS和NPM都应该在公共回购的某个地方。

如何在AWS亚马逊Linux上一个命令安装Node.JS和NPM ?


偶然发现了这个,奇怪的是后来很难再找到了。为子孙后代写在这里:

sudo yum install nodejs npm --enablerepo=epel

EDIT 3:截至2016年7月,EDIT 1不再适用于nodejs 4 (EDIT 2也一样)。这个答案(https://stackoverflow.com/a/35165401/78935)给出了一个真正的单行程序。

编辑1:如果你正在寻找nodejs 4,请尝试EPEL测试repo:

sudo yum install nodejs --enablerepo=epel-testing

编辑2:使用上面的命令从EPEL repo安装的nodejs 0.12升级到EPEL测试repo的nodejs 4,请遵循以下步骤:

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

较新的包将节点二进制文件放在/usr/bin中,而不是/usr/local/bin。

一些背景知识:

选项——enablerepo=epel会导致yum在epel存储库中搜索包。

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/

接受的答案给了我节点0.10.36和npm 1.3.6,这是非常过时的。我从nodejs下载页面获取了最新的linux-x64 tarball,安装起来并不太难: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

现在将刚刚下载的tar文件压缩

tar xzf node.tgz

运行这个命令,然后将它添加到你的.bashrc:

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

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

更新npm(只有一次,不要添加到.bashrc):

npm install -g npm

注意-g的意思是全局的,对npm实例来说是全局的也就是我们刚刚安装的实例它仅限于当前用户。这将适用于npm“全局”安装的所有包。

和其他人一样,这个公认的答案也给了我一个过时的版本。

这里有另一种非常有效的方法:

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

您也可以替换16。X与另一个版本,如18。x, 14。x,等等。

您可以在NodeSource Github页面上看到所有可用的版本,如果需要,也可以从那里提取。

注意:根据您的环境,您可能需要使用sudo运行。

简单的NVM安装…

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

如果要安装某个版本的Node(如18.12.1),请将最后一行更改为

nvm install 18.12.1

有关如何使用NVM的更多信息,请访问文档: https://github.com/nvm-sh/nvm

对于v4 LTS版本使用:

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

对于Node.js v6的使用:

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

在尝试在Amazon Linux上安装本机插件时,我也遇到了一些问题。如果你想这样做,你也应该安装构建工具:

yum install gcc-c++ make

对我有用的程序(遵循这些相当旧的说明,并进行了一些更新):

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


注:

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

我偶然发现了这个。我尝试了一些比较流行的答案,但最后对我有用的是亚马逊的快速设置指南。

教程:在Amazon EC2实例上设置Node.js

本教程的要点是:

确保已通过ssh连接到实例。 获取nvm: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash 活跃。~ / .nvm / nvm.sh 使用nvm Install 4.4.5安装节点(注意:您可以选择不同的版本。首先运行$ nvm ls-remote检查远程版本) 最后,运行$ node -e "console.log(' running node .js' + process.version)"

希望这能帮助到下一个人。

sudo yum install nodejs npm——enablerepo=epel适用于Amazon Linux AMI。 Curl—silent—location https://rpm.nodesource.com/setup_6.x | bash - Yum -y安装nodejs 在红帽公司工作。

我有Node.js 6。我想安装Node.js 8.x。

下面是我使用的命令(来自Nodejs的站点,有一些额外的步骤来处理yum缓存的数据):

sudo yum remove nodejs:卸载Node.jsx(我不知道这是否有必要) Curl—silent—location https://rpm.nodesource.com/setup_8.x | sudo bash - 须藤yum清洁所有 sudo yum makecache:重新生成元数据缓存(这在文档中没有,但yum一直尝试安装Node。jx 6。x,不成功,直到我发出这最后两个命令) sudo yum install nodejs:安装Node.js 8.x

最简单的解决方案是这样的(以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

如官方文件所述,简单以下2步-

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

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安装nodejs

对于那些想要在Ansible中运行接受的答案而不需要进一步搜索的人,我在这里发布了这个任务,以方便和将来的参考。

接受答案推荐:https://stackoverflow.com/a/35165401/78935

Ansible任务等效

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

您可以通过重新安装已安装的包到当前版本来更新/安装节点,这可能会使我们在进行更新时避免大量错误。

这是由nvm使用下面的命令完成的。在这里,我已经将我的节点版本更新到8,并将所有可用的包重新安装到v8 !

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

它也可以在AWS Linux实例上工作。

EC2-Instance的官方文档: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)"

似乎没有人提到这一点。在Amazon Linux 2上,加载EPEL的官方方式是:

Sudo amazon-linux-extras安装epel

...那么你可以:

Sudo yum安装nodejs  

参见Extras Library (Amazon Linux 2)

我通常使用NVM在服务器上安装节点。它让我可以选择安装多个版本的nodejs。

命令如下:

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

然后检查是否正确安装:

command -v nvm

在那之后,运行这个来安装最新版本:

nvm install node 

or

nvm install 11 

正如其他人提到的,使用epel会得到一个非常过时的版本,下面是我刚刚写的一个小脚本,它可以添加到CI管道中,或者将其传递给ec2 user-data,以安装最新版本的node,只需将版本替换为您想要的版本,以及您正在使用的合适的Linux发行版。

下面以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

如果您想在同一个shell中测试它,只需运行即可

. ~/.profile

RHEL、CentOS、CloudLinux、Amazon Linux、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

2022年5月

我在这上面花了太长时间。我的亚马逊Linux 2配置,以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

2022年6月-系统真的很讨厌箱子里没有链接的东西。如果你需要其他用户可以访问的东西,这里有一个小更新。诚然,添加/etc/profile.d/npm.sh正是nvm建议的,但我发现它被高估了。我认为它可以被消去取代ln -s。快乐的黑客

#!/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/


正如亚马逊文档(在亚马逊EC2实例上设置Node.js)中所述,只需运行以下命令:

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

完成了!