The author’s loved ones and friends have left. They have all deserted him. And the bad thing is, that God is the agent here. He has removed them. In the end, the author only has one companion: the darkness. The alternative ESV translation says, darkness has become my only companion
and therefore it has our preference.
Again, we may think of Jesus here. In Gethsemane the disciples fell asleep. No one was guarding the struggling Jesus (see Matthew 26:40–46). And when Jesus had been taken prisoner, all the disciples left him and fled
(Matthew 26:56b). And in that same night Peter denied knowing his Master—three times even. The word darkness
(in Matthew 27:45) also fits perfectly with what happened on Golgotha. When Jesus had been nailed to the cross, there was darkness over all the land until the ninth hour.
How much would Jesus have recognized himself in this Psalm!
18 You have caused my beloved and my friend to shun me; my companions have become darkness.