As we approach the holiday of Shavuot commemorating the receiving of the Torah, would you please explain how and when the first Torah scroll was actually written?
The Talmudic Sages (Gittin 60a) actually differ about this point.
In the view of one opinion, the Torah was written on separate parchments in separate installments throughout the sojourn in the wilderness. These segments were then assembled at the end of the forty years, before crossing into the Land of Israel.
Accordingly, immediately after Sinai, G‑d dictated to Moses the entire book of Genesis, and the book of Exodus until the portion of Yitro that recounts the giving of the Torah (Ramban, Intro to Torah). As further events occurred and further precepts were given, G‑d dictated to Moses how to record these events and precepts in additional parchments. The book of Deuteronomy was dictated as a type of summary at the end of the fortieth year.
The other view expressed by the Sages is that the entire Torah was dictated by G‑d to Moses at one time, and not in installments of separate scrolls. Accordingly, the Torah was written at the end of the fortieth year in the wilderness, just prior to the death of Moses.
In fact, the concluding eight verses of the Torah which describe the death of Moses raises an additional difference of opinions as to who actually wrote these final verses.
The Talmud states (Baba Batra 15), “‘And Moses the servant of the L-rd died there’ (Deut. 34:5). Can it be that while Moses was still alive he could have written, ‘And Moses died there’? Rather, until these verses describing his death, Moses wrote. From here on (i.e., after the death of Moses), Joshua wrote. Rabbi Shimon said, ‘Is it possible that the Torah should be lacking a single letter (i.e., Is it possible that Moses did not write the entire Torah?). Rather, we must conclude that until the account of Moses’ death, G‑d dictated and Moses wrote. From here on, G‑d dictated and Moses wrote with tears.”
One explanation of the above is that because of the truth of the Torah, Moses could not have formally written about his death in the Torah: In order to write he’d have to be alive, and could not write that he died. Conversely, if he wrote about his death, he would necessarily die. Thus, the first opinion concludes that Joshua wrote these last eight verses. However, since this negates the integrity of the Torah, the second opinion posits that Moses actually wrote the entire Torah to the very end, including his death. But these last eight verses were informally stenciled by Moses using the tears he shed over his pending death. Joshua then later filled in the tear-stenciled verses with proper ink. In this way, Joshua literally filled the void left by Moses’ death and continued the story where Moses left off.
The Gaon of Vilna, however, offers another explanation. He notes that the Hebrew term used in the Talmud for Moses’ concluding the final eight verses of the Torah in tears is “dema”. He writes that “dema” also connotes something that is mixed. Thus, he explains that when the Talmud states that Moses concluded the Torah with “dema”, this means that the concluding letters, words and verses of the Torah were written without interrupting spaces. In this way, the concluding section of the Torah describing Moses’ death was dictated to him by G‑d in a cryptic way, where many possible “readings” of the text were mixed together in various letter combinations. Therefore, the final words, “And Moses died there,” were not explicitly apparent in the Torah that Moses wrote. Rather, these words emerged from the mixture of possible readings only as a result of the “space” left by his death.