The deal is for six years, sources told ESPN's Adam Schefter.
Ryans will be officially introduced as the franchise's sixth full-time coach on Thursday. The Texans, meanwhile, become the first team in NFL history to have hired three consecutive Black coaches.
Ryans had his first interview with the Texans on Jan. 20 and met again with the franchise on Tuesday. Other NFL teams that had requested permission to talk with Ryans about head-coaching vacancies were the Broncos, Panthers and Colts.
"For so many reasons, DeMeco is everything we are looking for in a leader and coach for our organization," Texans owner Cal McNair said in a statement. "He has a proven track record for developing high-quality players and constantly innovated his defensive scheme over the last six seasons in San Francisco.
"We started this process with the goal of finding someone our fans and city can be proud of and we've done just that. While we received a lot of incredible feedback and support for DeMeco from different people, it was our discussions with him and his vision for our football team that made him the right coach to lead us as we continue to evolve as an organization."
Ryans' hiring ends a three-week search since the Texans fired Lovie Smith on Jan. 8. Ryans, a former Texans standout linebacker, will be the fourth coach in Houston in four years after Smith and David Culley were each fired after one season.
Former coach and general manager Bill O'Brien was fired after starting the 2020 season 0-4. O'Brien led the Texans for six full seasons before that, and his 52-48 record (.520) leaves him as the only Texans coach over .500.
The franchise hired Nick Caserio as GM in 2021. Caserio hired Culley (4-13) in 2021 and promoted Smith (3-13-1) from defensive coordinator in 2022.
Now Caserio has hired Ryans, who took over the 49ers' defensive unit in 2021.
"DeMeco is a proven coach with a track record of success who has an innate ability to lead people," Caserio said in a statement. "... We are working to build a sustainable program that has long-term success and DeMeco is the coach we feel is the best fit to help us achieve our goals. We know how important it is to get results now and we have a lot of work to do, but I'm excited to partner with DeMeco to build our football team together."
In his first season, the 49ers allowed the third-fewest yards per game (310), and in 2022, San Francisco allowed the fewest points (16.8) and yards (300.6) per game. Ryans' unit also tied for the second-most takeaways (30).
Ryans' defense helped lift the 49ers to a 13-4 record in the regular season before they fell to the Philadelphia Eagles 31-7 in the NFC Championship Game as the No. 2 seed.
Before becoming a coach, Ryans had a 10-year career with the Texans and Eagles. He was a second-round pick out of Alabama by the Texans in 2006 and was named Defensive Rookie of the Year after finishing second in league tackles (156). His 126 solo tackles were second most for a rookie in NFL history.
The following season, Ryans earned second-team All-Pro and Pro Bowl honors. The two-time Pro Bowler played for the Texans for six seasons and is currently the franchise's all-time leader in tackles (479).
"Being the head coach of the Houston Texans is my dream job and my family is thrilled to be back in H-Town," Ryans said in a statement. "I have been around the game of football my entire life and I've always had a natural ability to lead others. I know what it takes to win and be successful in this league as both a player and coach.
"We're going to build a program filled with players who have a special work ethic and relentless mindset. I understand the responsibility I have to this organization and to the fans of Houston to build a winner and I can't wait to get to work."
Ryans, 38, has spent the past six years coaching; his first role was a defensive quality control coach for the 49ers in 2017 under coach Kyle Shanahan. In 2018, he became the inside linebackers coach, and was elevated to defensive coordinator when Robert Saleh left to become the New York Jets' head coach in 2021.
"I'd like to first thank the San Francisco 49ers, Kyle Shanahan and John Lynch for six incredible seasons where I grew as a coach and a man," Ryans said. "I couldn't be prouder of what we accomplished together, and I will always cherish the relationships I built there."
One of the biggest tasks for Ryans will be fixing the league's worst rush defense, which allowed 170.2 yards per game. No team has allowed over 170 rushing yards since the 0-16 Lions (172.1) in 2008. The Texans also have finished 27th in points allowed per game three seasons in a row.
The pass defense was a bright spot for a team that allowed the third-most total yards per game (379.5). The Texans finished 10th in the league in passing yards allowed per game (209.3) and had more interceptions (16) than touchdowns allowed (15).
The team's 16 interceptions were tied for fourth, and the 15 touchdowns allowed were tied for the fewest.
Beyond fixing the defense, Ryans must fix the Texans' offensive struggles and find an answer at quarterback.
The unit averaged 17 points (30th), and the Texans' 19 interceptions were second most. Opening-day starting quarterback Davis Mills had 15 of those in 15 games and was tied for the league high, and Kyle Allen, who started when Mills was benched, threw the other four in two games.
The Texans have two first-round draft picks (No. 2 and No. 12 overall) in the 2023 NFL draft and could use one of those on a quarterback. They also could address the position via trade or free agency. The NFL informed teams on Monday that the salary cap will increase to $224.8 million for the 2023 league year. The Texans are estimated to have about $40 million in cap space.