BOSTON — Pablo Sandoval and Hanley Ramirez spent the last two seasons as NL West rivals. Now they’re teammates, signing with the Boston Red Sox as free agents to try to boost Boston out of the AL East cellar.

“It’s exciting for me to be with Hanley and David Ortiz,” Sandoval said Tuesday after the team announced a five-year deal that will add the former San Francisco Giants third baseman to a potent lineup he called “The Three Amigos.”

Boston was expected to announce Ramirez’s deal later Tuesday, according to a baseball official with knowledge of that agreement. He spoke on the condition of anonymity because Ramirez hadn’t yet passed a physical.

Sandoval helped the Giants win three titles, earning the World Series MVP in 2012 and the nickname “Kung Fu Panda” that helped cement him as a fan favorite. He thanked the Giants for bringing him up as a big leaguer and the fans for their support.

“I want a new challenge. I need a new challenge,” he said during a Fenway Park news conference. “I know that I had a great career in San Francisco. But I’m going to have a new one here.”

Sandoval helps fill a hole in the Red Sox lineup for a third baseman and a left-handed bat. Ramirez, who played shortstop and a little third base with the Marlins and Los Angeles Dodgers, is expected to move to left field.

“You’re always trying to get a sense of where they might fit in,” Red Sox manager John Farrell said when asked about potential lineups. “We’re not even at Thanksgiving yet. The potential for some other additions might exist.”

For now, the Red Sox are overloaded with outfielders and short on pitchers, having acquired Allen Craig and Yoenis Cespedes at the July trade deadline while shipping off four-fifths of the rotation. “We’ve got a ways to go in the offseason,” Farrell said.

Sandoval’s deal includes a team vesting option for 2020. The 28-year-old Venezuelan, who is listed at 5-foot-11 and 248 pounds, was seen as a potential replacement for Ortiz at designated hitter when he retires.

But Sandoval said he will manage his weight to remain in the field. “I’m going to be taking care of those things to play third base,” he said.

Sandoval said Ortiz gave him advice when he was in the minor leagues that he has carried with him. Having a chance to play with Ortiz, who was the Series MVP in 2013, was a factor that attracted him to Boston.

“To be Papi’s teammate — 162 games, all that with him — for me, it’s going to be a very exciting time,” said Sandoval, who had dinner with Ramirez on Monday night.

Sandoval is a career .294 hitter who had 16 homers and 73 RBIs in the regular season this year and then hit .366 in the postseason while helping the Giants win their third World Series in five years.

Red Sox general manager Ben Cherington said he was “a primary target” in the offseason.

“He really embodies a lot of what we care about,” Cherington said. “He’s been a big winner. He’s been a performer when it counts the most. He’s respected as a teammate, loves to play. We think he fits what we’re all about here. We’re excited to have him.”

Ramirez came up in the Red Sox system before he was traded to the Florida Marlins in the deal that brought Josh Beckett and future World Series MVP Mike Lowell to Boston. The 30-year-old batted .300 with 13 homers and 71 RBIs for Los Angeles this year.

To clear a roster spot for Sandoval, the Red Sox designated first baseman-catcher Ryan Lavarnway for assignment.