Preheat oven to 350° F. Lightly grease a 9"x13" or deep 9"x9" baking dish with butter and set aside.
Add olive oil to a medium skillet and heat over medium heat. Add thinly sliced onions and salt and pepper, to taste. Cook over medium heat, stirring occasionally until onions become caramel in color. Remove from skillet and set aside.
Wipe skillet clean and cook bacon until crisp. You can alternatively bake the bacon while the onions are cooking, saving a little time. Remove bacon from skillet, drain grease, and wipe clean again.
Melt butter over medium heat. Sprinkle flour on top of butter and whisk to form a roux. Cook for 2 minutes.
Slowly add the milk, whisking constantly. Bring roux to a simmer and let thicken just slightly, about 2-3 minutes after coming to a simmer. Remove from heat and set aside.
Assembly: Layer half of the potatoes, half of the caramelized onions, half the bacon, half the cheese, and half the sauce (in that order) in the prepared baking dish. Repeat.
Cover baking dish with foil and bake for 1 hour. Remove foil and bake an additional 30 minutes or until potatoes are easily pierced with a fork. Remove from oven and let cool 10 minutes before serving.