Set up your development environment for Ethereum on a Mac

Set up your development environment for Ethereum on a Mac


In this lecture, we are going to set up
our development environment for MacOS
with all the tools that we need to
develop smart contracts for Ethereum. We
are not going to describe all of them
but focus on those that are widely used
by the community. By the way, this video
is specifically about setting up your
environment on MacOS. If you are running
on Windows or Linux, there are dedicated
lectures for that. Now our tool box will
be composed of the following tools: Geth,
Ganache, Truffle and Atom. But before we
can install those, there are a few
prerequisites that we need to deal with.
Maybe some of these are already set up
on your machine, and in that case you can
just skip forward. But in case they are
not, we’ll show you how to install them. The first tool that we will need is
HomeBrew. If you are a Mac user, it is
highly recommended to use HomeBrew.
HomeBrew is a package manager for MacOSX that helps you install and update
packages that are not natively provided
by MacOS, like NodeJS and Go-Ethereum.
HomeBrew might already be installed on
your Mac. If not, I’m going to show you
how to proceed.
But before installing homebrew on your
Mac, you have to install Xcode, that is
available from the App Store. So if you
already have Xcode, again, you can skip
forward. But if not, you need to install
it from the App Store. And to do that…
I’ll put the link of Xcode in the App
Store in the resources of this lecture,
but you can also search for it in the
App Store. And that’s the one you need to
install, and just click the install
button and wait for it to complete. Now,
it’s a 5.5-gigabyte package
so it’s gonna take a while. We’ll come
back when it’s done. Once Xcode is
installed, we can go to applications and
start Xcode, which is important because
you need to accept the License Agreement,
otherwise HomeBrew will refuse to
install. Here, I already accepted this
License Agreement before so I don’t need
to do it again. The one thing I need to
do though is to install command-line
tools, the
Xcode command-line tools. Because those
tools include some compilers and other
utilities that are going to be needed
for HomeBrew to do its job. So I will
just quit Xcode, open a terminal and type
“xcode-select –install”. And here it’s telling me that command-line tools
are already installed, so I don’t need to
install them again. But otherwise you
should see a confirmation dialog where you
need to click OK, and then go through the
installation of all the command-line
tools. Once this is done, now we can
finally install HomeBrew. And to do that
you need to go to “brew.sh”. Again I’ll
add all the links to those sites in the
description of this lesson. And here in
the middle of the page you have a
comment that we need to copy and paste
in a terminal. We can run this, I need to
input my administrator password. Now it’s
downloading and installing HomeBrew. I
close the browser for now. And in the end
you should see this message: “installation
successful”. Now just to be sure, I will
clean up my terminal a little bit, and
just type “brew –version”, to check
which version I have. You should have the
latest one normally. Now that HomeBrew is
installed, let’s use it to install Geth.
Now Geth, or Go-Ethereum is the command
line interface that allows you to run
and operate a full Ethereum node. Geth
is implemented in Go, and allows you to
mine blocks, to generate Ether, to deploy
and interact with smart contracts, to
transfer funds, to inspect block history,
to create accounts and do all the things
that are needed for managing an Ethereum
node. Geth can be used to connect to the
public
Ethereum network, also called the main
net, or to create your own private
network for development purposes, which
we will demonstrate later during this
course. The benefit of using Geth in your
development process is to let you fully
test your application before deploying
it to the live network. On a Mac, the
best way to install Geth is using
HomeBrew, which we just installed before.
The first thing we have to do is to
connect HomeBrew to the repository
providing Geth. To do that I will just run
“brew update”, to update the package
catalog for HomeBrew. And then I will
“brew tap ethereum/ethereum” to add the
Ethereum repository to the package
catalog. And once this is done, I can just
run “brew install ethereum”. In my case I
already had Ethereum 1.7.3 installed. At any
point in time, if you already have Ethereum installed, or any HomeBrew package
for that matter, the only thing you need
to do to upgrade is just to type “brew
upgrade” and the name of the package. So
always make sure that you have the
latest version of Ethereum, or Geth in that
case: that’s important. And we’ll start
using Geth in the next lecture, when we
create our first private node. The second
tool we need to install is Ganache. Now
Ganache is an Ethereum blockchain
emulator that you can use for
development purposes. Ganache can be seen
as a replacement for TestRPC, which we
used in previous versions of this course.
And the strength of Ganache is its UI,
that allows you to inspect blocks and
transactions in a much more
user-friendly way. Compared to Geth,
Ganache is an in-memory Ethereum node
that runs faster and provides all the
visual components you need to test your
contracts locally before deploying them
to a real Ethereum implementation. So
you can download a package to install
Ganache from “truffleframework.com/ganache” and normally the website should
detect which operating system you are on
and adapt this button
accordingly. Now we noticed that there is
an issue with the very latest version,
the one that was released just last
night, which is 1.0.2. So instead of
installing the latest version, we’ll
install version 1.0.1, which we know works.
And to do that I actually need to go to
“github.com/trufflesuite/ganache”.
Again, the link will be in the
description. And here I can go to
“releases”. I will not install 1.0.2 and I
will stick with 1.0.1. And here
you can see we have different packages
and I’m going to use the DMG one for MacOS.
So all I need to do is to download
this archive, open it up when it’s
downloaded and then install it
like any other Mac application. OK so I’m
opening the DMG package, and there all I
need to do is to drag and drop Ganache
to the Applications folder. Then when
it’s done, I go to the Applications
directory, and Ganache is right there.
Let’s run it the first time… and the
first time we run it, MacOSX will warn
us that this is not signed, that this is
from an unidentified developer, and we
need to accept it.
So first I accept it, and actually here
it starts right away because I already
had authorized it. If you get a warning
saying that you should modify your
security settings, then you click OK
and you go to System Preferences,
Security and Privacy, and in the general
tab you should have a button here that
says “open anyway”. And then you can click
this button and it will start Ganache
right away. Let’s clean things up a
little bit: this one goes down ok.
So this is the user interface for
Ganache. Now let’s review some of its
main features. When you start Ganache, it
starts a new Ethereum node in memory on
network identifier 5777, which you can see in this part of
the screen. It also creates 10 accounts
and puts 100 ethers on each
account. Now contrary to what happened
with TestRPC before, Ganache always
creates the same accounts, uses the same
mnemonic. So the first screen you see is
the Accounts view and it lists all the
accounts with their details. At the top
of the screen it also shows the current
block, which is the number of the last
block that was mined, the gas price, the
gas limit for each block, the network
identifier, the URL of the RPC endpoint
and the mining status. Now the gas price
is the minimum gas price this node
requires on transactions to mine them. The
gas limit is the maximum gas this node
is willing to accept per block. The
network ID is just a network
identifier. RPC server is the host and
port of the RPC endpoint this node is
listening to. The mining status is very
important because in the case of Ganache
it starts as “Auto mining” by default. So
by default, this fake node only mines a
new block when it receives a new
transaction, which is completely
different from what real nodes would do
since the normal node would mine a new
block at regular intervals, whether there
are pending transactions or not. You can
disable auto mining, come back to a more
normal behavior and even set a mining
block time in the settings. We’ll show
you how to do that later.
And finally, this mnemonic corresponds to
the seed words that are used to generate
the wallets of 10 accounts. And this one
is the same by default on all
installations. Then you can see a list of
10 accounts generated by Ganache with
the following information: the wallet
address of the account, its balance
with only two decimals, the number of
transactions that were
sent by this account, the index of the
account in the list of accounts, and
finally a button to show the private key.
And this will be very practical to
import the accounts later in MetaMask.
Then you’ve got the “Blocks” tab, and here
you can see all the blocks that are
mined by the node. And of course, since we
just started it, we can only see the
genesis block, and it doesn’t contain any
transaction. In the “Transactions” tab, we
can see a list of all the transactions,
and there aren’t any for now. And in the
“Logs” tab, we can see the console log of
the node. There’s also a search field,
where you can search transactions and
blocks by their numbers or hashes. And
finally there’s the “Settings” button that
gives you access to the settings. So here
you can change the host name, the port
number and the network identifier. We’ll
actually use that part quite often. You
can also change the way that the miner
works: by default it’s auto mine, but you
can also deactivate auto mining and just
specify a mining block time. We’ll leave
auto mining on. “Accounts and keys” allows
you to set the number of accounts that are
generated, and also the mnemonic that is
used as a seed for the account
generation. In “Chain”, we can specify a
custom gas limit and gas price. And in
“Advanced” we can enable or disable Google
Analytics. And that’s it. The only thing
you have to remember is that whenever
you change something, when you change
something in settings, you have to save
and restart to make sure that the
new settings are taken into account.
Here we’ll just cance. Let’s close
Ganache for now. The next thing we need
to install is NodeJS and NPM. Now NodeJS
is a server-side JavaScript platform to
create applications that will interact
with an Ethereum node. When we install
NodeJS, we also install the Node
Package Manager, or NPM, that is required
to set up most of the tools and libraries
required to develop for Ethereum. To check
if NodeJS and NPM are already installed on
your machine, you can
go to the Terminal and run “node -v”
and “npm -v”. So here we can see
that Node 9.3.0 is already installed on
this machine, and NPM 5.6.0 is already
installed. If you have older versions of
those tools you should upgrade to
at least this version, because we tested
all our content with these versions, so
we can’t guarantee that older versions
will work. So if you still remember how
you installed NodeJS, because there are
many ways, then you should use the same
technique to upgrade. If you don’t
remember, or if you want to start from a
clean installation, here is a link that
you can use to see documentation about
how to completely clean up your system,
remove all traces of NodeJS and NPM, and
then we’ll be able to reinstall it
from scratch.
Basically, all that’s required to install
NodeJS on a new system is to run
“brew install node” and this will install
NodeJS and NPM in the latest version
available in HomeBrew. So here it’s
telling me that 9.3.0 is already
installed. The next tool we need to
install is Truffle 4. Now, to compile,
test and deploy your smart contracts, you
will need a build framework, that will
increase your development velocity
compared to doing it manually. We’ll
demonstrate that later in this course. In
this course we demonstrate the use of
Truffle 4, which you can find on this
website: “truffleframework.com”. This
version of Truffle embeds “truffle
develop”, a development blockchain that
you can use to speed up your automated
testing and your development lifecycle.
In this course we’ll use both a personal
Ethereum node provided by Ganache, and
“truffle develop”, to let you decide
which one would be more suitable for you.
If you have a previous version of
Truffle, please uninstall it before. If
you have Truffle 3, you need to remove it
from your system, otherwise it will
collide with the new version. So in the
Terminal,
to remove Truffle, you can run “npm
install -g truffle”, and this will
remove the old version of Truffle. And
then, to install the new versio, we’ll run
“npm install -g truffle”, but instead of
installing the latest version, we’ll
install version 4.0.4. The goal of this is
to avoid any breaking change in newer
versions that might not work with this
course content. And what’s it’s done, you
can run “truffle version” to check which
version is installed. And here you can
see I have version 4.0.4, and version 0.4.18 of the Solidity javascript compiler.
If you see a message about “checkPermissions missing write access to
/usr/local/lib/node_modules”, the most likely
reason is that you installed NodeJS with
the installable package from the node.js
website, and this does not set the
permissions on your system in the right
way. And this is a link of the
documentation that you can use to fix
NPM permissions. Once you have fixed
these permissions, you can try again to
install truffle with the same command,
and it should be good.
The last step of this lecture is to
install a text editor that will be used
to edit our smart contracts and other
text files. At this point in time,
unfortunately, we don’t have a real
Ethereum IDE. You can find some plugins
for IntelliJ Webstorm or Microsoft
Visual Studio, but not a full IDE
covering all the development chain. But
that’s not a big deal, because we have
some interesting free text editors that
are good enough for what we want to show here. In this course we chose to use Atom
because it’s freely available for Mac,
Windows and Linux. But of course, you are
free to use the one that you are more
familiar with. To install Atom, you can
just go to this website: “github.com/atom/atom”.
And here you go to “releases”. We
won’t install the beta version, but
instead we’ll install the latest stable
release, and that’s 1.23.2. Here we have
packages for a lot of different
operating systems. We are on a Mac so
we’ll install the “atom-mac.zip”. So
I’m downloading this file. Again, it might
take a while, because it’s 129 megabytes.
And once the package has been downloaded,
we can open it and simply drag and drop
to the Applications directory. Once it is
there, we can start it up the first time,
confirm the opening, and voila! Now in the
Atom menu you will find a special menu
item called “install shell commands”. This
is required to install the “atom” and “apm”
commands in the Terminal, and that will
be very useful. So here it confirms that
the shell commands “atom” and “apm” are
installed. I can quit Atom for now, go
back to the Terminal, and I will use APM
to install the Atom plugin to support
the Solidity language. So “apm install
language-ethereum”. And this just
installed the extension in Atom for
Solidity support. Now at this stage we
have all the tools that we need to start
developing our smart contracts on a Mac.
You can quit Atom, Ganache, Terminal and
all the running tools. We’ll start using
them when we need them. In the next
lecture, we will show you how to set up
and start a private Ethereum node.

One thought on “Set up your development environment for Ethereum on a Mac

Leave a Reply

Your email address will not be published. Required fields are marked *