简体   繁体   English

在 OS X 上安装 brew、node.js、io.js、nvm、npm 的建议方法是什么?

[英]What is the suggested way to install brew, node.js, io.js, nvm, npm on OS X?

I am trying to use homebrew as much as possible.我正在尝试尽可能多地使用自制软件。 What's the suggested way to install the following on OS X?在 OS X 上安装以下内容的建议方法是什么?

and hopefully supports development for:并希望支持以下方面的开发:

  1. Using homebrew install nvm :使用homebrew安装nvm

     brew update brew install nvm source $(brew --prefix nvm)/nvm.sh

    Add the last command to the .profile , .bashrc or .zshrc file to not run it again on every terminal start.将最后一个命令添加到.profile.bashrc.zshrc文件中,以免在每次终端启动时再次运行它。 So for example to add it to the .profile run:因此,例如将其添加到.profile运行:

     echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.profile

    If you have trouble with installing nvm using brew you can install it manually (see here )如果您在使用brew安装nvm遇到问题,您可以手动安装它(请参阅此处

  2. Using nvm install node or iojs (you can install any version you want):使用nvm install nodeiojs (你可以安装任何你想要的版本):

     nvm install 0.10 # or nvm install iojs-1.2.0
  3. npm is shipping with node (or iojs ), so it will be available after installing node (or iojs ). npmnode (或iojs )一起提供,因此在安装node (或iojs )后它将可用。 You may want to upgrade it to the latest version:您可能希望将其升级到最新版本:

     $ npm install -g npm@latest

    UPD Previous version was npm update -g npm . UPD以前的版本是npm update -g npm Thanks to @Metallica for pointing to the correct way (look at the comment bellow).感谢@Metallica 指出正确的方法(查看下面的评论)。

  4. Using npm install ionic :使用npm install ionic

     npm install -g ionic
  5. What about ngCordova : you can install it using npm or bower . ngCordova怎么ngCordova :你可以使用npmbower安装它。 I don't know what variant is more fit for you, it depends on the package manager you want to use for the client side.我不知道哪种变体更适合您,这取决于您要用于客户端的包管理器。 So I'll describe them both:所以我将描述它们:

    1. Using npm : Go to your project folder and install ng-cordova in it:使用npm :转到您的项目文件夹并在其中安装ng-cordova

       npm install --save ng-cordova
    2. Using bower : Install bower:使用bower :安装凉亭:

       npm install -g bower

      And then go to your project folder and install ngCordova in it:然后转到您的项目文件夹并在其中安装ngCordova

       bower install --save ngCordova

PS聚苯乙烯

  1. Some commands may require superuser privilege某些命令可能需要超级用户权限
  2. Short variant of npm install some_module is npm i some_module npm install some_module简短变体是npm i some_module

2019 update: Use NVM to install node, not Homebrew 2019 更新:使用 NVM 安装节点,而不是 Homebrew

In most of the answers , recommended way to install nvm is to use Homebrew在大多数答案中,推荐的安装 nvm 的方法是使用Homebrew

Do not do that不要那样做

At Github Page for nvm it is clearly called out:在 nvm 的Github 页面上,它清楚地标明:

Homebrew installation is not supported.不支持 Homebrew 安装。 If you have issues with homebrew-installed nvm, please brew uninstall it, and install it using the instructions below, before filing an issue.如果您对 homebrew 安装的 nvm 有问题,请在提交问题之前 brew uninstall 它,并使用下面的说明安装它。

Use the following method instead改用下面的方法

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash

The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).该脚本将 nvm 存储库克隆到 ~/.nvm 并将源代码行添加到您的配置文件(~/.bash_profile、~/.zshrc、~/.profile 或 ~/.bashrc)。

And then use nvm to install node.然后使用 nvm 安装节点。 For example to install latest LTS version do:例如要安装最新的 LTS 版本,请执行以下操作:

nvm install v8.11.1

