Melt the butter in a large saucepan over medium-low heat. Add the onions, garlic and thyme leaves, and stir. Cover and cook, stirring frequently, until the onions are deep golden brown, 45-60 minutes.
Add the sugar, salt and pepper. Stir, replace the lid and cook for 10 minutes.
Add the vinegar and scrape up any browned bits in the pan, mixing it into the onions. Cook until the liquid has evaporated, about 10 minutes. Transfer to a container and let cool completely, then cover and refrigerate for up to 2 weeks.