Packaging Go application for Debian

后端 未结 5 615
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 05:32

How can I put my Go binary into a Debian package? Since Go is statically linked, I just have a single executable--I don\'t need a lot of complicated project metadata information

5条回答
  •  失恋的感觉
    2021-01-30 05:43

    I've just been looking into this myself, and I'm basically there.

    Synopsis

    By 'borrowing' from the 'package' branch from one of Canonical's existing Go projects, you can build your package with dpkg-buildpackage.

    1. install dependencies and grab a 'package' branch from another repo.

       # I think this list of packages is enough. May need dpkg-dev aswell.
       sudo apt-get install bzr debhelper build-essential golang-go
       bzr branch lp:~niemeyer/cobzr/package mypackage-build
       cd mypackage-build
      
    2. Edit the metadata.

      • edit debian/control file (name, version, source). You may need to change the golang-stable dependency to golang-go.
      • The debian/control file is the manifest. Note the 'build dependencies' (Build-Depends: debhelper (>= 7.0.50~), golang-stable) and the 3 architectures. Using Ubuntu (without the gophers ppa), I had to change golang-stable to golang-go.
      • edit debian/rules file (put your package name in place of cobzr).
      • The debian/rules file is basically a 'make' file, and it shows how the package is built. In this case they are relying heavily on debhelper. Here they set up GOPATH, and invoke 'go install'. Here's the magic 'go install' line:

      cd $(GOPATH)/src && find * -name '*.go' -exec dirname {} \; | xargs -n1 go install

      • Also update the copyright file, readme, licence, etc.
    3. Put your source inside the src folder. e.g.

       git clone https://github.com/yourgithubusername/yourpackagename src/github.com/yourgithubusername/yourpackagename
      

      or e.g.2

       cp .../yourpackage/ src/
      
    4. build the package

       # -us -uc skips package signing.
       dpkg-buildpackage -us -uc
      

      This should produce a binary .deb file for your architecture, plus the 'source deb' (.tgz) and the source deb description file (.dsc).

    More details

    So, I realised that Canonical (the Ubuntu people) are using Go, and building .deb packages for some of their Go projects. Ubuntu is based on Debian, so for the most part the same approach should apply to both distributions (dependency names may vary slightly).

    You'll find a few Go-based packages in Ubuntu's Launchpad repositories. So far I've found cobzr (git-style branching for bzr) and juju-core (a devops project, being ported from Python).

    Both of these projects have both a 'trunk' and a 'package' branch, and you can see the debian/ folder inside the package branch. The 2 most important files here are debian/control and debian/rules - I have linked to 'browse source'.

    Finally

    Something I haven't covered is cross-compiling your package (to the other 2 architectures of the 3, 386/arm/amd64). Cross-compiling isn't too tricky in go (you need to build the toolchain for each target platform, and then set some ENV vars during 'go build'), and I've been working on a cross-compiler utility myself. Eventually I'll hopefully add .deb support into my utility, but first I need to crystallize this task.

    Good luck. If you make any progress then please update my answer or add a comment. Thanks

提交回复
热议问题