Install Go Swagger on Ubuntu 24.04

Install Go Swagger on Ubuntu 24.04

Go Swagger is a command line tool that simplifies the integration of OpenAPI Specification with the Go programming language. It allows developers to generate Go code from OpenAPI definitions and vice versa, making it easier to develop, document, and maintain RESTful APIs in Go. Additionally, Go Swagger supports other functions like serving UI directly from the specification file. This tutorial shows how to install Go Swagger on Ubuntu 24.04.

Install Go Swagger

Download Go Swagger executable to /usr/local/bin directory:

sudo wget -qO /usr/local/bin/swagger https://github.com/go-swagger/go-swagger/releases/latest/download/swagger_linux_amd64

Set execute permission:

sudo chmod a+x /usr/local/bin/swagger

We can check Go Swagger version as follows:

swagger version

Testing Go Swagger

Create simple configuration file:

nano swagger.yaml

Add the following code:

swagger: "2.0"
info:
  version: "1.0"
  title: "Hello World API"
paths:
  /hello/{user}:
    get:
      description: Returns a greeting to the user!
      parameters:
        - name: user
          in: path
          type: string
          required: true
          description: The name of the user to greet.
      responses:
        200:
          description: Returns the greeting.
          schema:
            type: string
        400:
          description: Invalid characters in "user" were provided.

Run the following command to serve a documentation site:

swagger serve swagger.yaml --no-open

To access documentation, open a web browser and go to the URL, which is printed to the terminal after running the command.

Go Swagger UI serving on Ubuntu

Uninstall Go Swagger

To remove Go Swagger, delete its corresponding file:

sudo rm -rf /usr/local/bin/swagger

Leave a Comment

Cancel reply

Your email address will not be published.