You did not include information about the event that led to the destruction of your friendships. It has been two years. That event seems to have had a significant and negative impact on your life. Perhaps it has led to your social isolation and contributed to the problem of talking to yourself. Perhaps if you were interacting with friends, then the likelihood of your “habit” continuing would decrease significantly or would vanish completely.
You associate talking to yourself with having a mental health disorder. That may or may not be true. I would have to interview you in person and extract many more details about your concerns to know if there was evidence of a mental health disorder. A clinical evaluation could determine if a mental health disorder is present.
How can you stop talking to yourself? Decrease the time you spend alone and increase the time you spend with others. Being in the presence of others will force you to interact with them. There are times when working alone and being alone are advantageous and desirable but prolonged periods of social isolation are unhealthy. It’s often a sign of depression. Human beings thrive on social interactions. Relationships are very important to the majority of people. Studies consistently show the positive benefits associated with social interaction. Individuals with more social supports are psychologically healthier than individuals with fewer social supports. Virtually all studies show this to be true.
If this is an issue that continues to concern you, then it would be advisable to see a mental health professional. The find help tab at the top of this page can help you locate a therapist in your community. The therapist might also help you to understand your past relationships with your friends and to remove the pain associated with those relationships. Good luck and I wish you well.