Install and Configure Go/Golang on Raspberry Pi

Go/Golang is one of the hot programming languages while I’m typing this today.

Go is a programming language created at Google in 2009 by Robert Griesemer, Rob Pike and Ken Thompson. Go is a statically typed compiled, procedural language similar to C, with memory safety, garbage collection, structural typing, concurrency and other great features are bundled to make it better compared to other languages in the marketplace.

Docker, Kubernetes, Graphana, Hugo are some of the best apps written in Go. It has a robust set of libraries and app performance is better compared to other languages.

Today I’m starting my journey to learn Go/Golang and Google will be my mentor to install and setup Golang on my Raspbian OS/Raspberry Pi 3. To get the latest version use below steps instead of native package management tool such as apt.

Installation Steps:

  • Download the current stable version of Go available on the google’s official website. At the time of writing this tutorial, 1.12.4 is the stable version. Check the latest version here
cd ~ && curl -O https://dl.google.com/go/go1.12.4.linux-armv6l.tar.gz

Above command would change directory to your ‘Home’ directory and download Go compressed tar file using ‘Curl’

  • Extract compressed tar file and place it inside /usr/local directory. Please note root level access or sudo access required to perform this step.
sudo tar -C /usr/local -xzvf go1.12.4.linux-armv6l.tar.gz
  • Set Path variables to avoid typing complete path and in order to access binaries or libraries of Go by the Raspbian OS. Open ~/.profile, a hidden file located in your ‘Home’ directory. Use nano or vi or subl to edit the file (subl ~/.profile). Add below lines at the end of the file.
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
  • To take effect of the above changes made to the file ‘profile’, run the below command. The source command can be used to load any functions file into the current shell, script or a command prompt.
source ~/.profile
  • Create directory called ‘go’ in the ‘Home’ directory. All my codes are placed in the folder. Change the directory name as per choice but do change the GOPATH accordingly as mentioned above.
 mkdir $HOME/go 
  • Validate Go is working as expected or not by running below command
vb@pi:~ $  go version
go version go1.12.4 linux/arm
vb@pi:~ $

My first code in Go

Create a directory first_code in ‘go’ and write following content in the file and save it as first_code.go

mkdir -p $HOME/go/src/first_code 
{-p is used to create directory and its sub-directories at once}
package main

import "fmt"

func main() {
    fmt.Printf("My first code in Go Language!!!\n")
}

Now build and run the code. Change directory
to the first_code cd ~/go/src/first_code and run below commands

vb@pi:~/go/src/first_code $ go build
vb@pi:~/go/src/first_code $

vb@pi:~/go/src/first_code $ ./first_code
My first code in Go Language!!!
vb@pi:~/go/src/first_code $

Note:

Above steps are applicable to any Linux distributions just by changing the first step of downloading the compressed tar file. Change the architecture from arm6l to amd64 or as applicable to your hardware.

I am able to successfully setup GO on my Pi. Hope you’d also do the same and happy learning Go. If you have any issues or questions please mention them in the below comment section.

Advertisements

Let’s use letsencrypt

Few folks told me that while they try to access my website vinaybabu.in they get nasty message from browsers that its unsafe to browse through my website. That’s because I was using self-signed certificate to encrypt the internet traffic. I didn’t want to go with CA vendors who charge more than the cost of my #raspberrypi.

After googling got to know about letsencrypt which provides free trusted SSL certificates. I would like to thank below tech blog / link from techmint for installing letsencrypt on my pi. Only catch is that we need to renew certificate within 90 days.

letsencrypt-logo-horizontal

http://www.tecmint.com/install-free-lets-encrypt-ssl-certificate-for-apache-on-debian-and-ubuntu/

To automate renewal of letencrypt (certbot) certificate we can use below script taken from the link https://www.upcloud.com/support/install-lets-encrypt-apache/

 #!/bin/sh
if ! /usr/local/letsencrypt/letsencrypt-auto certonly -tvv --keep --webroot -w <web root folder> -d vinaybabu.in > /var/log/letsencrypt/renew.log 2>&1 ; then
   echo Automated renewal failed:
   cat /var/log/letsencrypt/renew.log
   exit 1
fi
apachectl graceful

Note: Do change the <web root folder> with actual path

Save the above script in /root folder. I’ve saved this script as auto_renew.sh

Finally, create a new crontab entry as under to run the script every 80 days. Threshold to renew Letsencrypt (certbot) certificate is 30 days.

#crontab -e <run as root>

0 0 */80 * * /root/auto_renew.sh

To check crontab entries use below command

#crontab -l <run as root>

That’s it! folks can access my website from any type of device or browser without any issues. I’ve got free trusted certificate and enabled secure and encrypted traffic while accessing my website.

2016-09-24-19_36_59-welcome-to-vinays-website

Thanks much @letsencrypt and @tecmint.

Disclaimers:

