Ubuntu配置以太坊私链开发环境(使用Ganache)

安装geth

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

在update时可能会遇到如下问题

E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/cache/app-info -a -e /usr/bin/appstreamcli; then appstreamcli refresh > /dev/null; fi'
E: Sub-process returned an error code

解决方案:

cd /tmp && mkdir asfix
cd asfix
wget https://launchpad.net/ubuntu/+archive/primary/+files/appstream_0.9.4-1ubuntu1_amd64.deb
wget https://launchpad.net/ubuntu/+archive/primary/+files/libappstream3_0.9.4-1ubuntu1_amd64.deb
sudo dpkg -i *.deb

之后再继续安装就可以完成了,输入geth version可以看到版本信息

Geth
Version: 1.8.21-stable
Git Commit: 9dc5d1a915ac0e0bd8429d6ac41df50eec91de5f
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.10.4
Operating System: linux
GOPATH=
GOROOT=/usr/lib/go-1.10

https://blog.csdn.net/u014361775/article/details/78865582
https://blog.csdn.net/pony_maggie/article/details/79531534

安装solc

solidity是以太坊智能合约的开发语言,官方文档地址:https://solidity.readthedocs.io/en/latest/。
想要测试智能合约,开发DAPP的需要安装solc。,安装方法如下:

sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install solc

下载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

解压和安装Nodejs

1.用cd命令进入源代码压缩包所在的目录
2.根据压缩包类型解压缩文件(*代表压缩包名称):tar -zxvf ****.tar.gztar -jxvf ****.tar.bz(或bz2)
3.用CD命令进入解压缩后的目录
4.输入编译文件命令:./configure(有的压缩包已经编译过,这一步可以省去)
5.输入命令 make编译,应该要等好一会儿
6.再输入make install安装,安装好后可以查看node版本 node --version

如果之前意见有旧版本的Nodejs了,也可以直接更新,命令如下:

sudo npm cache clean -f  //清理Npm的cache
sudo npm install -g n   //安装node的版本管理工具“n“
n latest  // 更新到最新版

关于更新node,这里还有一篇比较考究的文章:https://blog.csdn.net/qq_16339527/article/details/73008708
编译Nodejs需要gcc/g++ 4.9.2以上,查看一下自己的系统里gcc/g++的版本,如果太旧的话就更新一下

关于npm
设成淘宝镜像

$ npm config set registry http://registry.npm.taobao.org/

若要换成原来的

$ npm config set registry https://registry.npmjs.org/

安装truffle

npm install -g truffle

如果使用truffle命令,出现如下的问题,主要是nodejs安装问题

SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:374:25)
    at Object.Module._extensions..js (module.js:417:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:442:10)
    at startup (node.js:136:18)
at node.js:966:3

在github上找到解决方法(https://github.com/trufflesuite/truffle/issues/128) ,把nodejs升级一下就可以了,升级方法上面已经有了

安装Ganache

也可以自己直接创建私链,做法和windows下步骤是一样的,具体参考另一篇文章:https://blog.csdn.net/Test_tju/article/details/86530485
这里还是先安装一下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: sudo npm install -g ganache-cli
注:有界面Ganache运行在7545端口;命令行ganache-cli运行在8545端口!!

使用Ganache部署合约

1.新建一个文件夹: mkdir private_net1
2.进入文件夹: cd private_net1
3.初始化: truffle init
4.查看产生的文件: ls
5.编辑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-cli,则改为8545
network_id: “*” // Match any network id
} }

6.编译: sudo truffle compile
7.部署:sudo truffle migrate Using network 'development'
8.交互
(1)启动:输入以下命令启动 Truffle 控制台,这会帮助我们与ganache的区块链进行交互 truffle console --network development
(2)退出:输入 .exit 退出控制台

文章分享:
1.从零开始搭建以太坊私链并部署智能合约
2.以太坊开发演练,Part-2:Truffle,Ganache,Geth 和 Mist
3.本地搭建以太坊(Ethereum)详细教程
4.Ubuntu下geth搭建私有链+部署智能合约+与合约交互笔记

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值