Envy is permitted only in two cases

1377. Ibn Mas`ud (May Allah be pleased with him) reported: The Prophet (sallallaahu ’alayhi wa sallam) said,

“Envy is permitted only in two cases: A man whom Allah gives wealth, and he disposes of it rightfully, and a man to whom Allah gives knowledge which he applies and teaches it.”

[Al-Bukhari and Muslim].

Commentary:  According to Imam An-Nawawis explanation, the word Hasad (jealousy) is used in the sense of Ghibtah  i.e., envy. In Islam, jealousy is forbidden and is held unlawful. The reason being that one who is jealous wants that the person, who possesses the quality of which he is jealous, be deprived of that quality. Envy is permissible for the reason that when one seems that a person has been graced by Allah with certain gifts and qualities, he also desires to be blessed with those gifts. In the latter case, he does not grumble and grieve but eagerly prays to Allah for those gifts. `Knowledge’ here stands for the knowledge of the Qur’an and Hadith because this knowledge alone is beneficial for man, and it is through this knowledge that correct judgements can be made among the people. This Hadith has an inducement for acquiring useful knowledge along with wealth to spend in the ways ordained by Allah.

Imam Al-Nawawi’s Riyad-us-Saliheen – Book Twelve: The Book of Knowledge
Chapter 241 : Virtues of Knowledge which is Learnt and Taught for the sake of Allah