33

Tried to install a gem on Mountain Lion and make couldn't find gcc-4.2:

kamil$ gem install posix-spawn -v '0.3.6'
Building native extensions.  This could take a while...
ERROR:  Error installing posix-spawn:
    ERROR: Failed to build gem native extension.

        /Users/kamil/.rbenv/versions/1.9.3-p0/bin/ruby extconf.rb
creating Makefile

make
compiling posix-spawn.c
make: gcc-4.2: No such file or directory
make: *** [posix-spawn.o] Error 1
ktusznio
  • 3,585
  • 3
  • 22
  • 21

4 Answers4

90

If you have Xcode installed, gcc should be available. Check where it is with:

kamil$ which gcc
/usr/bin/gcc

Then make a user-land symbolic link from gcc-4.2 to plain gcc:

kamil$ sudo ln -s ~/bin/gcc /usr/bin/gcc-4.2

(Ensure the user-land bind folder is in your path via export PATH=...:$HOME/bin in your .bash_profile or .zshrc.)

Gem installed fine afterwards.

ktusznio
  • 3,585
  • 3
  • 22
  • 21
  • 2
    It boggles my mind why this isn't set by default when you install Command Line Tools. I've spent so much time trying to get this work and finally came across your answer. Is this a Ruby issue or an Xcode issue where these operations fail even with gcc installed? – Richard D Aug 18 '13 at 14:45
  • If you get a 'permission denied' error when you try to run 'ln -s /usr/bin/gcc /usr/bin/gcc-4.2' 1) run 'sudo chmod 755 /usr/bin/gcc' 2) run 'sudo ln -s /usr/bin/gcc /usr/bin/gcc-4.2' – ATSiem Mar 08 '14 at 17:24
  • 1
    The problem with this is that the gcc version may not actually be 4.2. It'll function as a work around but this seems like a really hacky solution. It'd probably be better to actually install the correct version of gcc with homebrew as @Piioo suggests. – caspian311 Mar 26 '14 at 13:25
  • 1
    This will not work with any modern OSX version due to `System Integrity Protection`. – Artur Bodera Mar 03 '16 at 16:36
  • @fivenp thanks, I updated my answer to create the symlink in user-land as you recommended in your answer. – ktusznio Apr 30 '16 at 21:58
  • I don't think the updated answer works. My solutions is `sudo ln -s /usr/bin/gcc /usr/local/bin/gcc-4.2` – Mr. Ronald Dec 24 '18 at 16:00
  • @Mr.Ronald although I can make a link in `/usr/local/bin`, this is not picked up in the installation process I'm following (doing a `pip install`), although `which gcc-4.2` shows the link in `/usr/local/bin`. – Visionscaper Jul 11 '20 at 12:44
14

Install simply apple-gcc42 with brew. It generate gcc-4.2 .

brew install apple-gcc42

So we do not need symlinks, which apple update may remove.

Homebrew

Piioo
  • 759
  • 10
  • 24
1

As @Artur Bodera mentioned modern OSX will refuse to let you create the symlink in the systems /bin folder.

To avoid this simply create the symlink to your users bin folder

ln -s ~/bin/gcc /usr/bin/gcc-4.2

Don't forget to add the bin folder to your .zshrc or .bash_profile - e.g.

export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$HOME/bin
fivenp
  • 26
  • 2
  • the first command gives me a - operation not permitted. and presently my .bash_profile has - export PATH="/usr/local/bin:$PATH". Would updating it be safe? I am on OSX 10.12.4 – Alex Jose Apr 20 '17 at 06:35
  • Apples System Integrity Protection from newer MacOS versions doesn't let you symlink to /usr/bin but /usr/local/bin works. `sudo ln -s /usr/bin/gcc /usr/local/bin/gcc-4.2` – Mr. Ronald Dec 24 '18 at 15:57
1

I had a similar issue while installing a python pip package (building a wheel failed). I got the similar message:

unable to execute '/usr/bin/gcc-4.2': No such file or directory
  error: command '/usr/bin/gcc-4.2' failed with exit status 1

Linking /usr/bin/gcc-4.2 to /usr/bin/gcc was not possible due to Apples System Integrity Protection (SIP), and linking it to /usr/local/bin/gcc-4.2 was not picked up by the wheel building process; it was still trying to use /usr/bin/gcc-4.2.

I was finally able to solve this by setting the CC variable in the terminal:

CC=/usr/bin/gcc
# Install your packages
pip install -r requirements.txt

PS : note that disabling SIP doesn't work, even with SIP disabled I wasn't able to create the /usr/bin/gcc-4.2 link.

Visionscaper
  • 3,979
  • 1
  • 23
  • 26