Zack Scholl

How to install portmidi

 / #software #music #golang 

A simple way to get MIDI and Golang work together using portmidi.

The Go libraries for MIDI devices - and - both use the portmidi library with CGo. This means that building any package that requires these dependencies will need to also compile portmidi.

Here are simple instructions for macOS, Linux, and Windows to get started with portmidi and Go.

Install portmidi on Mac

Open a terminal and do

brew install portmidi

Install portmidi on Linux

Open a terminal and do:

sudo apt install libportmidi-dev

Install portmidi on Windows

First install MSYS2. This will allow you to add a natively built PortMidi directly onto your system.

Once that is installed, run MSYS2 and in the command prompt install PortMidi with the following command:

> pacman -S mingw-w64-x86_64-portmidi

In order to get it to work, in Powershell you should add the following two environmental variables when you try to build a Go program (note, if you didn’t install MSYS2 into C:\msys64 you will need to change that directory):


Simply copy and paste those environmental variables into the terminal you are using before doing a go build or go run.

Test it out

To test it, run and build this program:

package main

import (


func main() {
	err := portmidi.Initialize()
	if err != nil {
	fmt.Println("[INFO] total MIDI devices:", portmidi.CountDevices())

If everything worked correctly, you should be able to compile and run!