Image’s courtesy: letsencrypt.org

Squirrelmail IMAP 111 : Connection refused

After I started using K-9 Mail on my Oneplus 2, I hardly accessed webmail service hosted on my #RaspberryPi. I wanted to send an urgent note but when I tried to login, got a error message as under.

Error connecting to IMAP server: localhost 111 : Connection refused

As usual, I googled for hours in between my office work 😛 but could not find out the solution. I started rewinding myself to recall the changes made to the Pi, then suddenly remembered that a change was made to /etc/hosts by replacing ‘localhost’ with my server name…

Alas! I ran squirrelmail-configure command and updated server settings which resolved the issue.

Now I realize change management plays an important role in any level be it organization or personal use since the task of managing change is not an easy one!!

Happy Pi-ing!!

 

Personal Cloud Storage on Raspberry Pi

I’m a big fan of Dropbox and using its services from past few years. I’ve referred few of my friends there by increasing my personal cloud storage capacity to about 12 GB! Off late file syncing stopped because of lack of space in my Dropbox account. I wanted to sync photos and other important files from my phone to Dropbox and sync it on my personal laptop.

To avoid buying cloud storage, started googling for setting up personal cloud storage and sync tools. After reading multiple blogs and comparisons, I’ve decided to go with Seafile. Though Owncloud was very close competitor, I did prefer Seafile for client side encryption feature. We do not have apt package for Seafile yet, but installation was simple, thanks to the readily available script which takes care of install and configuration of Seafile. It took roughly 10 mins to setup on my Raspberry Pi.

Prerequisites:
a. Working Raspberry Pi running Raspbian OS
b. Install MySQL  > Follow steps from here https://pimylifeup.com/raspberry-pi-mysql-phpmyadmin/
c. Public IP or Dynamic DNS account to access Cloud from external network / internet

Installation Steps:
1. Download latest version of Seafile for Raspberry Pi from https://github.com/haiwen/seafile-rpi/releases

#wget https://github.com/haiwen/seafile-rpi/releases/seafile-server_xxx.tar.gz [Replace xxx with actual filename]
2. I’m using an external hard drive of size 500GB mounted as /usbstore. Change directory  #cd /usbstore
3. Copy downloaded seafile tarball

#cp seafile-server_xxx.tar.gz /usbstore [Replace xxx with actual filename]
4. Extract file

#tar -xvzf seafile-server_xxx.tar.gz
5. Change directory

#cd seafile-server*
6. Enable execution of script if not already

#chmod +x setup-seafile-mysql.sh
7. Run the script

#./setup-seafile-mysql.sh
8. Installation will start. Script will start collecting information required. Give a good name to your server 🙂 like “limitlesscloud”
9. When it prompts for IP address, Enter the IP address of Raspberry Pi
10. Ports 8000 and 8082 are used by Seafile. Accept the default ports unless you want to change
11. Creating admin account: Provide your email address for account name
12. Enter the password for the account
13. Database configuration: Choose option ‘1’ at the prompt to create new database for Seafile configuration
14. Since MySQL is running locally leave the default option “localhost” as is
15. By default username for the database will be ‘root’. Let’s change it as ‘seafile’
16. Enter password for ‘seafile’ database user account
17. Accept the default database names and continue
18. At the end; it prompts “Enter” to continue and showing the configuration details. Hit enter!
19. Congratulations!!! Seafile installation is completed. Unlike other software, Seafile wont start by itself
20. Start the Cloud Storage services

#cd /usbstore/seafile-server-latest && ./seafile.sh && ./seahub.sh
21. Install Seafile from playstore on Android and use the respecitve methods on other platforms (Sorry Windows ‘Smartphone’ users 😛 ). We do have clients for Linux, Mac and Windows…
22. Enter Public IP details http://10.11.12.13:8000 Provide Username user@somesite.com and Password
23. Create Library / Libraries and enable encryption with password protection
24. Now I’ve about 500GB of personal ‘secure’ cloud storage capacity with encryption enabled. Seafile is flexible to further configure it to suit my requirements.

Enjoy your personal cloud storage. I love my Raspberry Pi ~ Small but orotund!!!

Connect external USB HD to Raspberry Pi

 

I wanted to connect the external USB disk to backup logs, configuration files and other critical data which are running on Raspberry Pi internal SD card.

I was trying to connect external USB HD to Raspberry Pi. Though the LED was lit, disk was not detected by Pi. I felt something wrong with the disk so for testing it, connected USB disk to laptop. Disk was detected successfully and was able to read write some files to it. After googling for a while found that the root issue was inadequate power from Pi to USB Disk.

I bought an external powered USB Transcend TS-HUB3K from the local computer shop. It worked like a charm 🙂

Now, I’ve connected two USB disks instead of one and still two more ports left to connect USB stick or any other USB device without worries about lack of power supply from Pi.