The 18th mitzvah is that every Jewish male should have his own Sefer Torah. It is highly commendable and preferable that he write it himself, as our Sages said, "If he himself writes it, it is as if he personally received it at Mount Sinai." If it is impossible for him to write it himself, he is at least required to buy one or to appoint someone to write it for him.
The source of this commandment is G‑d's statement, "Write for yourselves this song." One may not write individual sections of the Torah [and therefore not write merely the song of Ha'azinu, which follows this verse]. Therefore, the commandment to write the "song" means the entire Torah, including this song.
In the tractate Sanhedrin, it is related: "Rava said, Even though a person's forefathers left him a Sefer Torah, it is still a mitzvah for him to write one for himself, as the verse says, 'And now write for yourselves this song.'
"Abaye questioned him: [The Tosefta says regarding a king, that] 'he must write a Torah scroll for himself, and not be satisfied with the one left for him by his forefathers' — [this implies that the mitzvah to write a Torah scroll applies] only for a king, not for a common person.' "
The Gemara's answer to this question: [The Tosefta's statement does not come to tell you that only the king, rather than a common person, has this commandment; rather,] "it teaches you [that the king is obligated to have] two Torah scrolls. The proof for this is from the following Baraisa: The verse says, 'He [the king] shall write for himself a copy of this Torah' — this means a total of two Torah scrolls."
The meaning of this statement: the difference between a king and a common person is that the common person must write for himself one Torah scroll, and the king, two Torah scrolls, as explained in the second chapter of tractate Sanhedrin.
The laws and conditions governing the writing of a Torah scroll are explained in the third chapter of tractate Menachos, the first chapter of tractate [Bava] Basra, and in tractate Shabbos.