Preferably with a studio and a beat, but, I've gotten some of my best stuff without, lyrically speaking.
In the car, waiting in line, sometimes just walking. I can't speak for anyone else but personally, my head kinda goes on perpetual loop when a tune catches my interest. Its not playing all the time up there naturally, but during slack time when I am not focused on something else my brain will load that loop up again, I will find myself humming along to it, trying to "feel" it out, and maybe lightning will strike. You never know.
Conversly, I have sat in my studio surrounded by great equipment, able to not only hear the beat, but also "FEEL" the beat , and couldn't come up with Ba-Ba Do-Do.
Boy, is that frustrating.
Nothing works for me all the time.