Positive commandment · Mitzvat Aseh #17 of 613
For each man to write a Torah scroll for himself
לִכְתֹּב כָּל אִישׁ סֵפֶר תּוֹרָה לְעַצְמוֹ
The verse · Devarim 31:19
וְעַתָּה כִּתְבוּ לָכֶם אֶת־הַשִּׁירָה הַזֹּאת וְלַמְּדָהּ אֶת־בְּנֵי־יִשְׂרָאֵל שִׂימָהּ בְּפִיהֶם לְמַעַן תִּהְיֶה־לִּי הַשִּׁירָה הַזֹּאת לְעֵד בִּבְנֵי יִשְׂרָאֵל
“Now therefore write this poem for yourselves, and teach it the children of Yisra᾽el: put it in their mouths, that this poem may be a witness for me against the children of Yisra᾽el.”
The Rambam’s words
For each man to write a Torah scroll for himself, as [Deuteronomy 31:19] states: "Write down this song."
לִכְתֹּב כָּל אִישׁ סֵפֶר תּוֹרָה לְעַצְמוֹ, שֶׁנֶּאֱמָר: "כִּתְבוּ לָכֶם אֶת הַשִּׁירָה הַזֹּאת" (דברים לא, יט).
Why · Sefer HaChinuch #613
For everyone to write a Torah scroll for himself
“It is from the roots of the commandment [that it is that] since it is well-known about people that they do all of their things according to what is prepared for them, therefore He, blessed be He, commanded that there be a Torah scroll prepared with him, that he can always read from it and that he not need to walk for it to the house of his fellow; “so that he will learn to fear the Lord,” and know and understand His commandments that are more precious and desirable than much gold and fine gold.”
Who & when
“And it is practiced at all times and in every place by males, as they are obligated in Torah study — and so too, to write it — but not females.”
Where in the Mishneh Torah
Hilchot Tefillin, Mezuzah and the Torah Scroll הלכות תפילין ומזוזה וספר תורה