How To Install Go on Debian 9

Introduction

Go, also known as golang, is a modern, open-source programming language developed by Google. Increasingly popular for many applications, Go takes a minimalist approach to development, helping you build reliable and efficient software.

This tutorial will guide you through downloading and installing Go, as well as compiling and executing a basic “Hello, World!” program on a Debian 9 server.

Prerequisites

To complete this tutorial, you will need access to a Debian 9 server and a non-root user with sudo privileges, as described in Initial Server Setup with Debian 9.

Step 1 — Downloading Go

In this step, we’ll install Go on your server.

First, install curl so you will be able to grab the latest Go release:

  • sudo apt install curl

Next, visit the official Go downloads page and find the URL for the current binary release’s tarball. Make sure you copy the link for the latest version that is compatible with a 64-bit architecture.

From your home directory, use curl to retrieve the tarball:

  • curl -O https://dl.google.com/go/go1.12.5.linux-amd64.tar.gz

Although the tarball came from a genuine source, it is best practice to verify both the authenticity and integrity of items downloaded from the Internet. This verification method certifies that the file was neither tampered with nor corrupted or damaged during the download process. The sha256sum command produces a unique 256-bit hash:

  • sha256sum go1.12.5.linux-amd64.tar.gz
Output
go1.12.5.linux-amd64.tar.gz aea86e3c73495f205929cfebba0d63f1382c8ac59be081b6351681415f4063cf go1.12.5.linux-amd64.tar.gz

Compare the hash in your output to the checksum value on the Go download page. If they match, then it is safe to conclude that the download is legitimate.

With Go downloaded and the integrity of the file validated, let’s proceed with the installation.

Step 2 — Installing Go

We’ll now use tar to extract the tarball. The x flag tells tar to extract, v tells it we want verbose output, including a list of the files being extracted, and f tells it we’ll specify a filename:

  • tar xvf go1.12.5.linux-amd64.tar.gz

You should now have a directory called go in your home directory. Recursively change the owner and group of this directory to root, and move it to /usr/local:

  • sudo chown -R root:root ./go
  • sudo mv go /usr/local

Note: Although /usr/local/go is the officially-recommended location, some users may prefer or require different paths.

At this point, using Go would require specifying the full path to its install location in the command line. To make interacting with Go more user-friendly, we will set a few paths.

Step 2 — Setting Go Paths

In this step, we’ll set some paths in your environment.

First, set Go’s root value, which tells Go where to look for its files:

  • nano ~/.profile

At the end of the file, add the following lines:

export GOPATH=$  HOME/work export PATH=$  PATH:/usr/local/go/bin:$  GOPATH/bin 

If you chose a different installation location for Go, then you should add the following lines to this file instead of the lines shown above. In this example, we are adding the lines that would be required if you installed Go in your home directory:

export GOROOT=$  HOME/go export GOPATH=$  HOME/work export PATH=$  PATH:$  GOROOT/bin:$  GOPATH/bin 

With the appropriate lines pasted into your profile, save and close the file.

Next, refresh your profile by running:

  • source ~/.profile

With the Go installation in place and the necessary environment paths set, let’s confirm that our setup works by composing a short program.

Step 3 — Testing Your Installation

Now that Go is installed and the paths are set for your server, you can ensure that Go is working as expected.

Create a new directory for your Go workspace, which is where Go will build its files:

  • mkdir $ HOME/work

Then, create a directory hierarchy in this folder so that you will be able to create your test file. We’ll use the directory my_project as an example:

  • mkdir -p work/src/my_project/hello

Next, you can create a traditional “Hello World” Go file:

  • nano ~/work/src/my_project/hello/hello.go

Inside your editor, add the following code to the file, which uses the main Go packages, imports the formatted IO content component, and sets a new function to print “Hello, World!” when run:

~/work/src/my_project/hello/hello.go
package main  import "fmt"  func main() {     fmt.Printf("Hello, World!\n") } 

When it runs, this program will print “Hello, World!,” indicating that Go programs are compiling correctly.

Save and close the file, then compile it by invoking the Go command install:

  • go install my_project/hello

With the program compiled, you can run it by executing the command:

  • hello

