paint-brush
How to Set Up Aleph.im Virtual Machinesby@stephenfiser
503 reads
503 reads

How to Set Up Aleph.im Virtual Machines

by StephenOctober 25th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Decentralized Virtual Machines are a decentralized network that has much of the functionality of AWS. Until recently, you could primarily use Aleph for data storage and file storage. When you deploy a service onto the network, you are putting your service into a decentralized context where no one person or entity can take it offline. The Aleph VMs currently work best (maybe exclusively) on up-to-date Linux distributions. The easiest way to run a Linux distribution if you're not actually on a Linux is with Vagrant.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to Set Up Aleph.im Virtual Machines
Stephen HackerNoon profile picture

Nearing the end of 2021, most blockchain dApps still have large parts of their stack that are centralized.

The smart contracts that power most DeFi protocols tend to be the only really decentralized aspect.

Most applications still have servers running on services like AWS that power their frontend user interfaces or process backend data and stats.

One of the main tenants of cryptocurrency and blockchain is "censorship resistance".

Platforms like Quickswap and Hot Potato are great, but what happens if Amazon decides to turn off the servers powering the actual app that users are using?

Introducing Decentralized Virtual Machines

Aleph.im is a decentralized network that has much of the functionality of AWS. Until recently, you could primarily use Aleph for data storage and file storage.

In the past months, they've started rolling out decentralized virtual machines.

For now, the most straightforward thing you can do is deploy simple Python or Javascript services. When you deploy a service onto the network, you are putting your service into a decentralized context where no one person or entity can take it offline. 

This adds another layer of decentralization to your application.

Getting Set up

The Aleph VMs currently work best (maybe exclusively) on up-to-date Linux distributions.

In my opinion, one of the easiest ways to run a Linux distribution if you're not actually on a Linux is with Vagrant. Here's a quick video walkthrough if you'd like to get Vagrant set up. If you don't want to do this, you can skip ahead to where we install the Aleph Python client.

As far as which Linux distribution to choose, I went with Debian 11, which has worked great.

$ vagrant init boxomatic/debian-11

Once you have a box installed that you want to use, you need to log into it.

$ vagrant up
$ vagrant ssh

Once you are logged into the Linux box, your terminal should look a bit different (and probably give you some kind of welcome message).

Now, we need to install the dependencies that we need.

$ sudo apt-get install -y python3-pip libsecp256k1-dev squashfs-tools

This installs Python 3 and a couple of other tools.

If you're following this path of using Vagrant, then you may also want to do the following for convenience.

$ cd /vagrant
$ mkdir app
$ cd app

💡 With Vagrant, anything we put inside of 

/vagrant
 will show up inside our Vagrant virtual machine as well as our local file system. This is really handy if you want to use your normal IDE or text editor for example.

Next, we need to install the Aleph Python client as well as a few other things like the FastAPI framework.

$ pip3 install uvicorn[standard] aleph-client fastapi eth_account

Our First Program

The next thing we need to do is get a program working locally that we want to deploy to Aleph.

In the official Aleph tutorial, you can find the following code. You should put it in a file called main.py directly inside the app folder we created.

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
  return {"message": "Hello World"}

In addition, in order to get this to work with Vagrant, we need to add the following to our Vagrantfile.

Vagrant.configure("2") do |config|
  config.vm.forwarded_port, guest: 8000, host: 8000
end

This just tells the VM to forward port 8000 of our Vagrant virtual machine to port 8000 on localhost. You'll see why this is important in the next bit.

Now, we want to actually start our program.

From 

/vagrant/app
, while logged into the virtual machine, we want to run

$ python3 -m uvicorn main:app --reload --host=0.0.0.0

Now, if you visit http://0.0.0.0:8000 in your browser, you should see

{
  "message": "Hello World"
}

So far, we've basically just set up an ultra-simple JSON API and ran it on our own system.

Next, let's deploy to Aleph.

Deploying Our Program

Now that we have a simple Python program running, we can deploy it to the Aleph network.

First, let's make sure the Aleph client is successfully installed.

$ aleph --help

You may get a warning about 'magic' not being installed, and that's fine to ignore.

But you should see a list of available commands.

In order to deploy, let's move up one level in our directories.

$ cd ..

We can deploy our program to the Aleph network with one simple command now.

$ aleph program ./app main:app

It will prompt you for a few things in your command line.

You can just hit "enter" to accept all the defaults.

If it succeeds, you'll get back two URLs.

One of them will look something like 

https://aleph.sh/..../

The other will be 

https://[.....].aleph.sh/

Visiting either URL should give you the same result. I *think* the second option allows you to set up custom domains and etc, but I will write a new article or post an update on that once I know more.

If you'd like to see this entire tutorial in video form (with more detail and commentary), check here:

Conclusion

Decentralized VMs are a powerful new tool. Aleph is way ahead of the pack with this technology, and it is super promising.

That being said, if you dive into this interesting world, be aware that it's still an early-stage technology, and you may experience some bumps in the road.

We'll continue posting content here and on our Youtube channel as we experiment and learn more.

This article was first published here