[英]NVM & Node.js - Recommended install for all users
is there a recommended install for nvm so all users can use it?是否有 nvm 的推荐安装以便所有用户都可以使用它? i cannot find anything on the web regarding this.
我在 web 上找不到与此相关的任何信息。
this is what i did这就是我所做的
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谢谢
Here is what I did:这是我所做的:
Installed nvm
in /opt/nvm
as root.以 root 身份在
/opt/nvm
中安装nvm
。 Seemed like an appropriate location.似乎是一个合适的位置。
# git clone git@github.com:creationix/nvm.git /opt/nvm
Created the directory /usr/local/nvm
.创建目录
/usr/local/nvm
。 This is where the downloads will go ( $NVM_DIR
)这是下载的地方(
$NVM_DIR
)
# mkdir /usr/local/nvm
Create the directory /usr/local/node
.创建目录
/usr/local/node
。 This is where the NPM global stuff will go:这就是 NPM 全局内容的去向:
# mkdir /usr/local/node
Created a file called nvm.sh
in /etc/profile.d
with the following contents:在
/etc/profile.d
中创建了一个名为nvm.sh
的文件,其内容如下:
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"
Re-login to a shell session, then set the default node version.重新登录到 shell 会话,然后设置默认节点版本。
# nvm install 0.10 # nvm alias default 0.10
The node binaries should now be in the PATH
for all users the next time you login to a shell session.下次登录 shell 会话时,节点二进制文件现在应该在所有用户的
PATH
中。 NPM will install global things to the /usr/local/node
prefix. NPM 会将全局的东西安装到
/usr/local/node
前缀。
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):
为此,请运行以下命令(在提示符处输入您的用户密码):
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 使用 vXX.XX.XX 将您用户的节点切换到您想要的版本,然后重新运行上面的第一个命令将其复制到系统目录。
sudo -s
sudo -s
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | NVM_DIR=/usr/local/nvm bash
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | NVM_DIR=/usr/local/nvm bash
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | NVM_DIR=/usr/local/nvm bash
nvm.sh
in /etc/profile.d
with 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
/etc/profile.d
中创建了一个名为nvm.sh
的文件,其内容如下: #!/usr/bin/env bash export NVM_DIR="/usr/local/nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
#!/usr/bin/env bash export NVM_DIR="/usr/local/nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
/etc/profile.d/nvm.sh
/etc/profile.d/nvm.sh
nvm install node
nvm install node
npm install -g npm
npm install -g npm
Install NVM on your Linux server, after that install node version using NVM (run all the command as root user).在 Linux 服务器上安装 NVM,然后使用 NVM 安装节点版本(以 root 用户身份运行所有命令)。 After that run the below command for all the users get nodejs available with nvm
之后,为所有用户运行以下命令,获得可与 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/bin/node
Switch the user name check your node version.切换用户名检查你的节点版本。
su - username
which node
/usr/local/bin/node
Since LJHarb recommends not installing this globally, I decided to create a script to install nvm when you login to the server.由于 LJHarb 建议不要在全局范围内安装它,因此我决定在您登录服务器时创建一个脚本来安装 nvm。 I needed this as I had several users setup that may login, but needed access to pm2 (to monitor one of our applications).
我需要这个,因为我有几个可以登录的用户设置,但需要访问 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'
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.更新:我尝试了各种方法来使用 xtuple 的 nvm 和 n 来管理全局节点环境,但我总是遇到有问题的边缘情况。 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.
最后,最适合我的是从他们的网站下载几个版本的 node 并将它们解压缩到 /usr/local。 Then update my path with the version I want.
然后用我想要的版本更新我的路径。 eg
例如
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 或指定一个用户来管理它。
I had a small VPS with two users, wanted to avoid installing multiple copies of node due to limited space我有一个有两个用户的小型 VPS,由于空间有限,想避免安装多个节点副本
Simply moved $HOME/.nvm
of user that had NVM to /opt/nvm
and chmod -R 777 /opt/nvm
so writable by all users只需将具有 NVM 的用户的
$HOME/.nvm
移动到/opt/nvm
和chmod -R 777 /opt/nvm
被所有用户写入
Then set each user's NVM_HOME
in .bashrc
to /opt/nvm
然后将
.bashrc
中每个用户的NVM_HOME
设置为/opt/nvm
Seemed like most obvious solution to me, since one user had a pre-existing .nvm folder对我来说似乎是最明显的解决方案,因为一个用户有一个预先存在的 .nvm 文件夹
Re: ugly permissions, this copy will only be used for development purposes - I would def recommend anyone using node in production not consider this method回复:丑陋的权限,此副本仅用于开发目的 - 我建议任何在生产中使用节点的人不要考虑这种方法
I found a cleaner way, and this way you can probably have multiple versions with tweaks, I haven't tried it.我找到了一种更简洁的方法,这样你可能可以有多个带有调整的版本,我还没有尝试过。
Login to your root account and follow these steps.登录到您的 root 帐户并按照以下步骤操作。
nvm install 14
n=$(which node);
n=${n%/bin/node};cd $n;cd ..;
ls
node_v=the_version_you_picked
mv $node_v /usr/local/node/$node_v;cd /usr/local/node/$node_v;chmod -R 755 ./
echo "PATH=\${PATH}:/usr/local/node/$node_v/bin;export PATH" > /etc/profile.d/gloabl_node.sh
and exit and re-login, now run which node
or node -v
to confirm it's working.并退出并重新登录,现在运行
which node
或node -v
以确认它正在工作。
Now you can rename the bin files and all the other configs it needs to run multiple versions.现在您可以重命名 bin 文件和运行多个版本所需的所有其他配置。 eg: rename
node
-> node-14
and so on.例如:重命名
node
-> node-14
等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.