1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

M2 MacでPythonインストールエラー

Last updated at Posted at 2025-03-26

Python実行時に、モジュールで変なエラーがでるようになってしまったので入れ直そうとしたらインストールで躓いてしまいました。

環境

  • M2 MacBookAir
  • OS Sonoma14.4
  • asdf

エラー概要

asdfでPythonのバージョンを管理しているのですが、Pythonのインストール時にBuild Failedとなってしまっていました。
色々といじったところ下記のエラーとなり、アーキテクチャに関連する問題が起きていることがわかりました。


Last 10 log lines:
      __locale_localeconv in _localemodule.o
      __locale_localeconv in _localemodule.o
      __locale_localeconv in _localemodule.o
      __locale_localeconv in _localemodule.o
  "_libintl_textdomain", referenced from:
      __locale_textdomain in _localemodule.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Programs/_freeze_module] Error 1
make: *** Waiting for unfinished jobs....
error installing version: failed to run install callback: exit status 1

原因

asdfはapple siliconだと/opt/homebrew/opt/配下にインストールされるはずなのですが、/usr/local/opt/配下にありました。

$ brew --prefix asdf
/usr/local/opt/asdf

このasdfを使ってPythonをインストールしようとすると、インストールに必要なモジュールにうまくリンクされず(?)エラーが起きていたようです。

これはIntel Mac用のHomebrewを使ってインストールされていることが原因なので、正しい場所にインストールし直すことが必要になります。

またHomebrewを確認してみると、archがarm64にも関わらずIntel Mac用のHomebrew
を参照するようになっていました。

brew configで設定を確認したところシステムがx86_64モードで動作していることがわかりました(M1が出たばかりのころに色々いじったのが起因していると思われます)。

$ which brew
brew: aliased to arch -arch x86_64 /usr/local/bin/brew

$ arch
arm64

解消

  1. まず、現在のHomebrewをアンインストール
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall.sh)"


# /usr/localの関連ディレクトリを削除
sudo rm -rf /usr/local/Homebrew
sudo rm -rf /usr/local/Caskroom
sudo rm -rf /usr/local/bin/brew

2.ARM64ネイティブモードに切り替え:

arch -arm64e /bin/zsh

3.ARM64用のHomebrewを新規インストール:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

4..zshrcに下記を追記

# Homebrew
eval "$(/opt/homebrew/bin/brew shellenv)"

5.設定を反映

source ~/.zshrc

6.brew configで設定確認

HOMEBREW_PREFIXが/opt/homebrewとなっていたらOKです。

あとは必要なパッケージをインストールして、Pythonを入れます。

terminal
brew install asdf
brew install gettext openssl@3 readline sqlite3 xz zlib tcl-tk
.zshrc
# asdf
. /opt/homebrew/opt/asdf/libexec/asdf.sh

# Python build configuration
export ARCHFLAGS="-arch arm64"
export LDFLAGS="-L/opt/homebrew/opt/gettext/lib -L/opt/homebrew/opt/openssl@3/lib -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/sqlite3/lib -L/opt/homebrew/opt/zlib/lib"
export CPPFLAGS="-I/opt/homebrew/opt/gettext/include -I/opt/homebrew/opt/openssl@3/include -I/opt/homebrew/opt/readline/include -I/opt/homebrew/opt/sqlite3/include -I/opt/homebrew/opt/zlib/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/gettext/lib/pkgconfig:/opt/homebrew/opt/openssl@3/lib/pkgconfig:/opt/homebrew/opt/readline/lib/pkgconfig:/opt/homebrew/opt/sqlite3/lib/pkgconfig:/opt/homebrew/opt/zlib/lib/pkgconfig"
terminal
asdf plugin add python
asdf install python 3.11.9

これで無事インストールできました🎉

Hyperなどの純正以外のターミナルを使うと変なところでうまくいかなかったりするので、なるべく純正のターミナルでインストールするのをおすすめします

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?