macOS
Homebrew
你可以通过 Homebrew 包管理工具 安装 Yarn。 如果你还未安装 Node.js,Homebrew 会自动为你安装。
brew install yarn
如果你用的是 nvm 或类似工具,你需要确保 PATH
变量中列出的 nvm 版本的路径在 Homebrew 安装的 Node.js 路径之前。
MacPorts
你可以通过 MacPorts 安装 Yarn 。 如果你还未安装 Node.MacPorts 会自动为你安装。
sudo port install yarn
Installation Script
One of the easiest ways to install Yarn on macOS and generic Unix environments is via our shell script. You can install Yarn by running the following code in your terminal:
curl -o- -L https://yarnpkg.com/install.sh | bash
The installation process includes verifying a GPG signature. View the source on GitHub
You can also specify a version by running the following code in your terminal:
curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version [version]
See the releases for possible versions.
Manual Install via tarball
You can install Yarn by downloading a tarball and extracting it anywhere.
cd /opt
wget https://yarnpkg.com/latest.tar.gz
tar zvxf latest.tar.gz
# Yarn is now in /opt/yarn-[version]/
Before extracting Yarn, it is recommended that you verify the tarball using GPG:
wget -qO- https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --import
wget https://yarnpkg.com/latest.tar.gz.asc
gpg --verify latest.tar.gz.asc
# Look for "Good signature from 'Yarn Packaging'" in the output
路径设置
如果未在 PATH 环境变量中找到 yarn,请按照以下步骤添加 yarn 到 PATH 环境变量中,使其可以随处运行。
注意:您的配置文件可能是 .profile
、.bash_profile
、.bashrc
、.zshrc
等。
- 将此项加入您的配置文件:
export PATH="$PATH:/opt/yarn-[version]/bin"
(路径可能根据您安装 Yarn 的位置而有差异) - 在终端中,执行登录并登出以使更改生效
为了可以全局访问 Yarn 的可执行文件,你需要在控制台(或命令行)中设置 PATH
环境变量。若要执行此操作,请添加 export PATH="$PATH:`yarn global bin`"
到你的配置文件中,或者,如果你使用的是 Fish shell,直接执行此命令 set -U fish_user_paths (yarn global bin) $fish_user_paths
即可。
升级 Yarn
有新版时,Yarn 会给你提示。 如需升级 Yarn ,仍然可以通过 Homebrew 来升级。
brew upgrade yarn
通过如下命令测试 Yarn 是否安装成功:
yarn --version