简体   繁体   English

如何在Linux上安装节点二进制分发文件

[英]How to install node binary distribution files on Linux

My production server (Centos 5.9) won't compile nodejs, possibly because it's gcc is only 4.1.2 (4.2 or above is recommended) so I've trying to install the binaries. 我的生产服务器(Centos 5.9)不会编译nodejs,可能是因为它的gcc只有4.1.2(建议使用4.2或更高版本)所以我试图安装二进制文件。

$ wget http://nodejs.org/dist/v0.10.22/node-v0.10.22-linux-x86.tar.gz
$ tar -zxvf node-v0.10.22-linux-x86.tar.gz
$ cd node-v0.10.22-linux-x86
$ sudo cp bin/* /usr/local/bin
$ sudo cp -R lib/* /usr/local/lib
$ sudo cp -R share/* /usr/local/share

And now for testing: 现在进行测试:

$ node -v  # => v0.10.22
$ man node # looks fine
$ npm -v   # UH OH, PROBLEM - Cannot find module 'npmlog'

Now (keeping in mind I'm a complete beginner at node) I did some searching and found there's an environment variable called NODE_PATH, so I tried: 现在(记住我是节点的完全初学者)我做了一些搜索,发现有一个名为NODE_PATH的环境变量,所以我尝试了:

$ export NODE_PATH=/usr/local/lib/node_modules
$ npm -v   # SAME PROBLEM - Cannot find module 'npmlog'

So then I found out where npmlog lives and tried modifying NODE_PATH accordingly: 那么我找到了npmlog所在的位置并尝试相应地修改NODE_PATH:

$ find /usr/local/lib -name npmlog # => /usr/local/lib/node_modules/npm/node_modules/npmlog
$ export NODE_PATH=/usr/local/lib/node_modules/npm/node_modules
$ npm -v   # DIFFERENT PROBLEM - Can't find '../lib/npm.js'

At this stage, after more unhelpful googling, I decided I was in over my depth and decided to ask for help. 在这个阶段,经过更多无益的谷歌搜索,我决定我超越自己的深度,并决定寻求帮助。 Can anyone tell me what I'm doing wrong? 谁能告诉我我做错了什么?

It is much faster to do clean NPM reinstall which will remove "broken" links: 执行干净的NPM重新安装会更快,这将删除“损坏的”链接:

wget https://npmjs.org/install.sh

chmod +x install.sh
sudo ./install.sh

Then it will ask you to remove old NPM link 然后它会要求您删除旧的NPM链接

Using Node Version Manager 使用节点版本管理器

Use a Node version manager like nvm to handle installation and version management for you. 使用像nvm这样的Node版本管理器来为您处理安装和版本管理。 After you install nvm you can simply install any Node version, for example nvm install 8 . 安装nvm后,您只需安装任何Node版本,例如nvm install 8

But if you just want to install the binary yourself, see below: 但如果您只想自己安装二进制文件,请参阅以下内容:

Using apt-get 使用apt-get

In special cases where you need a system wide Node installation, you can use apt-get: 在需要系统范围节点安装的特殊情况下,您可以使用apt-get:

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

The above snippet will install the latest Node 8. 上面的代码片段将安装最新的Node 8。

Installing the Binary Manually 手动安装二进制文件

In order to install the binary manually, all you need to do is to download the binary and create a bunch of symbolic links. 为了手动安装二进制文件,您需要做的就是下载二进制文件并创建一堆符号链接。 Execute the commands below one after the other, and it should do the job. 一个接一个地执行下面的命令,它应该完成这项工作。 I have also written a shell script that does it for you if that is easier (see the bottom of the answer). 我还编写了一个shell脚本,如果这更容易,可以为你完成(请参阅答案的底部)。 Hope that helps. 希望有所帮助。

Make sure to use the correct download link for your OS architecture (ie either 32-bit or 64-bit) for wget on the second line. 确保在第二行使用适用于您的OS体系结构的正确下载链接(即32位或64位)。

ME=$(whoami) ; sudo chown -R $ME /usr/local && cd /usr/local/bin #adding yourself to the group to access /usr/local/bin
mkdir _node && cd $_ && wget https://nodejs.org/dist/v8.11.4/node-v8.11.4-linux-x64.tar.xz -O - | tar zxf - --strip-components=1
ln -s "/usr/local/bin/_node/bin/node" .. # Making the symbolic link to node
ln -s "/usr/local/bin/_node/lib/node_modules/npm/bin/npm-cli.js" ../npm ## making the symbolic link to npm

Here is a shell script that downloads and installs all the components. 这是一个下载并安装所有组件的shell脚本。 If you use this script to install Node, you can use the uninstall script to uninstall it. 如果使用此脚本安装Node,则可以使用卸载脚本将其卸载。

Installing Node 安装节点

#! /bin/bash
# run it by: bash install-node.sh
read -p " which version of Node do you need to install: for example 8.11.4 (or any other valid version): " VERSIONNAME
read -p " Are you using a 32-bit or 64-bit operating system ? Enter 64 or 32: " ARCHVALUE
if [[ $ARCHVALUE = 32 ]]
    then
    printf "user put in 32 \n"
    ARCHVALUE=86
    URL=https://nodejs.org/dist/v${VERSIONNAME}/node-v${VERSIONNAME}-linux-x${ARCHVALUE}.tar.gz

elif [[ $ARCHVALUE = 64 ]]
    then
    printf "user put in 64 \n"
    ARCHVALUE=64
    URL=https://nodejs.org/dist/v${VERSIONNAME}/node-v${VERSIONNAME}-linux-x${ARCHVALUE}.tar.gz

else
    printf "invalid input expted either 32 or 64 as input, quitting ... \n"

    exit
fi

# setting up the folders and the the symbolic links
printf $URL"\n"
ME=$(whoami) ; sudo chown -R $ME /usr/local && cd /usr/local/bin #adding yourself to the group to access /usr/local/bin
mkdir _node && cd $_ && wget $URL -O - | tar zxf - --strip-components=1 # downloads and unzips the content to _node
cp -r ./lib/node_modules/ /usr/local/lib/ # copy the node modules folder to the /lib/ folder
cp -r ./include/node /usr/local/include/ # copy the /include/node folder to /usr/local/include folder
mkdir /usr/local/man/man1 # create the man folder
cp ./share/man/man1/node.1 /usr/local/man/man1/ # copy the man file
cp bin/node /usr/local/bin/ # copy node to the bin folder
ln -s "/usr/local/lib/node_modules/npm/bin/npm-cli.js" ../npm ## making the symbolic link to npm

# print the version of node and npm
node -v
npm -v

Uninstalling Node 卸载节点

#! /bin/bash
# run it by: ./uninstall-node.sh
sudo rm -rf /usr/local/bin/npm
sudo rm -rf /usr/local/bin/node
sudo rm -rf /usr/local/lib/node_modules/
sudo rm -rf /usr/local/include/node/
sudo rm -rf /usr/local/share/man/man1/node.1
sudo rm -rf /usr/local/bin/_node/ 

I had a problem like that, but with iojs. 我有这样的问题,但有iojs。 However it should be the same procedure: 但它应该是相同的程序:

(Assuming that you've got a file matching node-v*-linux-x64.tar.gz in your current directory): (假设您在当前目录中有一个匹配node-v*-linux-x64.tar.gz的文件):

# In case of iojs you need to replace the occurrences of 'node' with 'iojs'
# Extract the downloaded archive with the linux-x64 version of node
tar zxf node-v*-linux-x64.tar.gz
# Move the extracted folder (./node-v*-linux-x64/) to /opt/
mv ./node-v*-linux-x64/ /opt/

To make the binary files available in your shell, create some softlinks inside the /usr/bin/ directory: 要在shell中使用二进制文件,请在/usr/bin/目录中创建一些软链接:

# Create a softlink to node in /usr/bin/
ln -s /opt/node-v*-linux-x64/bin/node /usr/bin/node
# Create a softlink to npm  in /usr/bin/
ln -s /opt/node-v*-linux-x64/bin/npm  /usr/bin/npm
# Create a softlink to iojs in /usr/bin (this step can be omitted if you're using node)
ln -s /opt/node-v*-linux-x64/bin/iojs /usr/bin/iojs

Notice: If you'd like to access the cli of some globally installed node modules (for example bower , typescript or coffee-script ), you're required to create a softlink to each of those executables in the /usr/bin/ directory. 注意:如果您想访问某些全局安装的节点模块的cli(例如bowertypescriptcoffee-script ),则需要为/usr/bin/目录中的每个可执行文件创建一个软链接。

Alternatively you could just add the bin directory of your node installation directory (eg /opt/node-v*-linux-x64/ ) to the PATH environment variable: (you should use the absolute path for this!) 或者,您可以将节点安装目录的bin目录(例如/opt/node-v*-linux-x64/ )添加到PATH环境变量中:(您应该使用绝对路径!)

# create a new .sh script in /etc/profile.d which adds the directory to PATH
echo "export PATH=$PATH:/opt/node-v0.12.3-linux-x64/bin" > /etc/profile.d/node-npm.sh

This change will take effect after logging out and in again. 此更改将在注销后再次生效。

Both methods worked for me (I use a linux desktop version of Ubuntu 14.04/15.04 with GNOME 3). 这两种方法都适合我(我使用的是Linux桌面版的Ubuntu 14.04 / 15.04和GNOME 3)。

I had the same issue reported here. 我在这里报道了同样的问题。 Fixed it by removing /usr/local/bin/npm and replacing it with a symlink to /usr/local/lib/node_modules/npm/bin/npm-cli.js 通过删除/usr/local/bin/npm并将其替换为/usr/local/lib/node_modules/npm/bin/npm-cli.js的符号链接来修复它

$ ls -l /usr/local/bin/
node
npm -> /usr/local/lib/node_modules/npm/bin/npm-cli.js


$ npm -v
1.3.17
wget <node archive url from nodejs.org>
cd /usr/local
sudo tar --strip-components 1 -xf <path to node archive>

You can run node and npm right away. 您可以立即运行node和npm。

It used to be documented in the README inside the archive in older versions. 它曾经在旧版本的存档内的README中记录。

I had the same problem and I was able to resolve it by creating symlinks instead of copying the binaries. 我有同样的问题,我能够通过创建符号链接而不是复制二进制文件来解决它。

$ cd /usr/local/src
$ wget http://nodejs.org/dist/v0.10.24/node-v0.10.24-linux-x64.tar.gz
$ tar -zxvf node-v0.10.24-linux-x64.tar.gz
$ cd node-v0.10.24-linux-x64
$ sudo cp -R lib/* /usr/local/lib
$ sudo cp -R share/* /usr/local/share
$ ln -s /usr/local/src/node-v0.10.24-linux-x64/bin/node /usr/local/bin/node
$ ln -s /usr/local/src/node-v0.10.24-linux-x64/bin/npm /usr/local/bin/npm
$ node -v
v0.10.24
$ npm -v
1.3.21

I tend to use nave to install the binaries. 我倾向于使用nave来安装二进制文件。 Use wget to download the nave.sh file and then us it to install node. 使用wget下载nave.sh文件,然后使用它来安装节点。 Nave is also nice to have around in case one of your production apps requires a different version of node than what's installed globally. 如果您的一个生产应用程序需要不同于全局安装的节点版本,Nave也很高兴。

$ wget https://raw.github.com/isaacs/nave/master/nave.sh
$ sudo bash nave.sh usemain 0.10.22

You can use GNU stow to make symbolic links of those binaries in /usr/local properly with one command. 您可以使用GNU stow通过一个命令正确地在/ usr / local中创建这些二进制文件的符号链接。 Stow also allows you to easily remove Node js from /usr/local at a later time and swap multiple versions of Node js. Stow还允许您稍后从/ usr / local轻松删除Node js并交换多个版本的Node js。

$ # first, install stow
$ mkdir /usr/local/stow # if it doesn't exist
$ # then, place software binary package in /usr/local/stow
$ cd /usr/local/stow
$ stow <package_name>  # install / add sym links
$ source $HOME/.bash_profile  # reload your environment
$ # node -v and npm -v should now work
$ stow -D <package_name> # uninstall / remove sym links

These steps worked for me with node-v0.10.17-linux-x64. 这些步骤适用于node-v0.10.17-linux-x64。

In the man page of cp in Mac OS X: 在Mac OS X中的cp手册页中:

Symbolic links are always followed unless the -R flag is set, in which case symbolic links are not followed, by default. 除非设置-R标志,否则始终遵循符号链接,在这种情况下,默认情况下不遵循符号链接。

When you execute sudo cp bin/* /usr/local/bin , the symbolic link bin/npm is followed. 执行sudo cp bin/* /usr/local/bin ,将遵循符号链接bin/npm

Actually, bin/npm is linked to ../lib/node_modules/npm/bin/npm-cli.js , so cp will copy npm-cli.js to /usr/local/bin . 实际上, bin/npm链接到../lib/node_modules/npm/bin/npm-cli.js ,因此cp会将npm-cli.js复制到/usr/local/bin That's why you get an error. 这就是你得到错误的原因。

I had the same problem. 我有同样的问题。

The problem is the npm excutable in /usr/local/bin. 问题是/ usr / local / bin中的npm excutable。

The way I solved it was: 我解决它的方式是:

sudo rm /usr/local/bin/npm

sudo ln -s "/usr/local/lib/node_modules/npm/bin/npm-cli.js" /usr/local/bin/npm

In Ubuntu there is a .bashrc file which sets path to binaries. 在Ubuntu中有一个.bashrc文件,它设置二进制文件的路径。

By default, there is path set for bin in home directory. 默认情况下,主目录中为bin设置了路径。 Perhaps you can create bin directory in your home directory and move the binaries there. 也许您可以在主目录中创建bin目录并在那里移动二进制文件。 Reboot your system and try executing the command node 重新启动系统并尝试执行命令node

I faced the same problem. 我遇到了同样的问题。 So, I symlinked node and npm from ./bin/ to /usr/local/bin 所以,我从符号链接 节点 ,NPM ./bin//usr/local/bin

If someone is interested in using Docker, in the Dockerfile, 如果有人对使用Docker感兴趣,请在Dockerfile中

ENV NODE_VERSION 8.10.0
RUN wget https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz
RUN tar -xJvf node-v$NODE_VERSION-linux-x64.tar.xz -C /usr/local/
ENV NODEJS_HOME /usr/local/node-v$NODE_VERSION-linux-x64
ENV PATH $NODEJS_HOME/bin:$PATH

RUN node --version
RUN npm --version

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM