OPINIONS: The Beraisa quotes Rebbi Yehudah who states that one who did not see the Great Synagogue in Alexandria never saw the glory of the Jewish people. The Beraisa describes the magnificence of the structure and the vast number of Jews who worshipped there.
Abaye concludes the Beraisa's description with an account of the tragic end of the Jewish community of Alexandria: every member of the community was killed by a Roman monarch.
(According to the Vilna Ga'on and the Yerushalmi, the monarch was the emperor Trajan. According to Rav Yakov Emden and the Abarbanel in his introduction to Melachim, it was the Roman monarch Alexander Latirus (this may also be the view of Rashi here (DH Stav) who refers to "Alexandrus"). According to the Gemara in Gitin (57b), it was the emperor Hadrian. The text of our Gemara, which says that it was Alexander the Macedon who killed them, does not seem plausible, since he lived much earlier. See ARUCH LA'NER.)
The Gemara explains that the Jews of Alexandria were punished because they transgressed the prohibition, "You shall not return on this path [to Egypt] anymore" (Devarim 17:16).
The Mechilta (Shemos 14:13) expounds on this prohibition. The Mechilta points out that in three different places the Torah warns us not to return to Egypt. The first verse is the one quoted by the Gemara here, "You shall not return..." (Devarim 17:16). The second verse is, "... for as you have seen Egypt today, you shall never see them again" (Shemos 14:13). The third verse is written in the admonition in Parshas Ki Savo, "Hash-m will return you to Egypt in boats, on the path of which I said to you, 'Never again shall you see it'" (Devarim 28:68).
The RAMBAM (Hilchos Melachim 5:7, and Sefer ha'Mitzvos, Lo Ta'aseh 46) quotes the Mechilta and adds that the prohibition to live in Egypt applies to the entire area of 400 by 400 Parsa'os in the north-eastern corner of the African continent, which includes Sudan, Ethiopia, and part of the Sahara Desert.
The Gemara here, the Mechilta, and the ruling of the Rambam clearly indicate that a Jew is forbidden to return to Egypt. However, it is known that a prominent Jewish community thrived in Egypt until recent times (see the book, "Tuv Mitzrayim," by Rabbi Yosef Nafussi). Many of the greatest Jewish leaders, such as Rav Sa'adyah Ga'on (who was born in Egypt), the Rambam himself, and the Radvaz (one of the foremost commentators on Mishneh Torah) lived in Egypt. The KAFTOR VA'FERACH (ch. 5) writes that he met one of the Rambam's grandsons in Egypt who told him that his grandfather would sign his letters, "Moshe ben Maimon, who transgresses three prohibitions each day."
Why did Jews, among them great sages, live in Egypt if the Torah prohibits it? Why were they not concerned with the Gemara's account of the tragic fate of the Jews of Alexandria?
(a) The SEMAG (Lo Ta'aseh 227) writes that the prohibition against living in Egypt applies only to living among native Egyptians, descended directly from those who lived in Egypt at the time that the Torah was given. The Torah did not want the Jews to learn from that nation's evil ways, as the Rambam (ibid.) and Sefer ha'Chinuch (#500) write. From the time that Sancheriv dispersed the nations of the world, the Torah's prohibition no longer applies, because the people there are not the original Egyptians of yore. This also seems to be the view of RABEINU BACHYE (Devarim 17:16).
However, the Semag himself rejects this explanation because of the Gemara here, which refers to the Jews of Alexandria who lived in Egypt after Sancheriv dispersed the nations, and yet it still says that they were punished for living in Egypt.
The RITVA in Yoma (38a) revises the Semag's explanation in order to answer this question. He writes that the prohibition applies only to living in the cities that were founded by the original Egyptians. (The Ritva may have understood that Alexandria was originally an ancient Egyptian city that was developed and renamed by Alexander. Historical records, however, show that Alexander founded and built the city.) The prohibition applies to such cities presumably because the customs of cities are based on the customs of their original settlers. The Torah's prohibition against living in Egypt does not apply to the new cities that were established and settled after the original cities were abandoned.
(b) The SEFER YERE'IM (#309) writes that the Torah forbids only going from Eretz Yisrael to Egypt, as implied by the verse that the Gemara here quotes. The logic behind this limitation may be that one who returns to Egypt specifically from Eretz Yisrael shows a lack of gratitude to Hash-m. He shows that he does not appreciate the great miracle that Hash-m did for him when He redeemed the Jewish people from Egypt and brought them to Eretz Yisrael.
The RITVA and the KAFTOR VA'FERACH add that one is forbidden to go to Egypt from Eretz Yisrael only through the desert, along the path that the Jews traveled when they left Egypt. However, the BRIS MOSHE (on the Semag) points out that the verse in Devarim (28:68) seems to contradict this when it says that "Hash-m will return you to Egypt in boats, on the path of which I said to you, 'Never again shall you see it'." The verse implies that one is forbidden to go from Eretz Yisrael to Egypt even by boat.
The RADVAZ asks that although the verse quoted by the Gemara here might support the Yere'im's assertion, the other two verses cited by the Mechilta mention only that one may not go to Egypt; they make no mention of how, or from where, one may not go there.
(c) The RITVA (ibid.) concludes that the prohibition applies only when the Jewish people are an independent nation, settled in their homeland. When the Jews are in a state of Galus, all areas of the world are considered the same, and one may live in Egypt as in any other country. The only restriction on where a Jew may live is the prohibition which prohibits one who lives in Eretz Yisrael from leaving Eretz Yisrael to live somewhere else.
This might be Rashi's intention here when he writes (DH Stav) that the Jews of Alexandria settled there at the time of the Churban of the first Beis ha'Mikdash. Rashi means that the Jews of Alexandria transgressed the prohibition when they went directly from the Jewish kingdom in Eretz Yisrael to Egypt. Had they not traveled directly to Alexandria from Eretz Yisrael, they would have been allowed to settle in Egypt.
The logic behind this approach seems to be that going to Egypt when Eretz Yisrael is settled by Jews shows a lack of trust in Hash-m. The Jews should live in Eretz Yisrael and trust in Hash-m to protect them, rather than go to neighboring Egypt for protection. In times of Galus, however, when the Jews need to find a suitable place to live, they may live wherever they want.
(d) The RADVAZ (Hilchos Melachim 5:7) writes that the prohibition is to go to Egypt, not to live there. A person who is already in Egypt, who arrived there in a permitted way (such as for business, which the Yerushalmi (Sanhedrin 10:9) permits), does not transgress the Isur d'Oraisa (but only an Isur d'Rabanan) if he chooses to stay there. The Rabanan were lenient and did not apply their prohibition once a person is already settled in the place, because it is difficult to travel and find a livelihood elsewhere once a person is settled (although he should leave when the opportunity arises).
(e) None of these explanations are consistent with the words of the RAMBAM and SEFER HA'CHINUCH, who rule that the prohibition applies today unconditionally, regardless of where a person comes from or what path he takes to get to Egypt. They write that living there is forbidden, and not merely the act of going there.
Why, then, did the Rambam live in Egypt? The RADVAZ and the KAFTOR VA'FERACH write that perhaps he had no choice, since he was the physician of the Sultan and it was not possible for him to leave.
Perhaps the Radvaz means that one is permitted to travel to Egypt for business or for any other temporary purpose, as long as he intends to leave when he can. One is permitted even to settle there for an extended period, if he plans to leave. The Rambam always intended to leave, and to remind himself of his intentions he adopted the practice of signing his letters as "[he] who transgresses three prohibitions each day." This understanding is consistent with the words of the Radvaz (in Hilchos Melachim 5:7, and in Teshuvos 4:1145) when he relates that for many years he lived in Egypt, where he founded a Yeshiva and taught Torah until he eventually left and came to Eretz Yisrael. He writes that he certainly acted in a permitted manner, because he did not settle there for the sake of living in Egypt, but in order to teach Torah to those who were already there, and he planned to leave when the opportunity arose.