第4課 如何在UBUNTU虛擬機上編譯EOS完成環境搭建?

1,摘要

【本文目標】
通過本文實踐,能在WINDOWS操作系統搭建UBUNTU 18.04 LTS環境,并在此環境編譯成功EOS v1.0.5正式發布版本。
【前置條件】
有學習EOS環境搭建意向,不怕折騰。
【技術收獲】
1)搭建UBUNTU 18.04 LTS環境
2)在硬件要求不滿足官方要求的環境下EOS環境編譯成功
3) EOS編譯過程中的踩坑排查能力

2,編譯血淚史

EOS主網上線沒有幾天,從各種渠道得知EOS環境搭建比較麻煩,輝哥剛開始想繞過編譯環節或者直接使用可直接使用的環境。于是,輝哥首先從趙志明老師那兒拉個一個V1.0.5的已編譯成功的版本,拉到筆記本的Ubuntu虛擬機環境根本沒法跑起來。
然后,輝哥想用歐陽哥哥花了好長時間搭建成功的虛擬機環境來測試操作,結果發現EOS節點運行起來后,他主機的CPU占用率為90%,他就沒法工作了。
又為了省錢,不愿意購買阿里云ECS環境。最終,輝哥還是在古千峰老師和歐陽哥哥的鼓勵下,下定決心把EOS環境編譯成功。因為目前大部分研發人員用的是WINDOWS操作系統,那在此基礎上搭建Ubuntu虛擬機成功對大部分EOS學習者來說非常有幫助。而坊間搭建EOS環境編譯的步驟介紹中都非常簡單,基本都不寫編譯失敗或者踩過的坑,好像編譯都是很順利似的。作為一個熱心的技術布道者,輝哥就下定決心把環境編譯好,并把踩的坑和方法都貢獻出來,供大家學習交流。
如果還有其他人能夠在其他環境,例如Centos 7, MacOs上編譯成功的,希望也可以把自己編譯過程制作文檔,在本文留言,讓更多的人少踩坑。

輝哥把編譯成功的修改腳本,下載附件放在知識星球了,需要者加入自取,加入知識星球后也可以加入VIP微信群,提供技術對接。
image

3,環境配置

1)WINDOWS主機及操作系統
關鍵信息:Windows 10 64位操作系統,8G內存,4核CPU。

Windows 10系統環境.jpg

分析:從RAM要求看是不滿足7GB RAM空閑資源要求,但有方法繞過去。

2)VmWare虛擬機
輝哥用的是VMware 10版本,可從官網下載或者參考《第一課 如何在WINDOWS環境下搭建以太坊開發環境》 的“1,安裝VMware虛擬機”章節完成虛擬機的安裝。

3)Ubuntu資源配置
按照EOS的要求,輝哥選擇UBUNTU 18.04 LTS版本。下載官網地址為https://www.ubuntu.com/download/server/thank-you?country=CN&version=18.04&architecture=amd64,AMD架構的經安裝驗證沒有問題的。安裝和配置方法參考《第一課 如何在WINDOWS環境下搭建以太坊開發環境》 的“2,安裝Ubuntu操作系統”章節完成虛擬機的安裝,建議硬盤和內存選擇要盡可能大一些。

Ubuntu資源.png

EOSIO的安裝最低配置:

  1. 7GB RAM free required
  2. 20GB Disk free required
    輝哥的環境的RAM是不滿足要求的,后面編譯的時候注釋代碼繞過去,不影響編譯成功。

4,編譯歷程和踩坑記錄

4.1 【錯誤1】直接下載版本不可行

輝哥從EOSIO官網上下載V1.0.5的版本,然后上傳到Ubuntu虛擬機環境,運行'./eosio_build.sh'提示必須用github下載。只所以用v1.0.5版本,只是一個個人臨時決定而已。

【信息交互提示1】

duncanwang@duncanwang:~/work/eos-1.0.5$ ./eosio_build.sh

輸出:

> -bash: ./eosio_build.sh: Permission denied

解決方案:

duncanwang@duncanwang:~/work/eos-1.0.5$ chmod 755 ./eosio_build.sh

【信息交互提示2】

