הלכות ספר תורה
מצות עשה על כל אדם מישראל שיכתוב לו ס"ת ומאד צריך לחזר אחריה דאמר רבי יהושע בן לוי הלוקח ספר תורה מן השוק כחוטף מצוה מן השוק כתבו או שהגיה בו אפילו אות אחת כאילו קבלה מהר סיני וכל שכן שאין לו למוכרה אפילו יש לו הרבה מהם ואפילו למכור ישן כדי לקנות חדש אסור אפילו אין לו מה יאכל ומיהו אם צריך למוכרה כדי ללמוד תורה או לישא אשה ואין לו דבר אחר למכור שפיר דמי וכת' א"א הרא"ש ז"ל שזה לא נאמר אלא לדורות הראשוני' שהיו כותבין ס"ת ולומדי' בה אבל האידנא שכותבין ס"ת ומניחים אותו בבית הכנסת לקרות בהם ברבים מצות עשה על כל ישראל אשר ידו משגת לכתוב חומשי התורה ומשנה וגמרא ופירושיהם להגות בהן הוא ובניו. מצות כתיבת התורה היא כדי ללמוד בה דכתיב ולמדה את בני ישראל שימה בפיהם ועל ידי הגמרא ופירושה ידע פירוש המצות והדינין על בוריים לכן הן הן הספרים שאדם מצווה לכותבם וגם שלא למוכרם אם לא ללמוד תורה ולישא אשה: Laws of Torah Scrolls
It is a mitzvah incumbent on every Jewish man to write a Torah scroll, and it is extremely important to seek [an opportunity] to perform it. As Rabbi Joshua ben Levi says, "One who buys a [ready made] Torah Scroll is like one who grabs a mitzvah in the market place." [On the other hand] One who writes it himself or corrects even one letter is counted as if he received it directly from Mt. Sinai. It goes without saying that he shouldn't sell his Torah, even if he has many, and it is even forbidden to sell an old scroll in order to buy a new one, or even [to buy food]. However it is permitted to sell a Torah scroll in order to learn Torah or to get married if he has nothing else to sell. My master, my father, the Ro"sh, of blessed memory, wrote that this was written only for the earlier generations, who would write a Torah scroll and learn from it. But nowadays, when we write a Torah scroll and leave it in the synagogue to read from it publicly, it is a positive mitzvah incumbent on all Jews who have the means to do so to write [copies] of the Torah, the Mishnah, and the Gemara and their commentaries, and to study from them - him, and his sons. For the mitzvah of writing a Torah is in order to learn from it, as it says: (Deut. 31:19) "and teach it to the people of Israel, place it in their mouths." And through the Gemara and its explanation, one will know the explanation of the mitzvot and the laws with clarity. Hence these are the books that a person is commanded to write and also not to sell unless it is [to raise the money] to study Torah or to a marry a woman.