The Prophet (peace and blessings be upon him) forbade remaining alone: that a person sleep alone, or that they travel alone. [Ahmad, Musad, from Ibn Umar (Allah be pleased with him)]
This prohibition is for discouragement–dislikedness (karaha), as mentioned by the majority of the scholars. [Munawi, Fayd al-Qadir, others]
The wisdom in this discouragement relates to avoiding harm and to achieving benefit:
(a) As for avoiding harm, there are religious, spiritual, emotional, and social harms in being alone. A person alone is more likely to fall into sin; to be lazy in spiritual routines; being alone can result in feeling down and depressed; and a person alone is more likely to be socially alienated.
(b) As for achieving benefit, being with others—especially good company—is of religious, spiritual, emotional, and social benefit.
Being with people also nurtures good character (husn al-khuluq)—as it requires patience, concern for others, and other qualities that are means to the pleasure of Allah Most High. Being alone can stunt one’s human potential.
May Allah send His eternal blessings and peace upon the Greatest Teacher and Fountain of Wisdom, our Beloved Messenger (peace and blessings be upon him and his folk).
مسند أحمد من حديث ابن عمر رضي الله عنهما أن النبي صلى الله عليه وسلم نهى عن الوحدة، أن يبيت الرجل وحده أو يسافر وحده. قال المناوي في فيض القدير: “أن يبيت الرجل” ومثله المرأة “وحده” أي في دار ليس فيها أحد. انتهى.