重要通知: 此文档针对的是 Yarn 1 (Classic) 版本。
对于 Yarn 2+ 文档及迁移指南,请访问 yarnpkg.cn。

安装

These instructions only cover Yarn versions prior to 2.0. Those versions entered maintenance mode in January 2020 and will eventually reach their end-of-life in terms of support. Please see the main website for the most up-to-date documentation: yarnpkg.com/getting-started/migration.
The latest Yarn version is: Latest CLI Release
稳定版: v1.22.17
支持的 Node 版本: ^4.8.0 || ^5.7.0 || ^6.2.2 || >=8.0.0
在使用 Yarn 之前,首先要在您的系统上安装 Yarn 。 虽然安装 Yarn 有很多种方式,但是建议采用这一种跨平台的安装方式:

Install via npm

It is recommended to install Yarn through the npm package manager, which comes bundled with Node.js when you install it on your system.

Once you have npm installed you can run the following both to install and upgrade Yarn:

npm install --global yarn

Alternatives

点击以展开 / 折叠

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
curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --rc
curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --nightly

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]/
cd /opt
wget https://yarnpkg.com/latest-rc.tar.gz
tar zvxf latest-rc.tar.gz
# Yarn is now in /opt/yarn-[version]/
cd /opt
wget https://nightly.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

目前 Yarn 没有针对 Homebrew 或 MacPorts 的 RC 或每日构建版本。请使用 tarball 包安装:

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
curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --rc
curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --nightly

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]/
cd /opt
wget https://yarnpkg.com/latest-rc.tar.gz
tar zvxf latest-rc.tar.gz
# Yarn is now in /opt/yarn-[version]/
cd /opt
wget https://nightly.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 等。

  1. 将此项加入您的配置文件: export PATH="$PATH:/opt/yarn-[version]/bin" (路径可能根据您安装 Yarn 的位置而有差异)
  2. 在终端中,执行登录并登出以使更改生效

为了可以全局访问 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