Unfortunately, for many of us, this is not an atmosphere we always or even often have access to. Motivation when training alone can be extremely difficult, and your training progress can suffer considerably. So how do you get around this when your options are limited?
First and foremost, take care of energy problems the right way. Caffeine is not a substitute for adequate sleep and stress-reduction strategies. Without this foundation, all the other stuff you try to improve your motivation will fall far short of its potential.
Minimize distractions. When training alone, especially with something like weightlifting in which there is a great deal of rest relative to actual work, it can be easy to wander mentally and physically. Turn your phone off and don't keep it nearby. Don't check your email or read IronGarm between sets. If you need something to occupy yourself during the downtime, consider doing something like making notes about your training, about ideas for future cycles, or training goals - something that keeps you focused on training, even if it's not the actual training of that day. Better yet, focus on the current training. Visualize your next set as vividly as possible, making sure to create the perfect execution - don't get lazy and let yourself visualize marginal performances, and definitely don't ever let a missed rep into your head.
In many ways I prefer my lifters to train without music, but at the same time, there are undoubtedly circumstances in which it's very helpful; training alone is one of them. We've all felt the effect of music on our mood and our energy. Selecting music can be tough sometimes, however. If you have a long workout, it's tough to stay so aroused for so long - if the music is too intense too constantly, it may end up having a negative effect by wearing you out. Get a bit of variety in there. My lovely wife Aimee actually prefers pop-nonsense, not because it's directly motivating in the conventional sense, but because it keeps her distracted from negative thoughts and minimizes the anxiety arising from her expectations of her performance. Find what works for you.
Video is another one to try. It's not entirely uncommon to see weightlifting gyms with lifting videos playing while the athletes are training. This can be motivating in the obvious ways, but it can also have surprisingly beneficial effects on technical performance. Often the act of watching very proficient lifters will encourage better technical performance without any conscious thought.
And of course, the best method is to avoid the problem entirely and invite some other athletes to train with you.