NVM 和 Node.js - 建议所有用户安装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11542846/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:02:49  来源:igfitidea点击:

NVM & Node.js - Recommended install for all users

node.jsnvm

提问by jadent

is there a recommended install for nvm so all users can use it? i cannot find anything on the web regarding this.

是否有推荐的 nvm 安装以便所有用户都可以使用它?我在网上找不到任何关于此的信息。

this is what i did

这就是我所做的

  • installed nvm in a common directory
  • put the nvm.sh script locationin .profile for all users
  • created a nvm/alias directory (nvm complains if this is not here for other users)
  • 在公共目录中安装 nvm
  • 将所有用户的 nvm.sh 脚本放在 .profile 中
  • 创建了一个 nvm/alias 目录(如果其他用户不在这里,nvm 会抱怨)

then each user must either run "nvm use " or put it in their profile by default

那么每个用户必须要么运行“nvm use”,要么默认将其放入他们的配置文件中

not sure if there is a better way?

不确定是否有更好的方法?

thanks

谢谢

回答by Tim Smart

Here is what I did:

这是我所做的:

  1. Installed nvmin /opt/nvmas root. Seemed like an appropriate location.

    # git clone [email protected]:creationix/nvm.git /opt/nvm
    
  2. Created the directory /usr/local/nvm. This is where the downloads will go ($NVM_DIR)

    # mkdir /usr/local/nvm
    
  3. Create the directory /usr/local/node. This is where the NPM global stuff will go:

    # mkdir /usr/local/node
    
  4. Created a file called nvm.shin /etc/profile.dwith the following contents:

    export NVM_DIR=/usr/local/nvm
    source /opt/nvm/nvm.sh
    
    export NPM_CONFIG_PREFIX=/usr/local/node
    export PATH="/usr/local/node/bin:$PATH"
    
  5. Re-login to a shell session, then set the default node version.

    # nvm install 0.10
    # nvm alias default 0.10
    
  1. 安装nvm/opt/nvm作为根。似乎是一个合适的位置。

    # git clone [email protected]:creationix/nvm.git /opt/nvm
    
  2. 创建了目录/usr/local/nvm。这是下载的地方 ( $NVM_DIR)

    # mkdir /usr/local/nvm
    
  3. 创建目录/usr/local/node。这是 NPM 全局内容的去向:

    # mkdir /usr/local/node
    
  4. 创建了一个名为文件nvm.sh/etc/profile.d有如下内容:

    export NVM_DIR=/usr/local/nvm
    source /opt/nvm/nvm.sh
    
    export NPM_CONFIG_PREFIX=/usr/local/node
    export PATH="/usr/local/node/bin:$PATH"
    
  5. 重新登录到 shell 会话,然后设置默认节点版本。

    # nvm install 0.10
    # nvm alias default 0.10
    

The node binaries should now be in the PATHfor all users the next time you login to a shell session. NPM will install global things to the /usr/local/nodeprefix.

节点二进制文件现在应该在PATH您下次登录 shell 会话时为所有用户提供。NPM 将全局事物安装到/usr/local/node前缀。

回答by Glowin

It's best to install one copy of node globally so that other users can access it. To do this, run the following command (entering your user's password at the prompt):

最好在全局安装一个 node 副本,以便其他用户可以访问它。为此,请运行以下命令(在提示符下输入您的用户密码):

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

This commend is copying whatever version of node you have active via nvm into the /usr/local/directory and setting the permissions so that all users can access them.

这个命令是将您通过 nvm 激活的任何版本的节点复制到/usr/local/目录中并设置权限,以便所有用户都可以访问它们。

To check that it works, become the root user and do another which command to make sure that node is now installed to /usr/local/bin:

要检查它是否有效,请成为 root 用户并执行另一个 which 命令以确保该节点现在已安装到/usr/local/bin

sudo -s
which node

If you ever want to change the version of node that's installed system wide, just do another nvm use vXX.XX.XX to switch your user's node to the version you want, and then re-run the first command above to copy it to the system directory.

如果您想更改系统范围内安装的节点版本,只需执行另一个 nvm use vXX.XX.XX 将您用户的节点切换到您想要的版本,然后重新运行上面的第一个命令将其复制到系统目录。