duncanwang@duncanwang:~/work/eos-1.0.5$ ./eosio_build.sh

輸出:

> This build script only works with sources cloned from git
    Please clone a new eos directory with 'git clone https://github.com/EOSIO/eos --recursive'
    See the wiki for instructions: https://github.com/EOSIO/eos/wiki

4.2 【錯誤2】Github下載提示連不上github

【信息交互提示】

duncanwang@duncanwang:~/work$ git clone -b v1.0.5 https://github.com/EOSIO/eos --recursive

輸出:

Cloning into 'eos'...
fatal: unable to access 'https://github.com/EOSIO/eos/': Could not resolve host: github.com

解決方案:
提示表明鏈接不上github網站,增加了香港服務器解決。

  1. 加入香港線路

sudo vim /etc/hosts

加入以下配置后保存

192.30.253.113 github.com

  1. 命令行增加配置

git config --global http.sslversion tlsv1

4.3 【錯誤3】Github下載提示端口錯誤

【信息交互提示】

duncanwang@duncanwang:~/work$ git clone https://github.com/EOSIO/eos --recursive

Cloning into 'eos'...
fatal: unable to access 'https://github.com/EOSIO/eos/': Failed to connect to github.com port 443: No route to host

duncanwang@duncanwang:~/work$ ping github.com

PING github.com (192.30.253.113) 56(84) bytes of data.
From 192.168.1.100 icmp_seq=1 Destination Host Unreachable
From 192.168.1.100 icmp_seq=2 Destination Host Unreachable
From 192.168.1.100 icmp_seq=3 Destination Host Unreachable
From 192.168.1.100 icmp_seq=4 Destination Host Unreachable
From 192.168.1.100 icmp_seq=5 Destination Host Unreachable

解決方案:
參考網上方法,運行以下命令,但是結果還是不行。

sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT

古哥的建議是先解決linux翻墻的問題,有點小難度。輝哥就重啟了一下Ubuntu虛擬機,然后重新下載,發現下載成功了。

4.4 【成功】版本下載成功

duncanwang@duncanwang:~$ git clone -b v1.0.5 https://github.com/EOSIO/eos --recursive

Cloning into 'eos'...
remote: Counting objects: 107783, done.
remote: Compressing objects: 100% (66/66), done.
remote: Total 107783 (delta 25), reused 34 (delta 13), pack-reused 107700
Receiving objects: 100% (107783/107783), 105.34 MiB | 2.61 MiB/s, done.
Resolving deltas: 100% (85696/85696), done.
Note: checking out 'c9b7a2472dc3c138e64d07ec388e64340577bb34'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

4.5 【錯誤4】編譯提示RAM空間不足

輸入:

duncanwang@duncanwang:~/eos$ ./eosio_build.sh

錯誤提示:

    Beginning build version: 1.2
    Wed Jun 27 10:24:48 UTC 2018
    User: duncanwang
cat: .git/refs/heads/master: No such file or directory
    git head id: 
    Current branch: HEAD

    ARCHITECTURE: Linux

    OS name: Ubuntu
    OS Version: 18.04
    CPU speed: 3192.622Mhz
    CPU cores: 2
    Physical Memory: 974 Mgb
    Disk install: /dev/sda2
    Disk space total: 19G
    Disk space available: 13G
    Your system must have 7 or more Gigabytes of physical memory installed.
    Exiting now.

解決方法:
進到eos/scripts/路徑,修改對應的sh文件,找到“Your system must have 7 or more Gigabytes of physical memory installed”字樣,或者You must have at least %sGB of available storage to install EOSIO,將后面的exit 1命令去掉即可。

 if [ "${MEM_MEG}" -lt 7000 ]; then
        printf "\\tYour system must have 7 or more Gigabytes of physical memory installed.\\n"
        printf "\\tExiting now.\\n"
  #     exit 1
    fi

順道把硬盤不足的提示也注釋掉,測試發現13G的空閑硬盤是可以編譯成功的。

>   if [ "${DISK_AVAIL%.*}" -lt "${DISK_MIN}" ]; then
        printf "\\tYou must have at least %sGB of available storage to install EOSIO.\\n" "${DISK_MIN}"
        printf "\\tExiting now.\\n"