Clean and hassle free.干净又省心。 It would mark this as your default node version as well so you should be all set它也会将此标记为您的默认节点版本,因此您应该全部设置

I'm using n (Node version management)我正在使用 n(节点版本管理)

You can install it in two ways您可以通过两种方式安装它

brew install n

or或者

npm install -g n

You can switch between different version of node and io.您可以在不同版本的 node 和 io 之间切换。 Here's an example from my current env when I call n without params:这是我当前环境中的一个示例,当我在没有参数的情况下调用 n 时:

$ n

  io/3.3.1
  node/0.12.7
  node/4.0.0
  node/5.0.0
ο node/5.10.1 

I'm super late to this but I didn't like the other answers我太晚了,但我不喜欢其他答案

Installing Homebrew安装 Homebrew

For brew run对于 brew运行

"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing node & npm安装节点和 npm

You SHOULD NOT use brew to install node and npm .不应该使用brew来安装nodenpm

I've seen a few places suggested that you should use Homebrew to install Node (like alexpods answer and in this Team Treehouse blog Post ) but installing this way you're more prone to run into issues as npm and brew are both package managers and you should have a package manager manage another package manager this leads to problems, like this bug offical npm issues Error: Refusing to delete: /usr/local/bin/npm or this Can't uninstall npm module on OSX我已经看到一些地方建议您应该使用 Homebrew 来安装 Node(如 alexpods 的回答和这篇Team Treehouse 博客文章),但以这种方式安装您更容易遇到问题,因为npmbrew都是包管理器和你应该有一个包管理器来管理另一个包管理器,这会导致问题,比如这个 bug 官方npm问题错误:拒绝删除:/usr/local/bin/npm无法在 OSX 上卸载 npm 模块

You can read more on the topic in DanHerbert's post Fixing npm On Mac OS X for Homebrew Users , where he goes on to say您可以在 DanHerbert 的文章Fixing npm On Mac OS X for Homebrew Users 中阅读有关该主题的更多信息,他继续说

Also, using the Homebrew installation of npm will require you to use sudo when installing global packages.此外,使用 npm 的 Homebrew 安装将要求您在安装全局包时使用 sudo。 Since one of the core ideas behind Homebrew is that apps can be installed without giving them root access, this is a bad idea.由于 Homebrew 背后的核心思想之一是可以在不授予 root 访问权限的情况下安装应用程序,因此这是一个坏主意。

For Everything else对于其他一切

I'd use npm;我会使用 npm; but you really should just follow the install instruction for each modules following the directions on there website as they will be more aware of any issue or bug they have than anyone else但是您真的应该按照网站上的说明按照每个模块的安装说明进行操作,因为他们会比其他任何人都更了解他们遇到的任何问题或错误

If you have previously installed node using brew, then you will have a bunch of extra files that you should clean up before installing node "the right way".如果您之前使用 brew 安装了 node,那么您将有一堆额外的文件,您应该在“以正确的方式”安装 node 之前清理这些文件。 Plus, I had to add a few settings to my startup script to make things work smoothly.另外,我必须在我的启动脚本中添加一些设置才能使事情顺利进行。

I wrote a script to make this easy.我写了一个脚本来简化这个过程。

# filename:  install-nvm-npm-node
# author:    Lex Sheehan
# purpose:   To cleanly install NVM, NODE and NPM
# dependencies:  brew

NOW=$(date +%x\ %H:%M:%S)
CR=$'\n'
REV=$(tput rev)
OFF=$(tput sgr0)
BACKUP_DIR=$HOME/backups/nvm-npm-bower-caches/$NOW
MY_NAME=$(basename $0)
NODE_VER_TO_INSTALL=$1
if [ "$NODE_VER_TO_INSTALL" == "" ]; then
    NODE_VER_TO_INSTALL=v0.12.2
