目錄
前言
區塊鏈有多熱就不說了. 結合實操,搭建一個基於truffle框架的以太坊私有鏈開發環境。
依賴環境
- 虛擬機器 VMware® Workstation 12 Pro ,不多解釋。
- ubuntu-14.04 ubuntu-14.04-desktop-amd64.iso ,不多解釋。
- nodejs ,用來安裝truffle等工具
- truffle ,目前最好用的以太坊開發框架
- ganache ,可以跑開發環境下的私有區塊鏈
步驟
- 前提是ubuntu已經可以正常使用,包括上網。
- 下載nodejs,按經驗apt-get安裝的nodejs不可用,估計apt-get源上的nodejs版本不對,建議使用nodejs官方github上的release 版本 ,可以git clone, 再git
checkout 到某個指定版本。也可直接下載某個穩定版本,比如:
wget https://github.com/nodejs/node/archive/v9.3.0.tar.gz
3.由於nodejs的編譯需要 gcc/g 4.9.2以上,ubuntu14.04自帶4.8.所有需要先更新到 gcc/g 4.9.2
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-4.9
sudo apt-get install g -4.9
ln -s /usr/bin/g -4.9 /usr/bin/g -f //修改連結,指向4.9
ln -s /usr/bin/gcc-4.9 /usr/bin/gcc -f //修改連結,指向4.9
4.編譯並安裝nodejs
cd ~/work/nodejsgit/node-9.3.0 //進入node程式碼目錄
make //編譯,等待一段較長時間
make install //安裝
[email protected]:~/work/nodejsgit/node-9.3.0$ node -v //檢視node版本
v9.3.0
[email protected]:~/work/nodejsgit/node-9.3.0$ npm -v //檢視npm版本
5.5.1
5.安裝truffle
npm install -g truffle
6.下載並安裝ganache
wget https://github.com/trufflesuite/ganache/releases/download/v1.0.1/ganache-1.0.1-x86_64.AppImage //下載ganache
chmod x ganache-1.0.1-x86_64.AppImage //修改許可權
sudo ./ganache-1.0.1-x86_64.AppImage //啟動ganache
跑一個例子
- 啟動區塊鏈網路ganache
[email protected]:/home/liao/work# ./ganache-1.0.1-x86_64.AppImage
- 新建專案
[email protected]:~/work$ mkdir myproject //新建一個工作目錄
[email protected]:~/work$ cd myproject/ //進入專案目錄
[email protected]:~/work/myproject$ truffle init //初始化Truffle 專案
[email protected]:~/work/myproject$ ls //檢視產生的檔案
build contracts migrations test truffle-config.js truffle.js
[email protected]:~/work/myproject$ cat truffle.js //修改配置檔案後如下
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
networks: {
development: {
host: "127.0.0.1", //本地地址,ganache log中可看到
port: 7545, //埠,ganache log中可看到
network_id: "*" // Match any network id
}
}
};
[email protected]:~/work/myproject$ truffle compile //編譯
[email protected]:~/work/myproject$ truffle migrate //跑migrate
Using network 'development'.
Network up to date.
- 或使用一個自帶的例子
mkdir MetaCoin
cd MetaCoin
truffle unbox metacoin
truffle compile
truffle migrate
写评论
很抱歉,必須登入網站才能發佈留言。