@Separatrix's answer is correct: if the library is in the way during a conquest, it'll just be collateral damage, no avoiding it. However, their solution is to move the library into the mountains or deep into the desert.
However, there is a way of keeping the Library in Alexandria but "out of the way", though at the cost of geographic precision.
For that, we need to look at a map of ancient Alexandria.
The Library was a part of the "Museum", which is in the middle of the city and near the harbor. This made it a prime target for collateral damage: even if it isn't directly hit, any nearby fire could spread and destroy it (as actually happened).
So all we have to do is move the Library to another spot. My suggestion would be here:
Looking at the legend, we can see that the waterways marked as 5 and 6 are the ancient and present mouths of the Nile canal. If we handwave that away and pretend those both always existed, we effectively get an "island" separated from the city (with the west side closed off by the city walls).
This "island" is a useless target during a marine attack since it's nowhere close to the harbor, so it wouldn't be used by defenders to mount a defense.
In the case of a land-based assault, this flank of the city is incredibly defensible: attackers would need to either cross the exterior canal (6 on the map) or get funneled into the Library's walls (which, agreed, would be bad for the Library!), get past the walls, and then cross the interior canal. Much easier to attack the eastern walls.
So, with the exception of the case where attackers decide to funnel themselves over the Library's walls, it should be safe from any direct fire (especially if you change its architecture to make it a bad defensive position, so defenders aren't incentivized to make a last stand there). And then we just assume that attackers simply always agreed that any attack from the West flank of the city was suicidal and never attacked from there.
Lastly, all you have to do is make the canals wide enough to work as fire-breaks, so that if the city is on fire, the Library is safe.
Obviously, this solution is hardly perfect: putting the Library so close to the city walls makes it quite vulnerable to attacks on that flank, especially if attackers decide to dam the canal far from the city, eliminating most of the defensive advantage on that side of town, transforming the waterways into poorly designed dry moats. You can either pretend that never happened or change the canal into an actual powerful river, too hard to dam with ancient tech.