fi
if [ "`echo "$NODE_VER_TO_INSTALL" | cut -c1-1`" != "v" ]; then
    echo """$CR""Usage:   $ $MY_NAME <NODE_VERSION_TO_INSALL>"
    echo "Example: $ $MY_NAME v0.12.1"
    echo "Example: $ $MY_NAME $CR"
    exit 1
fi
echo """$CR""First, run:  $ brew update"
echo "Likely, you'll need to do what it suggests."
echo "Likely, you'll need to run: $ brew update$CR"
echo "To install latest node version, run the following command to get the latest version:  $ nvm ls-remote"
echo "... and pass the version number you want as the only param to $MY_NAME. $CR"
echo "Are you ready to install the latest version of nvm and npm and node version $NODE_VER_TO_INSTALL ?$CR"
echo "Press CTL+C to exit --or-- Enter to continue..."
read x

echo """$REV""Uninstalling nvm...$CR$OFF"
# Making backups, but in all likelyhood you'll just reinstall them (and won't need these backups)
if [ ! -d "$BACKUP_DIR" ]; then 
    echo "Creating directory to store $HOME/.nvm .npm and .bower cache backups: $BACKUP_DIR"
    mkdir -p $BACKUP_DIR
fi 
set -x
mv $HOME/.nvm   $BACKUP_DIR  2>/dev/null
mv $HOME/.npm   $BACKUP_DIR  2>/dev/null
mv $HOME/.bower $BACKUP_DIR  2>/dev/null
{ set +x; } &>/dev/null

echo "$REV""$CR""Uninstalling node...$CR$OFF"
echo "Enter your password to remove user some node-related /usr/local directories"
set -x
sudo rm -rf /usr/local/lib/node_modules
rm -rf /usr/local/lib/node
rm -rf /usr/local/include/node
rm -rf /usr/local/include/node_modules
rm /usr/local/bin/npm
rm /usr/local/lib/dtrace/node.d
rm -rf $HOME/.node
rm -rf $HOME/.node-gyp
rm /opt/local/bin/node
rm /opt/local/include/node
rm -rf /opt/local/lib/node_modules
rm -rf /usr/local/Cellar/nvm
brew uninstall node 2>/dev/null
{ set +x; } &>/dev/null

echo "$REV""$CR""Installing nvm...$CR$OFF"

echo "++brew install nvm"
brew install nvm 
echo '$(brew --prefix nvm)/nvm.sh'
source $(brew --prefix nvm)/nvm.sh

echo "$REV""$CR""Insert the following line in your startup script (ex: $HOME/.bashrc):$CR$OFF"
echo "export NVM_DIR=\"\$(brew --prefix nvm)\"; [ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\"$CR"
NVM_DIR="$(brew --prefix nvm)"

echo """$CR""Using nvm install node...$CR"
echo "++ nvm install $NODE_VER_TO_INSTALL"
nvm install $NODE_VER_TO_INSTALL
NODE_BINARY_PATH="`find /usr/local/Cellar/nvm -name node -type d|head -n 1`/$NODE_VER_TO_INSTALL/bin"
echo "$REV""$CR""Insert the following line in your startup script (ex: $HOME/.bashrc) and then restart your shell:$CR$OFF"
echo "export PATH=\$PATH:$NODE_BINARY_PATH:$HOME/.node/bin"

echo """$CR""Upgrading npm...$CR"
echo '++ install -g npm@latest'
npm install -g npm@latest
{ set +x; } &>/dev/null
echo "$REV""$CR""Insert following line in your $HOME/.npmrc file:$OFF"
echo """$CR""prefix=$HOME/.node$CR"
echo "Now, all is likley well if you can run the following without errors:  npm install -g grunt-cli$CR"
echo "Other recommended global installs: bower, gulp, yo, node-inspector$CR"

I wrote a short article here that details why this is "the right way".在这里写了一篇简短的文章,详细说明了为什么这是“正确的方法”。

If you need to install iojs, do so using nvm like this:如果您需要安装 iojs,请像这样使用 nvm:

nvm install iojs-v1.7.1

To install brew, just see its home page .要安装 brew,只需查看其主页

See alexpods answer for the rest.其余的请参阅 alexpods 答案。

