Install golang and beego framework in linux ubuntu 16.04 LTS
Golang is a programming language developed by Google. Thanks to its versatility, simplicity and reliability, Golang has become one of the most popular programming languages in the open source community.
And Beego is a RESTful HTTP framework for the rapid development of Go applications including APIs, web apps and backend services with integrated Go specific features such as interfaces and struct embedding.
In this article, I will show you how to install the latest stable release of Golang and Beego framework on 64-bit and 32-bit Linux operating systems, including Ubuntu 16.04 LTS, and Debian 9 Stretch.
Step 1: Download and decompress the Golang 1.x.x archive
For 64-bit Linux operating systems
If you are using a 64-bit Linux operating system, including CentOS 7 x64, Ubuntu 16.04 amd64, and Debian 9 amd64, you need to download the 64-bit version of Golang as below:
mkdir golang_source
cd golang_source
wget https://storage.googleapis.com/golang/go1.x.x.linux-amd64.tar.gz
sudo tar -zxvf go1.x.x.linux-amd64.tar.gz -C /usr/local
Change 1.x.x to golang version you want install, for example 1.9.1 as below :
mkdir golang_source
cd golang_source
wget https://storage.googleapis.com/golang/go1.9.1.linux-amd64.tar.gz
sudo tar -zxvf go1.9.1.linux-amd64.tar.gz -C /usr/local
For 32-bit Linux operating systems
If you are using a 32-bit Linux operating system, including Ubuntu 16.04 i386 and Debian 9 i386, you need to download the 32-bit version of Golang as below:
mkdir golang_source
cd golang_source
wget https://storage.googleapis.com/golang/go1.x.x.linux-386.tar.gz
sudo tar -zxvf go1.x.x.linux-386.tar.gz -C /usr/local
Change 1.x.x to golang version you want install, for example 1.9.1 as below :
mkdir golang_source
cd golang_source
wget https://storage.googleapis.com/golang/go1.9.1.linux-386.tar.gz
sudo tar -zxvf go1.9.1.linux-386.tar.gz -C /usr/local
Note: You can always find the download link to the latest release of Golang on the official download page. And if you want to install another golang version, you can change “1.x.x” to another version, for example “1.8.3”
Step 2: Setup GOROOT
and PATH
environment variables:
echo 'export GOROOT=/usr/local/go' | sudo tee -a /etc/profile
echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a /etc/profile
source /etc/profile
you can test golang installation with :
go version
go env
Step 3: Setup GOPATH
and PATH
environment variables (for bee tool):
mkdir -p /usr/local/go_path
echo 'export GOPATH=/usr/local/go_path' | sudo tee -a /etc/profile
echo 'export PATH=$PATH:/usr/local/go_path/bin' | sudo tee -a /etc/profile
source /etc/profile
you can write a simple Golang program and give it a shot:
cd /usr/local/go_path
mkdir -p src/hello
cd src/hello
vi hello.go
Populate the file /usr/local/go_path/src/hello/hello.go
with the following code segment:
package mainimport "fmt"func main() {
fmt.Printf("hello, world\n")
}
Save and quit:
:wq!
Finally, run your first Golang program as follows:
go run hello.go
If everything was done correctly, you will see the output:
hello world
Note :
You can change “/usr/local/go_path” with another path directory.
GOPATH
is discussed in the cmd/go
documentation:
The
GOPATH
environment variable lists places to look for Go code. On Unix, the value is a colon-separated string. On Windows, the value is a semicolon-separated string. On Plan 9, the value is a list.
GOPATH
must be set to get, build and install packages outside the standard Go tree.
GOROOT
is discussed in the installation instructions:
The Go binary distributions assume they will be installed in
/usr/local/go
(orc:\Go
under Windows), but it is possible to install the Go tools to a different location. In this case you must set theGOROOT
environment variable to point to the directory in which it was installed.For example, if you installed Go to your home directory you should add the following commands to
$HOME/.profile
:
export GOROOT=$HOME/go export PATH=$PATH:$GOROOT/bin
Note:
GOROOT
must be set only when installing to a custom location.
Step 4: Install Beego
You can use the classic Go way to install Beego:
go get github.com/astaxie/beego
Frequently asked questions (You can skip if already install git):
- git is not installed. Please install git for your system. In ubuntu you can install git :
sudo apt-get install git
- git https is not accessible. Please config local git and close https validation:
git config --global http.sslVerify false
Step 5: Install Bee tool
Bee tool is a project for rapid Beego development. With bee tool developers can create, auto compile and reload, develop, test, and deploy Beego applications quickly and easily.
Install bee tool with the following command:
go get github.com/beego/bee
then
go install github.com/beego/bee
bee
is installed into GOPATH/bin
by default. You need to add GOPATH/bin
to your PATH, otherwise the bee
command won’t work.
bee tool commands
Type bee
in command line and the following messages with be displayed:
bee is a tool for managing Beego framework.Usage: bee command [arguments]The commands are: new Create a Beego application
run run the app and start a Web server for development
pack Compress a Beego project into a single file
api create an API Beego application
bale packs non-Go files to Go source files
version show the bee, Beego and Go version
generate source code generator
migrate run database migrations
Step 6: Create new beego project with bee tool
You can create a new Beego project by typing bee new <project name>
under $GOPATH/src
for example, you want to create project “sample_mvc” as below :
bee new sample_mvc
That is a typical MVC application and main.go
is the project’s main file.
Step 7: Run beego project
Go to the path of the newly created project and enter bee run
to compile and run the project.
cd sample_mvc
bee run
gotcha,,your sample_mvc’s project has running well
You can try access it in your browser,
http://your-ip:8080
Because default beego framework using port 8080, and you should would get like below :
If you want to running in background, you can use “nohup”, like as below :
nohup bee run >/dev/null 2>&1 &