Fastest way to set up Hyperledger Fabric
When I first learned about blockchains, I was so fascinated by it, that I started working on it right away. I have been working on blockchains for over an year now.
Hyperledger Fabric is one of the most popular blockchain platform. The reasons I see behind its success is first that it is permissioned, so organisations can adopt it more readily and second it has huge support available online. But as a newbie, specially a student, it is very difficult to grasp everything quickly.
The best way to understand any platform is to use it. So here is the step-by-step guide on how to set up HF version 1.2 on local machine. The system I am using has the following configuration:
Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0–134-generic x86_64)
RAM: 4 GB
We are going to execute every command with root privileges, so first we type
sudo su
HF is written in Go language. Go version 1.10.x is required, so any previous installation of it needs to be removed. If there is no previous installation of Go available, then command 3 and 4 can be skipped
apt-get update
apt-get -y upgrade
apt-get purge golang*
rm -rf /usr/local/go/
curl -O https://storage.googleapis.com/golang/go1.10.3.linux-amd64.tar.gz
sudo tar -xvf go1.10.3.linux-amd64.tar.gz
sudo mv go /usr/local
vim ~/.bashrc
We need to update the environment variables in bashrc. Open the bashrc file using vim (or any of your favourite editor ;) ) and insert the following lines. If you are using vim, then press i to enter in insert mode, copy the content, once you’re done press Esc, then :wq to save and quit.
export GOROOT=/usr/local/go
export GOPATH=/root/gopath/
PATH=”$HOME/bin:$HOME/.local/bin:$PATH:$GOROOT/bin”
export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric/build/bin/
Do not worry about PATH and GOPATH variables, we will create the directory structure soon. To make the changes reflect, use the following command. You can check if the installation is done properly using command 2 and 3.
source ~/.bashrc
go version
go env
Other prerequisites include Docker and Docker compose. Run the following commands on the terminal for Docker Compose version 1.22.0 (1.14.0 or greater will work)
sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-composesudo chmod +x /usr/local/bin/docker-composedocker-compose --version
Output: docker-compose version 1.22.0, build 1719ceb
Docker version 17.06.2-ce or greater is required
sudo curl -sSL https://get.docker.com/ | sh
sudo apt-get update && sudo apt-get upgradedocker --version
Output: Docker version 18.06.1-ce, build e68fc7a
Next we create a directory structure, download fabric from Github and switch to branch of release-1.2. Note that we had set our GOPATH to /root/gopath which will contain all our Go projects.
cd /root
mkdir -p gopath/src/github.com/hyperledger
cd /root/gopath/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric.git
cd fabric
git checkout release-1.2
Last command can be skipped if you want to stay on master branch. Now to create docker images required to run Fabric applications, we run the following command inside the fabric folder. If make is already installed, first command can be skipped.
sudo apt install make
make docker
It can sometimes give the following error:
fatal error: ltdl.h: No such file or directory
To fix this, we run the following command
sudo apt install libltdl-dev
Another possible error is:
cp: cannot stat '.build/docker/gotools/bin/protoc-gen-go': No such file or directory
Makefile:278: recipe for target '.build/image/ccenv/payload' failed make: *** [.build/image/ccenv/payload] Error 1
To fix this, we need to download protoc-gen-go, and copy it at .build/docker/gotools/bin:
go get -u github.com/golang/protobuf/protoc-gen-go
cp $GOPATH/bin/protoc-gen-go .build/docker/gotools/bin/
I also got the following error:
.build/bin/cryptogen
CGO_CFLAGS=" "
GOBIN=/root/gopath/src/github.com/hyperledger/fabric/.build/bin go install -tags "" -ldflags "-X
github.com/hyperledger/fabric/common/tools/cryptogen/metadata.CommitSHA=a24152b" github.com/hyperledger/fabric/common/tools/cryptogen
# runtime/cgo
exec: "gcc": executable file not found in $PATH
Makefile:256: recipe for target '.build/bin/cryptogen' failed
make: *** [.build/bin/cryptogen] Error 2
The solution is to install the gcc/g++ compilers and libraries using the following command:
apt-get install build-essential
After make docker command is successful, we can run the command docker images and should get the following output
Now our fabric installation is complete. We can run any Fabric application written in Go/JAVA/NodeJS on top of it. A good starting point would be to experiment around with fabric-samples.
These steps should work for higher versions of Hyperledger Fabric too. If you face any issue in above steps, feel free to drop a comment, I bet I would have faced all the issues ;)