Go is successfully installed and functional if you see the following output:

Output
Hello, World!

You can see where the compiled hello binary is installed by using the which command:

  • which hello
Output
/home/sammy/work/bin/hello

The “Hello, World!” program established that you have a Go development environment.

Conclusion

By downloading and installing the latest Go package and setting its paths, you now have a system to use for Go development. To learn more about working with Go, see our development series How To Code in Go. You can also consult the official documentation on How to Write Go Code.

Additionally, you can read some Go tips from our development team at DigitalOcean.

DigitalOcean Community Tutorials

Weekly News Summary for Admins — 2019-05-31

Between Memorial Day in the US, Ascension Day in parts of Europe, and WWDC looming next week, this was a quiet news week.

Apple did have one more thing to get out before WWDC: the iPod touch was updated with the A10 processor.

Now, only the Mac Pro remains as a device that has not been updated in the last two years. (MacBook barely makes the two years limit with its last update in June 2017.)

In other news, the first “Scripting macOS” class took place this week. The attendees (and I) believe it went really well! You can still sign up for the next class here. If the next date doesn’t suite you, please use the contact form and let us know when you would like a class. We will be scheduling additional classes soon and your input will be considered.

If you would rather get the weekly newsletter by email, you can subscribe to the Scripting OS X Weekly Newsletter here!! (Same content, delivered to your Inbox once a week.)

Headlines

On Scripting OS X

News and Opinion

MacAdmins on Twitter

  • Graham Pugh: “IBM SPSSStatistics 26 still needs Java installed in order to get installed on Mac, but installs a JRE as part of the installation. How hard would it be to put the JRE in the installer itself?”
  • Timo Perfitt: “Right Click->Open to Install?… ” (Click for image)
  • Tom Bridge: “So, I’ve spent a little time with Mosyle + Google SSO + DEP tonight, and I gotta hand it to the team at @mosyle_biz : That’s a helluva beta. I can see that being HIGHLY useful.”

Bugs and Security

Support and HowTos

Scripting and Automation

Apple Support

Updates and Releases

To Listen

Just for Fun

Support

There are no ads on my webpage or this newsletter. If you are enjoying what you are reading here, please spread the word and recommend it to another Mac Admin!

If you want to support me and this website even further, then consider buying one (or all) of my books. It’s like a subscription fee, but you also get a useful book or two extra!

Scripting OS X

Janusworx: A Week of Python

Ok. Time to be a bit honest. As you folks know, I have been trying to learn programming using Python since June 2017, when I joined the 10th cohort of DGPLUG’s Summer Training.
And time and again, I have failed.
Not just with programming, but with most other projects I tried to do.

At the end of my rope I decided to just quit everything and considered (very seriously) a return to my old stressful career, thinking maybe that is all there is for me.

Two people saved me.

The first one was Kushal Das.
The man was absolutely bull headed about me being in the right place and that if I could do this.

The other was my better half.
Everyday I count my blessings and am thankful that I that she chose to share her life with mine.
She patiently listens to my frustrated rants and then tells me to just dust myself up and do it again.
That failure is not the end of the world.
And then she told me to do my physio.
And that I really could do this.

Just because you failed doesn’t mean you can’t succeed.
We all fail. Mentally resilient people realize that its not failure that defines your identity but how you respond.

Shane Parrish

So towards the end of last year I decided to focus only on one or two things at once.
And at that time it meant my 12th exams.
I studied really hard for three months.
And I did not finish studying.
And I am pretty sure I am going to bomb my exam results.

Then why do I sound so chirpy?

Because I realised Kushal and Abby were right.
That I can in fact learn.
The past four months have been an exercise in frustration.
But I learnt something new everyday.
I could test myself on what I learnt and realise that I did in fact know stuff.

Which led me to my lightbulb moment.
That I cannot do all my learning like those montages they show in movies.
All my learning came from stretching just a tiny bit, every day.
I learnt the basics of Accounts, and lots of Maths.

The difficulty of a task is irrelevant, if it’s vital to your success.

— Ed Latimore

And now that exams are done, I decided to turn my attention back to programming.
And so I made a big ask of Kushal.1
I decided to go to Pune, and try to pick up the basics of programming in Python all over again.
And he graciously volunteered to mentor me for a week.

