SETP0,安裝Substrate開發(fā)環(huán)境

簡介

安裝 Rust

  • 命令行輸入:curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

  • 輸入1 進行默認安裝


    image.png
  • 安裝過程很順利,最后提示 Rust is installed now. Great!


    image.png
  • 查看可執(zhí)行文件 ls ~/.cargo/bin

    image.png

  • 為了可以在命令行中直接使用 rustc 命令可以 source ~/.profile

  • 或者 vim ~/.bash_profile

# 在最后一行添加
$ export PATH="$HOME/.cargo/bin:$PATH"
  • 操作演示如下圖。


    image.png
  • 最后通過 rustup default stable 命令,將rustup 設(shè)定為穩(wěn)定版本

    image.png

  • 如上圖所示我們安裝的nightly版本是 2021-02-10,為了之后的學(xué)習我們需要把版本切換到 nightly-2020-10-06

# 輸入命令:
rustup install nightly-2020-10-06
# 上面命令后會進入安裝該版本的環(huán)節(jié),安裝后使用 `rustup default nightly-2020-10-06` 命令切換到該版本,這個很重要。

  • 操作截圖:


    image.png
  • 至此Rust 安裝并配置完畢

安裝 wasm32-unknown-unknown 即可:安裝方法

# 主要是安裝這個。
rustup target add wasm32-unknown-unknown 
# 順便把下面兩個一并安裝。
cargo install wasm-pack
cargo install https   

安裝

  • Mac 需要安裝 brew,一般來講Mac用戶都會安裝這個命令,如果已經(jīng)安裝直接 brew updatebrew install openssl cmake 即可,如下是全部流程:
# 此摘要源自:https://substrate.dev/docs/en/knowledgebase/getting-started/
# Install Homebrew if necessary https://brew.sh/
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

# Make sure Homebrew is up-to-date, install openssl and cmake
brew update
brew install openssl cmake
  • 如果安裝出錯可能是國外源被屏蔽了可以使用國內(nèi)的工具包:
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"
brew update
brew install openssl cmake

  • brew install openssl cmake 如果報錯的解決辦法
# 如果 出錯可以使用 
brew install --build-from-source cmake 通過源碼安裝試試(不過這個超級慢做好心理準備)
# 編譯大概需要1個小時左右,根據(jù)機器性能的不同,編譯后需要添加環(huán)境變量。
echo 'export PATH="/usr/local/opt/sphinx-doc/bin:$PATH"' >> ~/.bash_profile

安裝 Notejs

# Mac 下可以使用 curl 命令下載,或者瀏覽器直接訪問安裝包地址。
curl https://nodejs.org/dist/v14.16.0/node-v14.16.0.pkg -o ~/Desktop/node-v14.16.0.pkg
# 下載完成后Mac 上雙擊安裝即可
  • 驗證版本:node -v 顯示 v14.16.0 表示安裝成功。
    image.png

安裝Yarn

  • Yarn 是更牛逼的npm 包管理器,如果你沒有安裝的化也需要安一個。
# 我這里使用cnpm 如果你不知道cnpm 是什么baidu一下吧,如果你有國外代理直接使用 npm 也可以
cnpm install --global yarn
  • 驗證 yarn --version 安裝


    image.png

編譯Substrate

  • 經(jīng)過上面漫長的配置等待,接下來開始編譯 Substrate ,為了和大家保持一致,Substrate和Substrate-node-template版本:v2.0.0,這個版本很重要否則后面可能出現(xiàn)莫名其妙的問題,上面的Rust 使用的是 nightly-2020-10-06 版本,這個也很重要。
  • 克隆代碼倉庫
# 首先切換到自己常用的代碼倉庫, cd ~/ 找個合適的位置
# 然后下載 2.0.0 版本代碼
git clone -b v2.0.0 --depth 1 https://github.com/substrate-developer-hub/substrate-node-template
# 進入這個 substrate-node-template 
git tag # 如果顯示 v2.0.0 表示對了。
# 然后開始 make 項目
make init
make build

# 然后切換到上級目錄后下載前端代碼倉庫
cd ..
git clone -b v2.0.0 --depth 1 https://github.com/substrate-developer-hub/substrate-front-end-template
cd substrate-front-end-template/ # git tag 應(yīng)該 v2.0.0
yarn install # 安裝前端項目的依賴包
  • 最終效果截圖:


    image.png

啟動后端節(jié)點

  • 啟動節(jié)點
cd ../substrate-node-template/
./target/release/node-template --dev --tmp
image.png
image.png

啟動前端節(jié)點

## 啟動前端

要與您的本地節(jié)點交互,我們將使用 [Substrate 開發(fā)者 Front-end Template](https://github.com/substrate-developer-hub/substrate-front-end-template),它集合了常規(guī)情況下需要的各種前端交互組件。

如果您已經(jīng)安裝了 Front-end Template,請在 Front-end Template 所在的根目錄中執(zhí)行以下命令:

確保在 Front-end Template 根目錄中運行此命令

cd ../substrate-front-end-template/ 
yarn start
image.png

瀏覽器訪問效果

配置開發(fā)環(huán)境

  • 下載安裝 vscode,然后點擊插件,我們安裝插件


    image.png
  • 安裝如下插件,逐個搜索并安裝如下:
Rust-in-peace Extension Pack
vscode-rust-syntax
Rust syntax # 這個有可能沒有,請翻墻
  • 需要安裝的插件大致如下:


    image.png

可能遇到的問題

rust-analyzer加載時間過長

Blocking waiting for file lock on package cache
  • 運行
rm -rf ~/.cargo/.package-cache
cargo metadata
  • 之后重啟 vscode

cargo metadata 過慢

cd ~/.cargo
  • 創(chuàng)建一個 config 文件,這里使用 vim 編輯器:
vim config
  • 將下面的代碼貼進去:
# 放到 `$HOME/.cargo/config` 文件中
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"

# 替換成你偏好的鏡像源
replace-with = 'sjtu'
#replace-with = 'ustc'

# 清華大學(xué)
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

# 中國科學(xué)技術(shù)大學(xué)
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

# 上海交通大學(xué)
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"

# rustcc社區(qū)
[source.rustcc]
registry = "git://crates.rustcc.cn/crates.io-index"

結(jié)束

  • 感謝閱讀,接下來會進入SETP1環(huán)節(jié),參考下一篇文章。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容