如何在 Linux 上安装节点二进制分发文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20028996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install node binary distribution files on Linux
提问by sanichi
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 的完全初学者)我进行了一些搜索,发现有一个名为 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?
在这个阶段,经过更多无用的谷歌搜索后,我决定我的深度并决定寻求帮助。谁能告诉我我做错了什么?
回答by sanichi
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. Perhaps you can create bindirectory in your home directoryand move the binaries there. Reboot your system and try executing the command node
默认情况下,bin
在主目录中设置了路径。也许您可以在您的主目录中创建bin目录并将二进制文件移动到那里。重新启动系统并尝试执行命令node
回答by BadCanyon
I tend to use naveto install the binaries. Use wget to download the nave.sh file and then us it to install node. 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 下载 nave.sh 文件,然后使用它安装 node。如果您的生产应用程序需要与全局安装的节点版本不同的节点版本,那么 Nave 也很适合使用。
$ wget https://raw.github.com/isaacs/nave/master/nave.sh
$ sudo bash nave.sh usemain 0.10.22
回答by barbazul
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
回答by AJ Meyghani
Using Node Version Manager
使用节点版本管理器
Use a Node version manager like nvmto handle installation and version management for you. After you install nvm
you can simply install any Node version, for example nvm install 8
.
使用像nvm这样的 Node 版本管理器来为您处理安装和版本管理。安装后,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:
在需要系统范围的 Node 安装的特殊情况下,您可以使用 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). Hope that helps.
为了手动安装二进制文件,您需要做的就是下载二进制文件并创建一堆符号链接。一个接一个地执行下面的命令,它应该可以完成这项工作。我还编写了一个 shell 脚本,如果这更容易的话,它会为您完成(请参阅答案的底部)。希望有帮助。
Make sure to use the correct download link for your OS architecture (i.e. either 32-bit or 64-bit) for wget on the second line.
确保为第二行的 wget 使用适用于您的操作系统架构(即 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. If you use this script to install Node, you can use the uninstall script to uninstall it.
这是一个下载并安装所有组件的 shell 脚本。如果您使用此脚本安装 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/
回答by konyak
You can use GNU stow to make symbolic links of those binaries in /usr/local properly with one command. Stow also allows you to easily remove Node js from /usr/local at a later time and swap multiple versions of Node js.
您可以使用 GNU stow 通过一个命令正确地在 /usr/local 中创建这些二进制文件的符号链接。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。
回答by Miaonster
In the man page of cp
in Mac OS X:
在cp
Mac OS X的手册页中:
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
. That's why you get an error.
实际上,bin/npm
链接到../lib/node_modules/npm/bin/npm-cli.js
,因此cp
将复制npm-cli.js
到/usr/local/bin
。这就是为什么你会得到一个错误。
回答by Anthony Akentiev
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 链接
回答by RegisteredUser
I had a problem like that, but with iojs. However it should be the same procedure:
我遇到了这样的问题,但是使用 iojs。但是它应该是相同的过程:
(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.
注意:如果您想访问某些全局安装的节点模块(例如bower
、typescript
或coffee-script
)的 cli,则需要为/usr/bin/
目录中的每个可执行文件创建一个软链接。
Alternatively you could just add the bin directory of your node installation directory (e.g. /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).
这两种方法都对我有用(我使用带有 GNOME 3 的 Ubuntu 14.04/15.04 的 linux 桌面版本)。
回答by TomJShore
I had the same problem.
我有同样的问题。
The problem is the npm excutable in /usr/local/bin.
问题是 /usr/local/bin 中的 npm 可执行文件。
The way I solved it was:
我解决的方法是:
sudo rm /usr/local/bin/npm
sudo rm /usr/local/bin/npm
sudo ln -s "/usr/local/lib/node_modules/npm/bin/npm-cli.js" /usr/local/bin/npm
sudo ln -s "/usr/local/lib/node_modules/npm/bin/npm-cli.js" /usr/local/bin/npm
回答by Peter Dotchev
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.
它曾经记录在旧版本存档内的自述文件中。