You should install node.js with nvm, because that way you do not have to provide superuser privileges when installing global packages (you can simply execute "npm install -g packagename" without prepending 'sudo').你应该使用 nvm 安装 node.js,因为这样你在安装全局包时不必提供超级用户权限(你可以简单地执行“npm install -g packagename”而无需在前面加上“sudo”)。

Brew is fantastic for other things, however.然而,Brew 在其他方面也很棒。 I tend to be biased towards Bower whenever I have the option to install something with Bower.每当我可以选择使用 Bower 安装某些东西时,我都会倾向于使用 Bower。

Here's what I do:这是我所做的:

curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash
cd / && . ~/.nvm/nvm.sh && nvm install 0.10.35
. ~/.nvm/nvm.sh && nvm alias default 0.10.35

No Homebrew for this one.这个没有自制软件。

nvm soon will support io.js, but not at time of posting: https://github.com/creationix/nvm/issues/590 nvm很快将支持 io.js,但在发布时不支持: https : //github.com/creationix/nvm/issues/590

Then install everything else, per-project, with a package.json and npm install .然后使用package.jsonnpm install每个项目的其他所有内容。

I agree with noa -- if you need to have multiple versions of node , io.js then brew is not the appropriate solution.我同意 noa - 如果您需要有多个版本的nodeio.js则 brew 不是合适的解决方案。

You can help beta-test io.js support in nvm: https://github.com/creationix/nvm/pull/616您可以在 nvm 中帮助 beta-test io.js支持: https : //github.com/creationix/nvm/pull/616

If you just want io.js and are not switching versions, then you can install the binary distribution of io.js from https://iojs.org/dist/v1.0.2/iojs-v1.0.2-darwin-x64.tar.gz ;如果您只想要io.js并且不切换版本,那么您可以从https://iojs.org/dist/v1.0.2/iojs-v1.0.2-darwin-x64.tar安装io.js的二进制分发io.js .gz ; that includes npm and you will not need nvm if you are not switching versions.这包括npm ,如果您不切换版本,则不需要nvm

Remember to update npm after installing: sudo npm install -g npm@latest安装后记得更新npmsudo npm install -g npm@latest

For install with zsh and Homebrew:使用 zsh 和 Homebrew 安装:

brew install nvm

Then Add the following to ~/.zshrc or your desired shell configuration file:然后将以下内容添加到 ~/.zshrc 或您想要的 shell 配置文件:

export NVM_DIR="$HOME/.nvm"
. "/usr/local/opt/nvm/nvm.sh"

Then install a node version and use it.然后安装一个节点版本并使用它。

nvm install 7.10.1
nvm use 7.10.1

2021 Update 2021 更新

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

Troubleshooting for MAC: MAC 故障排除:

Since macOS 10.15, the default shell is zsh and nvm will look for .zshrc to update, none is installed by default.从 macOS 10.15 开始,默认 shell 是 zsh 并且 nvm 将查找 .zshrc 来更新,默认情况下没有安装。 Create one with touch ~/.zshrc and run the install script again.使用 touch ~/.zshrc 创建一个并再次运行安装脚本。

If you use bash, the previous default shell, run touch ~/.bash_profile to create the necessary profile file if it does not exist.如果您使用 bash(以前的默认 shell),请运行 touch ~/.bash_profile 以创建必要的配置文件(如果它不存在)。

You might need to restart your terminal instance or run .您可能需要重新启动终端实例或运行 . ~/.nvm/nvm.sh. ~/.nvm/nvm.sh。 Restarting your terminal/opening a new tab/window, or running the source command will load the command and the new configuration.重新启动终端/打开新选项卡/窗口,或运行 source 命令将加载命令和新配置。

You have previously used bash, but you have zsh installed.您以前使用过 bash,但您安装了 zsh。 You need to manually add these lines to ~/.zshrc and run .您需要手动将这些行添加到 ~/.zshrc 并运行。 ~/.zshrc. ~/.zshrc。

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

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