Doing masturbation is disliked (makruh) in Islam because it may cause serious health problems in long term. It is not forbidden (haraam) since there is not a specific ruling in the Qur’an which forbids masturbation.
The best solution to stay away from masturbation is marriage. Yet, not everybody may have the means to marry or meet the person they may want to marry. The Almighty God decrees living in continence to all Muslims until they get married:
وَأَنكِحُوا الْأَيَامَى مِنكُمْ وَالصَّالِحِينَ مِنْ عِبَادِكُمْ وَإِمَائِكُمْ إِن يَكُونُوا فُقَرَاء يُغْنِهِمُ اللَّهُ مِن فَضْلِهِ وَاللَّهُ وَاسِعٌ عَلِيمٌ . وَلْيَسْتَعْفِفِ الَّذِينَ لَا يَجِدُونَ نِكَاحًا حَتَّى يُغْنِيَهُمْ اللَّهُ مِن فَضْلِهِ .
“And wed the singles among you, and those who are fit (for marriage) among your male and female captives. If they are poor, God fulfills their needs out of His favor. God has ample means, He is all-knowing. And let those who do not find the means to marry (either free or captive) live in continence until God fulfills their needs out of His favor.” (an-Noor 24:32-33)
Although the normal ruling on masturbation is being disliked (makruh), it may become allowed (mubah) when it is really inevitable to be able to avoid a totally forbidden (haraam) act, such as fornication.
Yet, Nabi Muhammad (a.s.) had shown the way of living in continence, which will also help avoiding masturbation. He recommended single young people to fast:
يَا مَعْشَرَ الشَّبَابِ مَنِ اسْتَطَاعَ مِنْكُمُ الْبَاءَةَ فَلْيَتَزَوَّجْ فَإِنَّهُ أَغَضُّ لِلْبَصَرِ وَأَحْصَنُ لِلْفَرْجِ وَمَنْ لَمْ يَسْتَطِعْ فَعَلَيْهِ بِالصَّوْمِ فَإِنَّهُ لَهُ وِجَاءٌ
“O young people, those among you who can afford the expenses of marriage should marry, for it restrains eyes (from casting evil glances) and preserves one from immorality; but they who cannot afford It should observe fast for it is a means of controlling the sexual desire.” (Muslim, Marriage, 1; Bukhari, Marriage, 3)
In the mean time, you should keep on praying God for His help. As we know by the following verse that offering prayers and being patient are necessary to get the help of God:
“Oh, you who believe! Ask for help through patience and offering the prayer. God is with those who are patient.” (al-Baqarah 2:153)”
If you follow these recommendations, inshaAllah you may attain what you hope for…