497. Coronation Day: The obligation to appoint a king from Israel
You shall appoint over yourselves a king, whom God will choose from among your brethren… (Deuteronomy 17:15)
God knew that, eventually, the Jewish people were going to want a king. When this happened (as it did in the time of Samuel), He instructed them in how to proceed. The first thing was that the king had to be appointed by Sanhedrin based upon the instruction of a prophet. (Sure enough, Samuel himself was there to install not only King Saul, the first king of Israel, but King David, the first king of the permanent dynasty.)
The king was to be anointed with the special anointing oil (see Mitzvah #107), after which it was a hereditary position. (This is equally true of many other positions of honor. A Kohein Gadol or the rebbe of a Hasidic sect would be succeeded by a worthy son if there is one before looking at potential heirs from outside the family.)
Of course, there is an obvious question. We have established that the 613 mitzvos only include things meant to be observed in perpetuity. One-time commands, such as the Jews in Egypt painting their doorways, are not numbered among the list. So, since every king after the first simply inherits the throne, how is the mitzvah "to set a king over us" fulfilled after the one time?
Simple. “To set a king over us” doesn't only mean to appoint a king. It also means to acknowledge the king’s authority and to defer to his rule. So, whenever there’s a king, we must all “set him over us.” (Among the laws of placing a king over us are such details as no one may sit on the throne or marry his widow.)
The reason for this mitzvah is that the people need a leader. Without someone in control, we have anarchy and chaos. Say what you will about our kings, presidents or prime ministers, our countries would be in a lot worse shape without someone in the driver's seat!
This mitzvah applies when the Jews occupy their land. In the Talmud, it is discussed in tractate Sanhedrin (20b) and in Sotah (41b). It is codified in the Mishneh Torah in the first chapter of Hilchos Melachim and is #173 of the 248 positive mitzvos in the Rambam’s Sefer HaMitzvos.