Positive commandment · Mitzvat Aseh #173 of 613
To appoint a king
למנות מלך
The verse · Devarim 17:15
שׂוֹם תָּשִׂים עָלֶיךָ מֶלֶךְ אֲשֶׁר יִבְחַר ה׳ אֱלֹהֶיךָ בּוֹ מִקֶּרֶב אַחֶיךָ תָּשִׂים עָלֶיךָ מֶלֶךְ לֹא תוּכַל לָתֵת עָלֶיךָ אִישׁ נׇכְרִי אֲשֶׁר לֹא־אָחִיךָ הוּא
“then thou mayst appoint a king over thee, whom the Lord thy God shall choose: one from among thy brethren shalt thou set as king over thee: thou mayst not set a stranger over thee, who is not thy brother.”
The Rambam’s words
To appoint a king, as [Deuteronomy 17:15] states: "You shall surely set a king upon yourselves."
למנות מלך, שנאמר: "שום תשים עליך מלך" (דברים יז,טו).
Why · Sefer HaChinuch #497
To appoint a king from Israel
“It is from the roots of the commandment that it is because it is impossible for the settlement of people without their making one of them head over the others, to do his command and fulfill his decrees.”
Who & when
“And this commandment is practiced when Israel is on their Land — and like they, may their memory be blessed, said (Sanhedrin 20b), “Israel was commanded three commandments in their entering into the Land: to appoint a king over themselves, to build the Choice House and to cut off the seed of Amalek.””
Where in the Mishneh Torah
Hilchot Kings and Wars הלכות מלכים ומלחמות