Positive commandment · Mitzvat Aseh #18 of 613
That the king write a second Torah scroll for himself
לִכְתֹּב הַמֶּלֶךְ סֵפֶר תּוֹרָה לְעַצְמוֹ, יָתֵר עַל הָאֶחָד שֶׁלְּכָל אָדָם, עַד שֶׁיִּהְיֶה לוֹ שְׁתֵּי תּוֹרוֹת
The verse · Devarim 17:18
וְהָיָה כְשִׁבְתּוֹ עַל כִּסֵּא מַמְלַכְתּוֹ וְכָתַב לוֹ אֶת־מִשְׁנֵה הַתּוֹרָה הַזֹּאת עַל־סֵפֶר מִלִּפְנֵי הַכֹּהֲנִים הַלְוִיִּם
“And it shall be, when he sits upon the throne of his kingdom, that he shall write for himself a copy of this Tora in a book out of that which is before the priests the Levites:”
The Rambam’s words
For the king to write a second Torah scroll for himself in addition to the one which [he is required to write to fulfill the mitzvah incumbent upon] every man. Thus, he will have two Torah scrolls, as [Deuteronomy 17:19] states: "And he shall write for himself a copy of this Torah."
לִכְתֹּב הַמֶּלֶךְ סֵפֶר תּוֹרָה לְעַצְמוֹ, יָתֵר עַל הָאֶחָד שֶׁלְּכָל אָדָם, עַד שֶׁיִּהְיֶה לוֹ שְׁתֵּי תּוֹרוֹת, שֶׁנֶּאֱמָר: "וְכָתַב לוֹ אֵת מִשְׁנֵה הַתּוֹרָה הַזֹּאת" (דברים יז, יח).
Why · Sefer HaChinuch #503
That the king write a second Torah scroll for himself
“It is from the roots of the commandment [that it is] because the king is under his own power — no man will pain him over his actions nor rebuke him, and with the staff of his mouth he can plague his land and with the breath of his lips he can kill anyone he wants from his entire people.”
Who & when
“And there is no need to write at which time it is practiced, as the thing is well-known that there is no monarchy in Israel except at the time that their Land is in its inhabitation — may our eyes see it soon with the coming of the redeemer, and he shall rule over us in it.”
Where in the Mishneh Torah
Hilchot Tefillin, Mezuzah and the Torah Scroll הלכות תפילין ומזוזה וספר תורה