Table of Contents
In this article, I will take you through the steps to Install GO Using APT GET on Ubuntu 18.04 . Go, sometimes referred to as Golang to make it easier to find on the web, is a statically typed and compiled open source programming language initially developed by Google. Robert Griesemer, Rob Pike, and Ken Thompson were attempting to create a language for modern systems programming that solved real-world problems they encountered while building large systems at scale.
Easy Steps to Install GO Using APT GET on Ubuntu 18.04
Also Read: Easy Steps to Install GO Using YUM On CentOS 7
Step 1: Prerequisites
a) You require a running Ubuntu 18.04 System.
b) You need to have root access or User with sudo access to run all the privileged commands. In this article, I ran all the commands through root user to make things simple and straight. You can use any other user with sudo access to execute all the commands. It really does not matter. To provide sudo access to User, you can check How to add User to Sudoers on Ubuntu 18.04
c) You should have apt-get utility available in your System.
Step 2: Update Your System
Before going through the steps to install GO in your system, you need to update your system by running apt-get update
command as shown below.
root@localhost:~# apt-get update Hit:1 http://in.archive.ubuntu.com/ubuntu bionic InRelease Get:2 http://in.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB] Get:3 https://artifacts.elastic.co/packages/7.x/apt stable InRelease [7,124 B] Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB] Get:5 http://in.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB] Get:6 http://in.archive.ubuntu.com/ubuntu bionic-updates/main i386 Packages [650 kB] Get:7 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [871 kB] Get:8 http://security.ubuntu.com/ubuntu bionic-security/main i386 Packages [442 kB] Get:9 https://artifacts.elastic.co/packages/7.x/apt stable/main i386 Packages [23.2 kB]
Step 3: Install GO Using APT GET
After successful update, it is now time to install Golang package using apt-get install golang-go
command as shown below.
root@localhost:~# apt-get install golang-go Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: build-essential dpkg-dev fakeroot g++ g++-7 gcc gcc-7 golang-1.10-go golang-1.10-race-detector-runtime golang-1.10-src golang-race-detector-runtime golang-src libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan4 libatomic1 libc-dev-bin libc6-dev libcilkrts5 libdpkg-perl libfakeroot libgcc-7-dev libitm1 liblsan0 libmpx2 libquadmath0 libstdc++-7-dev libtsan0 libubsan0 linux-libc-dev make manpages-dev pkg-config Suggested packages: debian-keyring g++-multilib g++-7-multilib gcc-7-doc libstdc++6-7-dbg gcc-multilib autoconf automake libtool flex bison gcc-doc gcc-7-multilib gcc-7-locales libgcc1-dbg libgomp1-dbg libitm1-dbg libatomic1-dbg libasan4-dbg liblsan0-dbg libtsan0-dbg libubsan0-dbg libcilkrts5-dbg libmpx2-dbg libquadmath0-dbg bzr mercurial subversion glibc-doc libstdc++-7-doc make-doc The following NEW packages will be installed: build-essential dpkg-dev fakeroot g++ g++-7 gcc gcc-7 golang-1.10-go golang-1.10-race-detector-runtime golang-1.10-src golang-go golang-race-detector-runtime golang-src libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan4 libatomic1 libc-dev-bin libc6-dev libcilkrts5 libfakeroot libgcc-7-dev libitm1 liblsan0 libmpx2 libquadmath0 libstdc++-7-dev libtsan0 libubsan0 linux-libc-dev make manpages-dev pkg-config The following packages will be upgraded: libdpkg-perl 1 upgraded, 34 newly installed, 0 to remove and 177 not upgraded. Need to get 1,022 kB/67.4 MB of archives. After this operation, 342 MB of additional disk space will be used. Do you want to continue? [Y/n] y ............................................
Step 4: Check GO Version
Once installation is completed, you can check the installed GO version by running go version
command as shown below.
root@localhost:~# go version go version go1.10.4 linux/amd64
Step 5: Write Your First Program
Once GO is installed successfully, you can go ahead and write your first Hello World
program as shown below. Here we have used package main
keyword which makes this program to compile as an executable program. We are also importing fmt package from GO Standard Library using import "fmt"
keyword to use printf
function for displaying the output.
package main
import "fmt"
func main() {
fmt.Printf("hello, world\n")
}
Step 6: Build Your Program
Once you written your program, it is now time to build your program by using go build
command as shown below. Please note that i have created a directory hello and written my hello.go program inside that.
root@localhost:~/hello# go build
After the successful build, you can see one executable file hello
got created.
root@localhost:~/hello# ls -lrt total 1980 -rw-r--r-- 1 root root 73 Feb 22 15:19 hello.go -rwxr-xr-x 1 root root 2020012 Feb 22 15:21 hello
Step 7: Run Your Program
Now you need to run your program by using ./hello
executable. You can see hello, world
in the output.
root@localhost:~/hello# ./hello hello, world
Step 8: Alternative way to run your Program
There is another way to run your program without specifically issuing go build
command like we did in Step 6. You just have to run go run hello.go
command to compile and execute your program. In a production system, usually go build
command is often recommended to build a large number of codes in numerous files. This method is only suitable for using in small programs.
root@localhost:~/hello# go run hello.go hello, world
As you can see from below output, there is no hello executable got created like we had the case in Step 6.
root@localhost:~/hello# ls -lrt total 4 -rw-r--r-- 1 root root 73 Feb 22 15:19 hello.go
Reference: GO in Practice