Error - Fixing "Bad CPU type in executable" Error on M1 Mac
Fixing “Bad CPU type in executable” Error on M1 Mac
Step 1: Install Rosetta 2 (If Not Already Installed)
bash
# Check if Rosetta is needed (if this returns something, Rosetta is installed)
1 |
/usr/bin/pgrep -q oahd && echo "Rosetta is installed" || echo "Rosetta is not installed" |
# Install Rosetta 2 if needed
1 |
softwareupdate --install-rosetta --agree-to-license |
Step 2: Clean Ruby Installation
# Remove existing Ruby installations
1 |
sudo rm -rf ~/.rbenv && sudo rm -rf ~/.rvm && sudo rm -rf /usr/local/opt/ruby && sudo rm -rf /usr/local/lib/ruby && sudo rm -rf /opt/homebrew/opt/ruby && sudo rm -rf /opt/homebrew/lib/ruby |
# Clean Homebrew Ruby and gems
1 2 3 |
brew uninstall ruby brew cleanup gem uninstall cocoapods |
Step 3: Install Ruby for ARM64
# Install Homebrew if not installed
1 |
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
# Add Homebrew to PATH for ARM64
1 2 |
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc source ~/.zshrc |
# Install Ruby for ARM64
1 |
brew install ruby |
# Add the new Ruby to your PATH
1 2 |
echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc source ~/.zshrc |
Step 4: Install CocoaPods for ARM64
# Install CocoaPods
1 2 |
sudo gem install -n /usr/local/bin cocoapods gem install cocoapods |
# Install ffi specifically for ARM64
1 |
gem install ffi |
Step 5: Configure Project
# Navigate to your project
1 |
cd /path/to/your/project |
# Clean existing pods
1 2 |
cd ios sudo rm -rf Pods && sudo rm -rf build && rm Podfile.lock && cd .. |
# Clean npm
1 2 |
sudo rm -rf node_modules rm package-lock.json |
# Reinstall dependencies
1 |
npm install |
# Install pods (using ARM64 native installation)
1 2 |
cd ios pod install --repo-update |
Step 6: Update Podfile
Add these configurations to your Podfile:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
def fix_config_for_m1(installer) installer.pods_project.build_configurations.each do |config| config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64" end installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['ONLY_ACTIVE_ARCH'] = 'YES' config.build_settings['VALID_ARCHS'] = 'arm64 arm64e x86_64' end end end post_install do |installer| react_native_post_install(installer) fix_config_for_m1(installer) # Additional M1 specific settings installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '12.4' config.build_settings["DEVELOPMENT_TEAM"] = "YOUR_TEAM_ID" # Replace with your team ID end end end |
Step 7: Environment Variables Setup
Add these to your ~/.zshrc or
~/.bash_profile:
# Ruby and CocoaPods paths
1 2 3 |
export PATH="/opt/homebrew/opt/ruby/bin:$PATH" export LDFLAGS="-L/opt/homebrew/opt/ruby/lib" export CPPFLAGS="-I/opt/homebrew/opt/ruby/include" |
# Pod configurations
1 |
export USE_RVM=true |
Troubleshooting Steps
1. If pod install still fails:
# Try with verbose logging
1 |
pod install --verbose |
# Or try with repo update
1 |
pod install --repo-update |
2. If specific gems fail:
# Install with specific architecture
1 |
arch -arm64 gem install cocoapods |
3. Check Ruby installation:
# Verify Ruby location
1 |
which ruby |
# Check Ruby version and architecture
1 2 |
ruby -v ruby -e 'puts RbConfig::CONFIG["host_cpu"]' |
4. Reset CocoaPods setup:
# Remove all CocoaPods caches
1 2 3 |
sudo rm -rf ${HOME}/Library/Caches/CocoaPods sudo rm -rf ${HOME}/.cocoapods/repos pod setup |
Important Notes
1. Architecture Verification:
- Use file /usr/local/bin/pod to check pod binary architecture
file /usr/local/bin/gem` to check gem binary architecture
- Use
2. Xcode Configuration:
– Ensure Xcode is running natively on ARM64
– Check Build Settings > Architectures > Valid Architectures includes “arm64”
3. Common Issues:
– Always run pod commands from the ios directory
– Keep your Xcode installation up to date
– Consider using rbenv or rvm for Ruby version management