And here I am a week later, writing all sorts of tiny little programs that do whimsical things and bringing me joy.
I obviously have miles to go before I can even grasp at fluency.
But this time, I am filled with hope and a good measure of confidence. It’s been a little nerve wracking and there’s been tonnes of head scratching and back stretching.
Kushal has been extremely patient with me, guiding me these past few days, making sure I stretch just the right amount.
And for that I owe him a mountain of gratitude.
Thank you so much Kushal! I hope to pay it forward someday!

I go back home now, hoping to keep up the momentum with small incremental, regular periods of work.
I will log progress on the dtw blog where I can rant and rave to my hearts content.
My main focus will not be on results though.
Just to stretch myself everyday.
Improve myself just that little bit every day.
And then look back one day and be amazed at how far I’ve travelled.

The way you train reflects the way you fight.
People say I’m not going to train too hard, I’m going to do this in training, but when it’s time to fight I’m going to step up.

There is no step up. You’re just going to do what you did every day.”

— Georges St. Pierre

Planet Python

EuroPython: PyData EuroPython 2019

We are happy to announce a complete PyData track at EuroPython 2019:

image

PyData EuroPython 2019

The PyData track will be part of EuroPython 2019, so you won’t need to buy an extra ticket to attend. 

The PyData track is run in cooperation with NumFocus.

There are two full tracks featuring more than 30 talks and 4 trainings:

  • 4 trainings on Monday and Tuesday (July 8-9)
  • 34 talks on Wednesday and Thursday (July 10-11)
  • no PyData talks on Friday (July 12)

The full program is available on our PyData EuroPython 2019 page.

If you’d like to attend PyData EuroPython 2019, please register for EuroPython 2019 soon.

Dates and Venues

EuroPython 2019 will be held from July 8-14 2019 in Basel, Switzerland, at the Congress Center Basel (CCB) for the main conference days (Wed-Fri) and the FHNW Muttenz for the workshops/trainings/sprints days (Mon-Tue, Sat-Sun).

Please see our website for more information.

Enjoy,

EuroPython 2019 Team
https://ep2019.europython.eu/
https://www.europython-society.org/ 

Planet Python

How To Use Certbot Standalone Mode to Retrieve Let’s Encrypt SSL Certificates on Debian 9

Introduction

Let’s Encrypt is a service offering free SSL certificates through an automated API. The most popular Let’s Encrypt client is EFF‘s Certbot.

Certbot offers a variety of ways to validate your domain, fetch certificates, and automatically configure Apache and Nginx. In this tutorial, we’ll discuss Certbot’s standalone mode and how to use it to secure other types of services, such as a mail server or a message broker like RabbitMQ.

We won’t discuss the details of SSL configuration, but when you are done you will have a valid certificate that is automatically renewed. Additionally, you will be able to automate reloading your service to pick up the renewed certificate.

Prerequisites

Before starting this tutorial, you will need:

  • A Debian 9 server with a non-root, sudo-enabled user and basic firewall set up, as detailed in this Debian 9 server setup tutorial.
  • A domain name pointed at your server, which you can accomplish by following “How to Set Up a Host Name with DigitalOcean.” This tutorial will use example.com throughout.
  • Port 80 or 443 must be unused on your server. If the service you’re trying to secure is on a machine with a web server that occupies both of those ports, you’ll need to use a different mode such as Certbot’s webroot mode or DNS-based challenge mode.

Step 1 — Installing Certbot

Debian 9 includes the Certbot client in their default repository, and it should be up-to-date enough for basic use. If you need to do DNS-based challenges or use other newer Certbot features, you should instead install from the stretch-backports repo as instructed by the official Certbot documentation.

Use apt to install the certbot package:

  • sudo apt install certbot

You may test your install by asking certbot to output its version number:

  • certbot --version
Output
certbot 0.28.0

Now that we have Certbot installed, let’s run it to get our certificate.

Step 2 — Running Certbot

Certbot needs to answer a cryptographic challenge issued by the Let’s Encrypt API in order to prove we control our domain. It uses ports 80 (HTTP) or 443 (HTTPS) to accomplish this. Open up the appropriate port in your firewall:

  • sudo ufw allow 80