#       exit 1
    fi

4.6 【錯誤5】mongoDB無法下載

編譯過程中錯誤提示:

    Boost successfully installed @ /home/duncanwang/opt/boost.

    Checking MongoDB installation.

    Installing MongoDB 3.6.3.
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:30 --:--:--     0
curl: (28) Operation timed out after 30002 milliseconds with 0 out of 0 bytes received
    Unable to download MongoDB at this time.
    Exiting now.

分析:
eosio_build_ubuntu.sh 查找 “Unable to download MongoDB at this time.”關鍵字
可知這個語句執行失敗。

--STATUS=$(curl -LO -w '%{http_code}' --connect-timeout 30 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.3.tgz)

解決辦法:
1)本地下載2個文件“mongodb-linux-x86_64-3.6.3.tgz”和“mongo-c-driver-1.9.3.tar.gz”,上傳到虛擬機的軟件存放目錄“/home/duncanwang/work/software”,
2)修改腳本,重新運行./eosio_build.sh即可解決。這2個下載文件可在輝哥的知識星球找到,也可以上官網下載。

        #STATUS=$(curl -LO -w '%{http_code}' --connect-timeout 30 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.3.tgz)
        #if [ "${STATUS}" -ne 200 ]; then
        #   printf "\\tUnable to download MongoDB at this time.\\n"
        #   printf "\\n\\tExiting now.\\n\\n"
        #   exit 1;
        #fi
        cp /home/duncanwang/work/software/mongodb-linux-x86_64-3.6.3.tgz "${HOME}/opt/"

#       STATUS=$(curl -LO -w '%{http_code}' --connect-timeout 30 https://github.com/mongodb/mongo-c-driver/releases/download/1.9.3/mongo-c-driver-1.9.3.tar.gz)
#       if [ "${STATUS}" -ne 200 ]; then
#           if ! rm -f "${TEMP_DIR}/mongo-c-driver-1.9.3.tar.gz"
#           then
#               printf "\\n\\tUnable to remove file %s/mongo-c-driver-1.9.3.tar.gz.\\n" "${TEMP_DIR}"
#           fi
#           printf "\\tUnable to download MongoDB C driver at this time.\\n"
#           printf "\\tExiting now.\\n\\n"
#           exit 1;
#       fi
   cp /home/duncanwang/work/software/mongo-c-driver-1.9.3.tar.gz "${TEMP_DIR}/"

4.7 【錯誤6】RPC錯誤
編譯過程中錯誤提示:

error: RPC failed; curl 56 GnuTLS recv error (-54): Error in the pull function.
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed
    Unable to clone clang repo @ https://github.com/llvm-mirror/clang.git.

    Exiting now.

Cloning into 'llvm'...
remote: Counting objects: 24803, done.
remote: Compressing objects: 100% (23960/23960), done.
error: RPC failed; curl 56 GnuTLS recv error (-54): Error in the pull function.
fatal: The remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

解決方法
提示RPC錯誤,古千峰反饋說,主網RPC服務不穩定。
1) 輝哥想到的方法是把需要的git clone --depth 1 --single-branch --branch release_40 https://github.com/llvm-mirror/llvm.gitgit clone --depth 1 --single-branch --branch release_40 https://github.com/llvm-mirror/clang.git單獨執行或者從windows下載轉放到Ubuntu對應目錄。
編譯的提示如下,還是搞不定。有大俠認為是因為我們下載的llvm.git,clang.git用的是最新版本的原因,沒有用 release_40的版本,沒有做重新嘗試。

CMake Error at tools/clang/tools/diagtool/CMakeLists.txt:14 (target_link_libraries):
  The keyword signature for target_link_libraries has already been used with
  the target "diagtool".  All uses of target_link_libraries with a target
  must be either all-keyword or all-plain.

  The uses of the keyword signature are here:

   * cmake/modules/LLVM-Config.cmake:105 (target_link_libraries)
   * cmake/modules/AddLLVM.cmake:785 (target_link_libraries)

2)輝哥第二天早晨過來,把 /tmp/llvm-compiler文件夾刪除,重新編譯一下,結果什么都成了。

