为什么nvm命令以root用户身份安装,并且在vagrant bootstrap.sh期间也找不到?

时间:2016-07-24 18:47:45

标签: node.js bash vagrant nvm

当我尝试安装nvm并测试它是否安装在我的vagrant shell配置脚本中时,使用:

sudo wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash
source ~/.bashrc
nvm

我明白了:

==> default: => Downloading nvm as script to '/root/.nvm'
==> default: => Appending source string to /root/.bashrc
==> default: => You can now install Node.js by running `nvm install`
==> default: /tmp/vagrant-shell: line 7: nvm: command not found

首先,我不希望它以root身份安装,我想将其安装为/ home / vagrant中的vagrant用户,以避免权限问题。

第二个为什么在我按照https://github.com/creationix/nvm中的nvm安装说明中的说明运行source .bashrc时找不到命令?

我现在有一个解决方案。

1 个答案:

答案 0 :(得分:5)

首先,如果你想在bootstrap.sh脚本中安装somethnig作为vagrant用户,最简单的方法是将另一个配置脚本添加到以非特权模式运行的Vagrantfile中:

config.vm.provision "ScriptRunAsRoot", type:"shell", path: "Vagrantdata/rootUserBootstrap.sh"
config.vm.provision "ScriptRunAsVagrantUser", privileged: false, type:"shell", path: "Vagrantdata/vagrantUserBootstrap.sh"

然后在这种情况下获取nvm命令,你必须确保直接运行nvm.sh脚本,即不通过nvm安装说明中所述的.bashrc文件。这是因为.bashrc有一条线,如果它在非交互式shell中运行,它会提前退出,我想这就是流浪汉。来自.bashrc:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

因此,您需要代替source ~/.bashrc

source ~/.nvm/nvm.sh