I listen to music while alone. For me it gets me in the mood and also I have certain playlists for certain times of coding. If I am learning I have a playlist for that, also for if I know the language and what I am doing because I planned it out I have a playlist for that as well. I have made these playlists for working and playing for 4 years now.
I'm not sure where I heard this but something about the part of the brain that listens to music is actually seperate and can be a part of your multitasking. Although I have noticed if I have the wrong kind of music playing or if it is too loud then my thought process will be slowed or I will start to daydream. So the right music is dependent on your own mind and you would have to practice working in different situations with different songs to test if they work for you. Like I said this has taken me years.