4.8【成功】編譯成功

完成4.7的刪除文件夾重編譯后,編譯就成功了。
1.EOS編譯成功.png

4.9【成功】安裝包

sudo make install

執行結果為成功,如下:

-- Installing: /usr/local/bin/nodeos
-- Installing: /usr/local/var/log/eosio
-- Installing: /usr/local/var/lib/eosio
-- Installing: /usr/local/bin/cleos
-- Installing: /usr/local/bin/keosd
-- Installing: /usr/local/bin/eosio-launcher
-- Installing: /usr/local/bin/eosio-abigen
-- Installing: /usr/local/bin/eosiocpp
duncanwang@duncanwang:~/eos/build$ make test

4.10【部分失敗】驗證安裝是否成功

1) 運行mongod,在Ubuntu系統中,運行:

~/opt/mongodb/bin/mongod -f ~/opt/mongodb/mongod.conf &
輸出內容:

[1] 24769

2)檢驗eosio是否安裝成功命令

duncanwang@duncanwang:~/eos/build$ make test

26/36 Test #26: unit_test_binaryen ...................   Passed  130.46 sec
      Start 27: unit_test_wavm
27/36 Test #27: unit_test_wavm .......................***Exception: SegFault433.62 sec
      Start 28: validate_deferred_test_abi
28/36 Test #28: validate_deferred_test_abi ...........   Passed    0.04 sec
      Start 29: plugin_test
29/36 Test #29: plugin_test ..........................   Passed    0.10 sec
      Start 30: nodeos_run_test
30/36 Test #30: nodeos_run_test ......................   Passed   61.58 sec
      Start 31: p2p_dawn515_test
31/36 Test #31: p2p_dawn515_test .....................   Passed    8.18 sec
      Start 32: distributed-transactions-test
32/36 Test #32: distributed-transactions-test ........   Passed   74.44 sec
      Start 33: restart-scenarios-test-resync
33/36 Test #33: restart-scenarios-test-resync ........   Passed  126.77 sec
      Start 34: restart-scenarios-test-hard_replay
34/36 Test #34: restart-scenarios-test-hard_replay ...   Passed  123.08 sec
      Start 35: restart-scenarios-test-none
35/36 Test #35: restart-scenarios-test-none ..........   Passed  130.01 sec
      Start 36: validate_dirty_db_test
36/36 Test #36: validate_dirty_db_test ...............   Passed    2.41 sec

97% tests passed, 1 tests failed out of 36

Total Test time (real) = 1091.60 sec

The following tests FAILED:
     27 - unit_test_wavm (SEGFAULT)
Errors while running CTest
Makefile:119: recipe for target 'test' failed
make: *** [test] Error 8

5. 編譯總結

EOS成功的編譯總是相同的,不成功的編譯各個各的不幸。這兒總結下成功的步驟,一般是及其簡單的。
1)、下載最新代碼
git clone https://github.com/EOSIO/eos.git --recursive
3)、內存限制修改
ubuntu:eosio_build_ubuntu.sh 25行7000改成3000
4)、編譯
./eosio_build.sh
5)、安裝命令:cd build 然后maker install

6. 【成功】私有節點運行

輝哥解決不了哪個安裝測試錯誤的問題,就當做沒有發生了。
直接輸入命令啟動私有節點。

cd ~/eos/build/programs/nodeos

./nodeos -e -p eosio --plugin eosio::chain_api_plugin --plugin eosio::history_api_plugin

輸出結果如下:

