Setting up geth Ethereum node to run automatically on Ubuntu

1 minute read

For a recent project I’m working on I needed to get geth running as a background service on Ubuntu so that I could connect to it via HTTP-RPC and obtain blockchain information. In this post I will outline the steps I took to make this work on my Digital Ocean droplet.

I created a $5 per month droplet (512 MB RAM) and setup 1 GB of swap space on it as follows:

$ fallocate -l 1024MB /swapfile  
$ chmod 600 /swapfile  
$ mkswap /swapfile  
$ echo '/swapfile none swap sw 0 0' >> /etc/fstab  
$ /sbin/shutdown -r now  

That sets up swap space and reboots the server. Next let’s install the supervisor daemon, which will be responsible for auto-starting geth at bootup and restarting it if it goes down:

$ apt-get install supervisor  

Now let’s install geth using the official instructions:

$ apt-get install software-properties-common  
$ add-apt-repository -y ppa:ethereum/ethereum  
$ apt-get update  
$ apt-get install geth  

Finally we have to add a configuration file to supervisor to let it know that it should run geth, Create /etc/supervisor/conf.d/geth.conf and fill it with:

command=/usr/bin/geth --fast --rpc --rpcaddr --rpcport 8545 --rpccorsdomain * --rpcapi admin,db,eth,debug,miner,net,shh,txpool,personal,web3

Note that we use geth’s --fast option to ensure the initial sync of the blockchain happens as quickly as possible. The downside of this is that you won’t have the full data for the entire historical blockchain, just the most recent stuff and for new incoming blocks. The command also enables RPC access to this geth instance, so that our dapps can connect to it. If you do this on a public hosted server I highly recommend setting firewall rules (I use arno-iptables-firewall) to prevent unauthorized access to geth’s RPC server.

Now just restart supervisor to startup geth:

$ supervisorctl reload  

Boom! You can now attach a console to your running geth instance by typing geth attach. You can then query it using the standard commands.

Leave a Comment