I tried the online editor of Liquidity to program my first Tezos smart contracts, which is really nice to get familiar with smart contract programming on Tezos. To move the efficiency of the smart contracts of Tezsure to a higher level I wanted to program our contracts in Michelson, unfortunately I couldn’t find a complete setup guide on how to set up a development environment, including a localhost instance of a Tezos network and Emacs with the Michelson mode.

I was personally having trouble with setting this up, so to help any Tezos enthusiasts out there I wanted to write this blogpost.

Note: this is my way of setting up the developer environment, there are probably a lot of ways to this. I combined parts of the Zastrin course, the Tezos documentation, the tutorial of Kate Sills and my own trial and error.

Prerequisites

This tutorial is written for Debian 9. I have tried setting it up with Ubuntu LTS, but I kept getting different errors. I am not saying it is impossible to set it up that way, I just couldn’t make it work.

If you use the Windows operating system, you can use VirtualBox to set up a Debian 9 virtual machine. There are plenty of tutorials out there in the wild that will teach you how to do this. If you are completely new to virtual machines, follow along this video.

Installing Tezos

The first step is to update and install all the latest packages on your machine. Open a terminal and use the following commands.

sudo apt-get update

sudo apt-get upgrade

Now we need to download and install Opam. This is a source-based package manager for OCaml.



sudo mv opam-2.0.0-x86_64-linux /usr/local/bin/opam

sudo chmod a+x /usr/local/bin/opam wget https://github.com/ocaml/opam/releases/download/2.0.0/opam-2.0.0-x86_64-linux sudo mv opam-2.0.0-x86_64-linux /usr/local/bin/opamsudo chmod a+x /usr/local/bin/opam

Check if opam is installed with:

Opam --version

This will show the following:

Now lets initialize the compiler with the following command:

opam init --compiler=4.06.1

As you can see this gives me an error message. We should install bubblewrap .

sudo apt-get install bubblewrap

Now try to run the previous command again with:

opam init --compiler=4.06.1

The initialization will start and during this you need to answer a few questions, just answer these with the default answers by pressing enter. This process will take a couple of minutes to complete.

Now update your PATH accordingly.

eval $(opam env)

After setting up the compiler we can download and install the Tezos software in your preferred location. We are using the alphanet branch. (When you have a fresh install of Debian 9 you need to install git before continuing with `sudo apt-get install git`)



cd tezos/

make build-deps git clone -b alphanet https://gitlab.com/tezos/tezos.git cd tezos/make build-deps

Not all packages can be installed, because we miss some. Luckily the terminal shows which system dependencies need to be installed.

sudo apt-get install libev-dev

sudo apt-get install libgmp-dev

sudo apt-get install m4

sudo apt-get install pkg-config

After installing these dependencies we can try to run our previous command.

make build-deps

Unfortunately we still get an error. This time we are missing one other package:

Lets install that one as well:

sudo apt-get install libhidapi-dev

third time’s the charm!

make build-deps

All the dependencies are now installed. Update your PATH again and use the final command make to create all the binaries.

eval $(opam env)

make

Setting up a local Tezos network

Everything that we need is installed, with two extra commands we can start a alphanet node and sync it to the alphanet. For this tutorial that’s not necessary. We just need a local Tezos network for testing purposes. The Tezos docs provide a guide on how to do this which gives a more in depth description on what happens at every step.

Open a terminal and make sure you are in the tezos directory. With the following command you can initialize the first node.

./src/bin_node/tezos-sandboxed-node.sh 1 — connections 1

To launch the second node, open a new terminal and make sure you are in the tezos directory.

./src/bin_node/tezos-sandboxed-node.sh 9 — connections 1

Now we have two nodes running locally! To initialize the client data in a temporary directory we need to open a third terminal and use the following command.

eval `./src/bin_client/tezos-init-sandboxed-client.sh 1`

Note: this command sets the current terminal session with the alias tezos-client . That is something that I didn’t realize when I started.

When you bootstrap a new network, the network is initialized with a dummy economic protocol, called genesis. We want to run the same protocol as the alphanet, for that we need to execute one extra command (in the terminal with the `tezos-client` alias session).

tezos-activate-alpha

We now have the possibility to send transactions to the sandboxed network. We can use the preconfigured accounts which can be listed with the following command (in the terminal with the tezos-client alias session).

tezos-client list known addresses

We can check the balance for example of address bootstrap1 with:

tezos-client get balance for bootstrap1

To send a transaction use:

tezos-client transfer 42 from bootstrap1 to bootstrap2 &

You will notice that this command doesn’t terminate. It is waiting for the network to include the transaction in a block. Given that we are in a sandbox we need to bake a block ourselves and we can do so with the following command:

tezos-client bake for bootstrap1

The operation is included in the chain!

Set Up Michelson Mode in Emacs

The final step is to install Emacs. This is a very easy command:

sudo apt-get install emacs

Emacs can be a challenging tool to use. It uses commands that I struggled with at first.

Note: You’re going to see references to Emacs commands that look like C-[character] or M-[character] . C is the ctrl key on your keyboard. M is the altkey on your keyboard. You should hold the ctrl or alt key and then press whatever character the command uses.

We need to add a repository to the Emacs initialization file. Open up Emacs, using the terminal command:

emacs

Use the following commands to use the find file function:

c-x

c-f

Then open the .emacs file, as shown in the image below.

~/.emacs

Add the following to this file:

(require ‘cl)

(require ‘package)

(let* ((no-ssl (and (memq system-type ‘(windows-nt ms-dos))

(not (gnutls-available-p))))

(url (concat (if no-ssl “http” “https”) “://melpa.org/packages/”)))

(add-to-list ‘package-archives (cons “melpa” url) t))

(when (< emacs-major-version 24)

;; For important compatibility libraries like cl-lib

(add-to-list ‘package-archives ‘(“gnu” . “https://elpa.gnu.org/packages/")))

(package-initialize)

Save with the c-x c-s command and restart Emacs.

We need to install the deferred and exec-path-from-shell packages. The packages can be installed from within Emacs. The following command will trigger the terminal at the bottom of the Emacs screen.

M-x

After that, write the following and press enter:

package-install

Now install the package `deferred` and `exec-path-from-shell`.

The last step is to set up the Michelson mode by adding in to the .emacs file.

c-x

c-f

Then open the .emacs file with:

~/.emacs

Add this at the bottom of the file:

(load “~/tezos/emacs/michelson-mode.el” nil t)

(setq michelson-client-command “tezos-client”)

(setq michelson-alphanet nil)

Now your file should look like this:

Save with the c-x c-s command and close Emacs.

Note: To use Emacs with the tezos-client it is important to mention that you should open Emacs from the terminal in where you initialized the client data in.

This was the terminal with the session with an alias tezos-client . This is very important, otherwise the Michelson mode will not work!

To use the Michelson mode, open Emacs from the terminal within the tezos-client session.

emacs

Now you can open your preferred .tz file. The folder ~/tezos/src/bin_client/test/contracts contains test contracts to play around with.

When moving the cursor on a Michelson instruction, in the bottom of the window, Emacs should display the state of the stack before (left) and after (right) the application of the instruction. The Emacs mode automatically type-checks your program and reports errors.

Thanks for reading! Follow us on Twitter and keep spreading the word about Tezsure and Tezos! Share your feedback and suggestions to us by sending an email to bernd@tezsure.com.