Substitute 443 above if that’s the port you’re using. ufw will output confirmation that your rule was added:

Output
Rule added Rule added (v6)

We can now run Certbot to get our certificate. We’ll use the --standalone option to tell Certbot to handle the challenge using its own built-in web server. The --preferred-challenges option instructs Certbot to use port 80 or port 443. If you’re using port 80, you want --preferred-challenges http. For port 443 it would be --preferred-challenges tls-sni. Finally, the -d flag is used to specify the domain you’re requesting a certificate for. You can add multiple -d options to cover multiple domains in one certificate.

  • sudo certbot certonly --standalone --preferred-challenges http -d example.com

When running the command, you will be prompted to enter an email address and agree to the terms of service. After doing so, you should see a message telling you the process was successful and where your certificates are stored:

Output
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/example.com/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/example.com/privkey.pem Your cert will expire on 2019-08-28. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

We’ve got our certificates. Let’s take a look at what we downloaded and how to use the files with our software.

Step 3 — Configuring Your Application

Configuring your application for SSL is beyond the scope of this article, as each application has different requirements and configuration options, but let’s take a look at what Certbot has downloaded for us. Use ls to list out the directory that holds our keys and certificates:

  • sudo ls /etc/letsencrypt/live/example.com
Output
cert.pem chain.pem fullchain.pem privkey.pem README

The README file in this directory has more information about each of these files. Most often you’ll only need two of these files:

  • privkey.pem: This is the private key for the certificate. This needs to be kept safe and secret, which is why most of the /etc/letsencrypt directory has very restrictive permissions and is accessible by only the root user. Most software configuration will refer to this as something similar to ssl-certificate-key or ssl-certificate-key-file.
  • fullchain.pem: This is our certificate, bundled with all intermediate certificates. Most software will use this file for the actual certificate, and will refer to it in their configuration with a name like ‘ssl-certificate’.

For more information on the other files present, refer to the “Where are my certificates” section of the Certbot docs.

Some software will need its certificates in other formats, in other locations, or with other user permissions. It is best to leave everything in the letsencrypt directory, and not change any permissions in there (permissions will just be overwritten upon renewal anyway), but sometimes that’s just not an option. In that case, you’ll need to write a script to move files and change permissions as needed. This script will need to be run whenever Certbot renews the certificates, which we’ll talk about next.

Step 4 — Handling Certbot Automatic Renewals

Let’s Encrypt’s certificates are only valid for ninety days. This is to encourage users to automate their certificate renewal process. The certbot package we installed takes care of this for us by adding a renew script to /etc/cron.d. This script runs twice a day and will renew any certificate that’s within thirty days of expiration.

With our certificates renewing automatically, we still need a way to run other tasks after a renewal. We need to at least restart or reload our server to pick up the new certificates, and as mentioned in Step 3 we may need to manipulate the certificate files in some way to make them work with the software we’re using. This is the purpose of Certbot’s renew_hook option.

To add a renew_hook, we update Certbot’s renewal config file. Certbot remembers all the details of how you first fetched the certificate, and will run with the same options upon renewal. We just need to add in our hook. Open the config file with you favorite editor:

  • sudo nano /etc/letsencrypt/renewal/example.com.conf

A text file will open with some configuration options. Add your hook on the last line:

/etc/letsencrypt/renewal/example.com.conf
renew_hook = systemctl reload rabbitmq 

Update the command above to whatever you need to run to reload your server or run your custom file munging script. Usually, on Debian, you’ll mostly be using systemctl to reload a service. Save and close the file, then run a Certbot dry run to make sure the syntax is ok:

  • sudo certbot renew --dry-run

If you see no errors, you’re all set. Certbot is set to renew when necessary and run any commands needed to get your service using the new files.

Conclusion

In this tutorial, we’ve installed the Certbot Let’s Encrypt client, downloaded an SSL certificate using standalone mode, and enabled automatic renewals with renew hooks. This should give you a good start on using Let’s Encrypt certificates with services other than your typical web server.

For more information, please refer to Certbot’s documentation.

DigitalOcean Community Tutorials