回答by Biscar

  1. Login as root: sudo -s
  2. Install nvm: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | NVM_DIR=/usr/local/nvm bash
  3. Created a file called nvm.shin /etc/profile.dwith the following contents: #!/usr/bin/env bash export NVM_DIR="/usr/local/nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
  4. Run /etc/profile.d/nvm.sh
  5. Install node: nvm install node
  6. Optionally update npm with: npm install -g npm
  1. 以 root 身份登录: sudo -s
  2. 安装 nvm: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | NVM_DIR=/usr/local/nvm bash
  3. 创建了一个名为文件nvm.sh/etc/profile.d有如下内容: #!/usr/bin/env bash export NVM_DIR="/usr/local/nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
  4. /etc/profile.d/nvm.sh
  5. 安装节点: nvm install node
  6. 可以选择使用以下命令更新 npm: npm install -g npm

回答by matth

Since LJHarb recommends not installing this globally, I decided to create a script to install nvm when you login to the server. I needed this as I had several users setup that maylogin, but needed access to pm2 (to monitor one of our applications).

由于 LJHarb 建议不要全局安装它,因此我决定创建一个脚本来在您登录服务器时安装 nvm。我需要这个,因为我有几个可以登录的用户设置,但需要访问 pm2(以监视我们的应用程序之一)。

Create the script in /etc/profile.d/ (named nvm.sh for example):

在 /etc/profile.d/ 中创建脚本(例如名为 nvm.sh):

#!/bin/bash
NODE_VER=6.2.2
if [ ! -f ~/.nvm/nvm.sh ]; then
    # May need to be updated with the latest nvm release
    wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash
fi
source ~/.nvm/nvm.sh
if ! command -v node | grep -q $NODE_VER; then
    echo "Node is not installed"
    nvm install $NODE_VER
    nvm alias default $NODE_VER
fi

For our application, we needed pm2 shared between users:

对于我们的应用程序,我们需要在用户之间共享 pm2:

if ! command -v pm2 &>/dev/null; then
    echo "pm2 not installed"
    npm install -g pm2
fi
# Share pm2 configuration between users
alias pm2='env HOME=/opt/sora pm2'

回答by bibincatchme

Install NVM on your Linux server, after that install node version using NVM (run all the command as root user). After that run the below command for all the users get nodejs available with nvm

在 Linux 服务器上安装 NVM,然后使用 NVM 安装节点版本(以 root 用户身份运行所有命令)。之后,为所有用户运行以下命令,使用 nvm 获取 nodejs

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

The above command is a bit complicated, but all it's doing is copying whatever version of node you have active via nvm into the /usr/local/ directory (where user installed global files should live on a linux VPS/server) and setting the permissions so that all users can access them.

上面的命令有点复杂,但它所做的只是将您通过 nvm 激活的任何版本的节点复制到 /usr/local/ 目录(用户安装的全局文件应位于 linux VPS/服务器上)并设置权限以便所有用户都可以访问它们。

/root/.nvm/versions/node/v8.10.0/bin/node

/root/.nvm/versions/node/v8.10.0/斌/节点

Switch the user name check your node version.

切换用户名检查你的节点版本。

su - username
which node
/usr/local/bin/node

回答by Brendon

There is also this fork of nvm designed for global usage: https://github.com/xtuple/nvm

还有一个专为全球使用而设计的 nvm 分支:https: //github.com/xtuple/nvm

wget -qO- https://raw.githubusercontent.com/xtuple/nvm/master/install.sh | sudo bash

sudo chown -R $USER /usr/local/nvm

nvm install 8

Update : I tried various ways to use xtuple's nvm and also n to manage a global node environment and I always ran into edge cases where there were issues. In the end what worked best for me was to download a few versions of node from their website and uncompress them to /usr/local. Then update my path with the version I want. e.g.

更新:我尝试了各种方法来使用 xtuple 的 nvm 和 n 来管理全局节点环境,但我总是遇到出现问题的边缘情况。最后,对我来说最有效的是从他们的网站下载几个版本的 node 并将它们解压缩到 /usr/local。然后用我想要的版本更新我的路径。例如

export PATH=/usr/local/node-v7.10.1-linux-x64/bin:$PATH

Note: You will probably have to chmod 777 the node path or dedicate one user to mange it.

注意:您可能需要 chmod 777 节点路径或指定一个用户来管理它。