It's only officially done a few minutes prior to the beginning of the game. Some managers will (unofficially) release a lineup (or announce changes to an established lineup) to the media well ahead of a game. However, such a release doesn't bind the team. The manager may make changes to the lineup until the official presentation without penalty.
According to MLB Rules:
4.03 Exchange of Lineup Cards
Unless the home club shall have given previous notice that the game has been postponed or will be delayed in
starting, the umpire, or umpires, shall enter the playing field five
minutes before the hour set for the game to begin and proceed directly
to home base where they shall be met by the managers of the opposing
teams. In sequence:
(a) First, the home manager, or his designee,
shall give his batting order to the umpire-in-chief, in duplicate.
(b) Next, the visiting manager, or his designee, shall give his batting
order to the umpire-in-chief, in duplicate.
(d) The umpire-in-chief shall make certain that the original and
copies of the respective batting orders are identical, and then tender
a copy of each batting order to the opposing manager. The copy
retained by the umpire shall be the official batting order. The tender
of the batting order by the umpire shall establish the batting orders.
Thereafter, no substitutions shall be made by either manager, except
as provided in the rules.