1672402ms thread-0   chain_plugin.cpp:208          plugin_initialize    ] initializing chain plugin
1672402ms thread-0   chain_plugin.cpp:391          plugin_initialize    ] Starting up fresh blockchain with default genesis state.
CHAINBASE:   Failed to pin chainbase shared memory (of size 1024 MB) in RAM. Performance degradation is possible.
CHAINBASE:   Failed to pin chainbase shared memory (of size 340 MB) in RAM. Performance degradation is possible.
1672457ms thread-0   http_plugin.cpp:290           plugin_initialize    ] configured http to listen on 127.0.0.1:8888
1672458ms thread-0   net_plugin.cpp:2947           plugin_initialize    ] Initialize net plugin
1672458ms thread-0   net_plugin.cpp:2971           plugin_initialize    ] host: 0.0.0.0 port: 9876 
1672458ms thread-0   net_plugin.cpp:3043           plugin_initialize    ] my node_id is dd1b4640288cfefc56f90bc02f92b7ddd5f8e2e6f1d46d0422f7a5f43dafce8e
1672459ms thread-0   main.cpp:104                  main                 ] nodeos version c9b7a247
1672459ms thread-0   main.cpp:105                  main                 ] eosio root is /home/duncanwang/.local/share
1672459ms thread-0   controller.cpp:1190           startup              ] No head block in fork db, perhaps we need to replay
1672459ms thread-0   controller.cpp:304            initialize_fork_db   ]  Initializing new blockchain with genesis state                  
1672496ms thread-0   chain_plugin.cpp:446          plugin_startup       ] starting chain in read/write mode
1672496ms thread-0   chain_plugin.cpp:451          plugin_startup       ] Blockchain started; head block is #1, genesis timestamp is 2018-06-01T12:00:00.000
1672496ms thread-0   http_plugin.cpp:331           plugin_startup       ] start listening for http requests
1672496ms thread-0   chain_api_plugin.cpp:75       plugin_startup       ] starting chain_api_plugin
1672496ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/abi_bin_to_json
1672496ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/abi_json_to_bin
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_abi
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_account
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_block
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_block_header_state
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_code
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_currency_balance
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_currency_stats
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_info
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_producers
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_required_keys
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/get_table_rows
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/push_block
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/push_transaction
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/chain/push_transactions
1672497ms thread-0   history_api_plugin.cpp:38     plugin_startup       ] starting history_api_plugin
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/history/get_actions
1672497ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/history/get_controlled_accounts
1672498ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/history/get_key_accounts
1672498ms thread-0   http_plugin.cpp:377           add_handler          ] add api url: /v1/history/get_transaction
1672498ms thread-0   net_plugin.cpp:3055           plugin_startup       ] starting listener, max clients is 25
1672498ms thread-0   producer_plugin.cpp:577       plugin_startup       ] producer plugin:  plugin_startup() begin
1672498ms thread-0   producer_plugin.cpp:592       plugin_startup       ] Launching block production for 1 producers at 2018-06-29T09:27:52.498.
1672499ms thread-0   producer_plugin.cpp:604       plugin_startup       ] producer plugin:  plugin_startup() end
1673001ms thread-0   producer_plugin.cpp:1073      produce_block        ] Produced block 000000024e3c9579... #2 @ 2018-06-29T09:27:53.000 signed by eosio [trxs: 0, lib: 0, confirmed: 0]
1673502ms thread-0   producer_plugin.cpp:1073      produce_block        ] Produced block 00000003176eba27... #3 @ 2018-06-29T09:27:53.500 signed by eosio [trxs: 0, lib: 2, confirmed: 0]
1674001ms thread-0   producer_plugin.cpp:1073      produce_block        ] Produced block 00000004726778be... #4 @ 2018-06-29T09:27:54.000 signed by eosio [trxs: 0, lib: 3, confirmed: 0]
1674502ms thread-0   producer_plugin.cpp:1073      produce_block        ] Produced block 0000000546ea9c4a... #5 @ 2018-06-29T09:27:54.500 signed by eosio [trxs: 0, lib: 4, confirmed: 0]

【問題】系統異常重啟后加載nodeos節點失敗。
【linux-ubuntu解決方法】
清理舊的數據即可重新啟動。

cd ~/.local/share/eosio/nodeos
rm -rf data

7. 參考

1)Windows10下 opencv2.4.9+dlib19.7+Visual Studio 2017 配置教程
2) EOS智能合約開發(一):EOS環境搭建和啟動節點[P叔]
3)官網本地環境搭建
4)EOSIO編譯安裝[古千峰@BTCMedia]
5)EOS測試環境快速搭建

編譯過程中得到古千峰大俠和歐陽哥哥的幫助,深表感謝。技術圈要多做分享,不怕被學習,自身更容易獲得成長!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容