Substrate 环境安装提速文档(Mike版,仅限Debian/Ubuntu Linux 和 Mac brew)
================================
这是一份提速文档:)
这是一篇非官方的 Substrate 环境安装文档,因为我发现 Substrate 官方的安装脚本中有以下几个问题:
对于咋们墙内的同学来说,完成一次安装,就像经历了一次地狱。
下面切入正题。
官方的一键安装脚本是这个:
我们看一下这个脚本里面什么内容:
if [[ `whoami` == "root" ]]; then
MAKE_ME_ROOT=
else
MAKE_ME_ROOT=sudo
fi
if [ -f /etc/redhat-release ]; then
echo "Redhat Linux detected."
echo "This OS is not supported with this script at present. Sorry."
echo "Please refer to https://github.com/paritytech/substrate for setup information."
elif [ -f /etc/SuSE-release ]; then
echo "Suse Linux detected."
echo "This OS is not supported with this script at present. Sorry."
echo "Please refer to https://github.com/paritytech/substrate for setup information."
elif [ -f /etc/arch-release ]; then
echo "Arch Linux detected."
$MAKE_ME_ROOT pacman -Syu --needed cmake gcc openssl-1.0 pkgconf git clang
export OPENSSL_LIB_DIR="/usr/lib/openssl-1.0";
export OPENSSL_INCLUDE_DIR="/usr/include/openssl-1.0"
elif [ -f /etc/mandrake-release ]; then
echo "Mandrake Linux detected."
echo "This OS is not supported with this script at present. Sorry."
echo "Please refer to https://github.com/paritytech/substrate for setup information."
elif [ -f /etc/debian_version ]; then
echo "Ubuntu/Debian Linux detected."
$MAKE_ME_ROOT apt update
$MAKE_ME_ROOT apt install -y cmake pkg-config libssl-dev git gcc build-essential clang libclang-dev
else
echo "Unknown Linux distribution."
echo "This OS is not supported with this script at present. Sorry."
echo "Please refer to https://github.com/paritytech/substrate for setup information."
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "Mac OS (Darwin) detected."
if ! which brew >/dev/null 2>&1; then
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
fi
brew install openssl cmake llvm
elif [[ "$OSTYPE" == "freebsd"* ]]; then
echo "FreeBSD detected."
echo "This OS is not supported with this script at present. Sorry."
echo "Please refer to https://github.com/paritytech/substrate for setup information."
else
echo "Unknown operating system."
echo "This OS is not supported with this script at present. Sorry."
echo "Please refer to https://github.com/paritytech/substrate for setup information."
fi
if ! which rustup >/dev/null 2>&1; then
curl https://sh.rustup.rs -sSf | sh -s -- -y
source ~/.cargo/env
rustup default stable
else
rustup update
fi
function install_substrate {
g=`mktemp -d`
git clone https://github.com/paritytech/substrate $g
pushd $g
./scripts/init.sh
./scripts/build.sh
cargo install --force --path . substrate
popd
}
if [[ "$1" == "--fast" ]]; then
echo "Skipped cargo install of 'substrate' and 'subkey'"
echo "You can install manually by cloning the https://github.com/paritytech/substrate repo,"
echo "building the Wasm, and using cargo to install 'substrate' and 'subkey' from the repo path."
else
cargo install --force --git https://github.com/paritytech/substrate subkey
install_substrate
fi
f=`mktemp -d`
git clone https://github.com/paritytech/substrate-up $f
cp -a $f/substrate-* ~/.cargo/bin
cp -a $f/polkadot-* ~/.cargo/bin
echo "Run source ~/.cargo/env now to update environment"
简单分析一下后,可以整理出一个更高效的安装步骤。
1. 安装系统依赖包
Linux
sudo apt updatesudo apt install -y cmake pkg-config libssl-dev git gcc build-essential clang libclang-dev
Mac
brew install openssl cmake llvm2. 安装 Rust 工具链(如果之前已经装过 Rust 工具链,此步骤可跳过)
source ~/.cargo/env
rustup default stable
rustup update nightly
可以看到,安装后,默认还是用的 stable 版本。这里提醒一下,如果之前装过 Rust 工具链,按官方的脚本走,会重做一次 rustup update。开发 substrate,不一定非要用最新的 Rust 版本(但是太老的估计也不行),nightly 版本几乎每日构建,所以你每执行一次 rustup update,就可能会重新下载新的构建版本,比较费时。
安装好了 rust 编译器后,依据网络流畅度,我们可以修改一下包管理器源地址为国内 rustcc 社区提供的源地址,把下面内容填充到你的 ~/.cargo/config 文件中(没有就创建一个):
replace-with = "rustcc"
[source.rustcc]
registry = "https://code.aliyun.com/rustcc/crates.io-index.git"
后面的源码编译就会使用这个源地址。
3. 安装 substrate 的基础组件
3.1 安装 nightly 版本的 wasm32 target,执行
cd ~/works/
rustup target add wasm32-unknown-unknown --toolchain nightly
3.2 下载 wasm-gc 源码,安装
cd ~/works/
git clone https://github.com/alexcrichton/wasm-gc
cd wasm-gc
cargo +nightly build --release --target-dir=../nightly_target
cp -af ../nightly_target/release/wasm-gc ~/.cargo/bin
3.3 (这一步可选)下载 substrate 代码
git clone https://github.com/paritytech/substrate
3.4 (这一步可选)进入下载的 substrate 代码目录,编译安装 substrate 工具和 subkey 工具
cd ~/works/substrate
cargo build --release --target-dir=../target
cp -af ../target/release/substrate ~/.cargo/bin
cargo build -p subkey --release --target-dir=../target
cp -af ../target/release/subkey ~/.cargo/bin
说明:
4. 安装 substrate 工程辅助脚本
cp -af substrate-up/substrate-* ~/.cargo/bin
cp -af substrate-up/polkadot-* ~/.cargo/bin
环境和工具安装完毕。
如何加快我们自己的 substrate runtime 工程编译
====================================
官方 substrate-package 仓库地址:
https://github.com/substrate-developer-hub/substrate-package
官方给出的一个 node 模板的相关操作指令如下:
# BuildingInstall Rust:
```bash
curl https://sh.rustup.rs -sSf | sh
```
Install required tools:
```bash
./scripts/init.sh
```
Build the WebAssembly binary:
```bash
./scripts/build.sh
```
Build all native code:
```bash
cargo build
如果我们前面已经完成了 substrate 的环境安装,其实后面只需要执行第 3 步和第 4 步,于是可以优化成下面的指令:
cd ~/works/my_substrate_node/
编译 wasm 文件
./scripts/build.sh
编译 native node
cargo build --target-dir=../target
或
cargo build --release --target-dir=../target
编译出的二进制文件在 ../target/ 中。
这样能充分利用前面的缓存,使得编译可以大大加速。如果前面没有缓存,这样操作,也可以在不同的项目间共享编译缓存。
完结。