GNU compiler install on older versions of Mac OS X

Last modified by Xwiki VePa on 2024/02/07 07:37

After installing the GNU compilers, you may want to check out the Open MPIHOWTO for installing  on Mac OS X.

Background

One of the nice things about Mac OS X is that you have a polished user interface atop a Unix operating system. This means that using command line utilities such as compilers is straightforward, making coding on your Mac easy. If you have a multi-core Mac (most should by now) and would like to run codes that use MPI to distribute processing across multiple processor cores, you should also check out the Open MPIHOWTO for installing  on Mac OS X. Note that you'll need to follow the instructions on this page prior to installing Open MPI if you don't already have some set of compilers installed.

Requirements

To install the GNU compilers as described in this HOWTO, you'll need the following:

  • A Mac running 10.6 (Snow Leopard), 10.7 (Lion), 10.8 (Mountain Lion), 10.9 (Mavericks), or 10.10 (Yosemite)
  • An Apple ID
  • An Apple App Store account (for Mac OS 10.7-10.10)
  • Internet access
Instructions for Mac OS 10.6 (Snow Leopard) with Xcode 3

Installing the compilers is essentially a 2 step process.

  1. Download Xcode 3 from Apple. Xcode is Apple's software development environment, a set of tools that aim to make it easier to write Mac software. You will need to register as an Apple developer to download Xcode 3 , but this is free. Xcode 3 uses a standard Mac installer, so the install should be painless.
  2. Download the latest precompiled GNU compilers. That's right, precompiled compilers. Xcode 3 includes the GNU C and C++ compilers, but the versions are a bit dated. Fortunately, the folks who run the High Performance Computing for Mac OS X website keep recent versions of the GNU compilers available on their site. What's great is that the compilers are pre-built, meaning that all you have to do is download and extract the files to install. Below is a bit more detail on the install process.* Grab the latest archive of the GNU compilers from the HPC on Mac OS X site. I've linked to the Snow Leopard version. As of August 2011, PPC and Intel versions for Leopard are still available on the HPC on Mac OS X sourceforge site.

    • Launch Terminal.app (in /Applications/Utilities, hopefully you know that)
    • Change directories to where the GCC file was downloaded (usually, ~/Downloads)
    • If your browser didn't automatically unzip the file (still zipped if the file ends with .gz), you can unzip the file by typing

      gunzip gcc-snwleo-intel-bin.tar.gz
    • To install (as described on the HPC on Mac OS X site), type

      sudo tar xvf gcc-snwleo-intel-bin.tar -C /

      Beware that using sudo can do major damage to your computer if you aren't careful. Before hitting return, you should confirm you have typed things in exactly as listed above. In case you'd like to know what this set of commands does, sudo runs a program with administrator privileges and allows commands that follow to read/write/execute in directories normally protected from user modification. tar is an archive program and the flags xvf tell tar to extract files, give verbose output and the file name follows f. The -C flag tells the archive to be extracted to the root (/) directory, which puts the archived files in the /usr/local subdirectory, in accordance with how they are packaged in the tarfile.

    • To ensure the newly installed compilers are used by default, you'll want to add the following line to your shell resource file. You can determine which shell you are using by typing

      echo $SHELL

      in Terminal.

      If that returns /bin/bash (the default for Mac OS X since Panther, I believe), type the following:

      echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile

      If you are using a terminal emulator other than Terminal.app or iTerm2 on your Mac (such as xterm in the Xquartz package), you may find that you need to type the following to find the newly installed compilers.

      echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc

      If that returns /bin/tcsh or /bin/csh, you should type

      echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.tcshrc

      for /bin/tcsh or

      echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.cshrc

      for /bin/csh.

    • You should now be able to confirm the compilers are properly installed by opening a new Terminal window and typing gcc -v, g++ -v or gfortran -v.
Instructions for Mac OS 10.7 (Lion) with Xcode 4

Similar to the Snow Leopard instructions, installing the compilers is essentially a 2 step process.

  1. Install Xcode 4 from the Mac App Store. Xcode is Apple's software development environment, a set of tools that aim to make it easier to write Mac software. After you launch the App Store application (/Applications/App Store), you'll need to sign in with your Apple ID, or create an account to download anything. Xcode 4 is available for free (search for 'xcode'), but you'll still need to supply your credit card info.
    • For Xcode 4.2 and earlier, you'll find an Install Xcode application in your /Applications folder after the download completes. Xcode 4 uses a standard Mac installer, so the install should be painless. Feel free to delete the Install Xcode application after the Xcode 4 installation process completes, but note that you may want to keep a backup of the installer, as suggested on Apple's Developer site.
    • For Xcode 4.3 and later, you'll find that the Xcode application is installed directly in your /Applications directory and there is no longer an installer program. However, because there is no installer, there is one critical additional step needed to install the compliers as directed below.
      • After Xcode is installed in /Applications, launch Xcode. Note that you may be prompted to uninstall earlier versions if they exist.
      • In the menu bar, click on Xcode→Preferences...
      • Click on the Downloads tab and under Components, click on the Install button to the right of Command Line Tools. This will install the tools needed to proceed with the compiler installation below.
  2. Download the latest precompiled GNU compilers. That's right, precompiled compilers. Xcode includes the GNU C and C++ compilers, but the versions are often a bit dated. Fortunately, the folks who run the High Performance Computing for Mac OS X website keep recent versions of the GNU compilers available on their site. What's great is that the compilers are pre-built, meaning that all you have to do is download and extract the files to install. Below is a bit more detail on the install process.* Grab the latest archive of the GNU compilers from the HPC on Mac OS X site (I've linked to the Lion version, but older versions are also available - see  instructions for Mac OS 10.6 ).

    • Launch Terminal.app (in /Applications/Utilities, hopefully you know that)
    • Change directories to where the GCC file was downloaded (usually, ~/Downloads)
    • If your browser didn't automatically unzip the file (still zipped if the file ends with .gz), you can unzip the file by typing

      gunzip gcc-lion.tar.gz
    • To install (as described on the HPC on Mac OS X site), type

      sudo tar xvf gcc-lion.tar -C /

      Beware that using sudo can do major damage to your computer if you aren't careful. Before hitting return, you should confirm you have typed things in exactly as listed above. In case you'd like to know what this set of commands does, sudo runs a program with administrator privileges and allows commands that follow to read/write/execute in directories normally protected from user modification. tar is an archive program and the flags xvf tell tar to extract files, give verbose output and the file name follows f. The -C flag tells the archive to be extracted to the root (/) directory, which puts the archived files in the /usr/local subdirectory, in accordance with how they are packaged in the tarfile.

    • To ensure the newly installed compilers are used by default, you'll want to add the following line to your shell resource file. You can determine which shell you are using by typing

      echo $SHELL

      in Terminal.

      If that returns /bin/bash (the default for Mac OS X since Panther, I believe), type the following:

      echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile

      If you are using a terminal emulator other than Terminal.app or iTerm2 on your Mac (such as xterm in the Xquartz package), you may find that you need to type the following to find the newly installed compilers.

      echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc

      If that returns /bin/tcsh or /bin/csh, you should type

      echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.tcshrc

      for /bin/tcsh or

      echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.cshrc

      for /bin/csh.

    • You should now be able to confirm the compilers are properly installed by opening a new Terminal window and typing gcc -vg++ -v or gfortran -v.
Instructions for Mac OS 10.8 (Mountain Lion) with Xcode 4

These instructions are quite similar the instructions for Lion (above), but I've included them here anyway.

  1. Install Xcode 4 from the Mac App Store. Xcode is Apple's software development environment, a set of tools that aim to make it easier to write Mac software. After you launch the App Store application (/Applications/App Store), you'll need to sign in with your Apple ID, or create an account to download anything. Xcode 4 is available for free (search for 'xcode'), but you'll still need to supply your credit card info.
    • When you download it from the App Store, Xcode 4.5 (the current version as of September 2012) will be installed directly in your /Applications directory. However, before you can proceed with the GNU compiler install you must perform one critical additional step.
      • Launch  Xcode . Note that you may be prompted to uninstall earlier versions if they exist or to update various components.


      • In the menu bar, click on Xcode→Preferences...
      • Click on the Downloads tab and under Components, click on the Install button to the right of Command Line Tools. This will install the tools needed to proceed with the compiler installation below.
  1. Download the latest precompiled GNU compilers. That's right, precompiled compilers. Xcode includes the GNU C and C++ compilers, but the versions are often a bit dated. Fortunately, the folks who run the High Performance Computing for Mac OS X website keep recent versions of the GNU compilers available on their site. What's great is that the compilers are pre-built, meaning that all you have to do is download and extract the files to install. Below is a bit more detail on the install process.* Grab the latest archive of the GNU compilers from the HPC on Mac OS X site (I've linked to the Mountain Lion version, but older versions are also available - see instructions for Mac OS 10.6, instructions for Mac OS 10.7).

    • Launch Terminal.app (in /Applications/Utilities, hopefully you know that)
    • Change directories to where the GCC file was downloaded (usually, ~/Downloads)
    • If your browser didn't automatically unzip the file (still zipped if the file ends with .gz), you can unzip the file by typing

      gunzip gcc-mlion.tar.gz
    • To install (as described on the HPC on Mac OS X site), type

      sudo tar xvf gcc-mlion.tar -C /

      Beware that using sudo can do major damage to your computer if you aren't careful. Before hitting return, you should confirm you have typed things in exactly as listed above. In case you'd like to know what this set of commands does, sudo runs a program with administrator privileges and allows commands that follow to read/write/execute in directories normally protected from user modification. tar is an archive program and the flags xvf tell tar to extract files, give verbose output and the file name follows f. The -C flag tells the archive to be extracted to the root (/) directory, which puts the archived files in the /usr/local subdirectory, in accordance with how they are packaged in the tarfile.

    • To ensure the newly installed compilers are used by default, you'll want to add the following line to your shell resource file. You can determine which shell you are using by typing

      echo $SHELL

      in Terminal.

      If that returns/bin/bash(the default for Mac OS X since Panther, I believe), type the following:

      echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile

      If you are using a terminal emulator other than Terminal.app or iTerm2 on your Mac (such as xterm in the Xquartz package), you may find that you need to type the following to find the newly installed compilers.

      echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc

      If that returns /bin/tcsh or /bin/csh, you should type

      echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.tcshrc

      for /bin/tcsh or

      echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.cshrc

      for /bin/csh.

    • You should now be able to confirm the compilers are properly installed by opening a new Terminal window and typing gcc -vg++ -v or gfortran -v.
Instructions for Mac OS 10.9 (Mavericks) with Xcode 5

  1. Install Xcode 5 from the Mac App Store. Xcode is Apple's software development environment, a set of tools that aim to make it easier to write Mac software. After you launch the App Store application (/Applications/App Store), you'll need to sign in with your Apple ID, or create an account to download anything. Xcode 5 is available for free (search for 'xcode'), but you'll still need to supply your credit card info if you haven't already used your account.
    • When you download it from the App Store, Xcode 5.0 (the current version as of December 2013) will be installed directly in your /Applications directory. However, before you can proceed with the GNU compiler install you must perform one critical additional step.
      • Launch Xcode and install any available updates. Quit Xcode.


      • Launch Terminal.app (in /Applications/Utilities, hopefully you know that)
      • Install the Command Line Tools for OS X Mavericks by typing

        xcode-select --install 

        This will open a dialog box to install the Command Line Tools (OS X Mavericks) for Xcode package. Install following the standard procedure.

  1. Download the latest precompiled GNU compilers. That's right, precompiled compilers. Xcode includes the GNU C and C++ compilers, but the versions are sometimes a bit dated. Fortunately, the folks who run the High Performance Computing for Mac OS X website keep recent versions of the GNU compilers available on their site. What's great is that the compilers are pre-built, meaning that all you have to do is download and extract the files to install. Below is a bit more detail on the install process.* Grab the latest archive of the GNU compilers from the HPC on Mac OS X site

    • In Terminal.app, change directories to where the GCC file was downloaded (usually, ~/Downloads)
    • If your browser didn't automatically unzip the file (still zipped if the file ends with .gz), you can unzip the file by typing

      gunzip gcc-4.9-bin.tar.gz
    • To install (as described on the HPC on Mac OS X site), type

      sudo tar xvf gcc-4.9-bin.tar -C /

      Beware that using sudo can do major damage to your computer if you aren't careful. Before hitting return, you should confirm you have typed things in exactly as listed above. In case you'd like to know what this set of commands does, sudo runs a program with administrator privileges and allows commands that follow to read/write/execute in directories normally protected from user modification. tar is an archive program and the flags xvf tell tar to extract files, give verbose output and the file name follows f. The -C flag tells the archive to be extracted to the root (/) directory, which puts the archived files in the /usr/local subdirectory, in accordance with how they are packaged in the tarfile.

    • To ensure the newly installed compilers are used by default, you'll want to add the following line to your shell resource file. You can determine which shell you are using by typing

      echo $SHELL

      in Terminal.

      If that returns /bin/bash (the default for Mac OS X since Panther, I believe), type the following:

      echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile

      If you are using a terminal emulator other than Terminal.app or iTerm2 on your Mac (such as xterm in the Xquartz package), you may find that you need to type the following to find the newly installed compilers.

      echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc

      If that returns /bin/tcsh or /bin/csh, you should type

      echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.tcshrc

      for /bin/tcsh or

      echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.cshrc

      for /bin/csh.

    • You should now be able to confirm the compilers are properly installed by opening a new Terminal window and typing gcc -vg++ -v or gfortran -v.
Instructions for Mac OS 10.10 (Yosemite) with Xcode 6

  1. Install Xcode 6 from the Mac App Store. Xcode is Apple's software development environment, a set of tools that aim to make it easier to write Mac software. After you launch the App Store application (/Applications/App Store), you'll need to sign in with your Apple ID, or create an account to download anything. Xcode 6 is available for free (search for 'xcode'), but you'll still need to supply your credit card info if you haven't already used your account.
    • When you download it from the App Store, Xcode 6.1 (the current version as of November 2014) will be installed directly in your /Applications directory. However, before you can proceed with the GNU compiler install you must perform one critical additional step.
      • Launch Xcode and install any available updates. Quit Xcode.


      • Launch Terminal.app (in /Applications/Utilities, hopefully you know that)
      • Install the Command Line Tools for OS X Yosemite by typing

        xcode-select --install 

        This will open a dialog box to install the Command Line Tools (OS X Yosemite) for Xcode package. Install following the standard procedure. 

  1. Download the latest precompiled GNU compilers. That's right, precompiled compilers. Xcode includes the GNU C and C++ compilers, but the versions are sometimes a bit dated. Fortunately, the folks who run the High Performance Computing for Mac OS X website keep recent versions of the GNU compilers available on their site. What's great is that the compilers are pre-built, meaning that all you have to do is download and extract the files to install. Below is a bit more detail on the install process.* Grab the latest archive of the GNU compilers from the HPC on Mac OS X site

    • In Terminal.app, change directories to where the GCC file was downloaded (usually, ~/Downloads)
    • If your browser didn't automatically unzip the file (still zipped if the file ends with .gz), you can unzip the file by typing

      gunzip gcc-4.9-bin.tar.gz
    • To install (as described on the HPC on Mac OS X site), type

      sudo tar xvf gcc-4.9-bin.tar -C /

      Beware that using sudo can do major damage to your computer if you aren't careful. Before hitting return, you should confirm you have typed things in exactly as listed above. In case you'd like to know what this set of commands does, sudo runs a program with administrator privileges and allows commands that follow to read/write/execute in directories normally protected from user modification. tar is an archive program and the flags xvf tell tar to extract files, give verbose output and the file name follows f. The -C flag tells the archive to be extracted to the root (/) directory, which puts the archived files in the /usr/local subdirectory, in accordance with how they are packaged in the tarfile.

    • To ensure the newly installed compilers are used by default, you'll want to add the following line to your shell resource file. You can determine which shell you are using by typing

      echo $SHELL

      in Terminal.

      If that returns /bin/bash (the default for Mac OS X since Panther, I believe), type the following:

      echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bash_profile

      If you are using a terminal emulator other than Terminal.app or iTerm2 on your Mac (such as xterm in the Xquartz package), you may find that you need to type the following to find the newly installed compilers.

      echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc

      If that returns /bin/tcsh or /bin/csh, you should type

      echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.tcshrc

      for /bin/tcsh or

      echo 'setenv PATH /usr/local/bin:$PATH' >> ~/.cshrc

      for /bin/csh.

    • You should now be able to confirm the compilers are properly installed by opening a new Terminal window and typing gcc -vg++ -v or gfortran -v.
Tips & Warnings

I mention all but the last of these tips and warnings in the text above, but it doesn't hurt to list them a second time...

  • Beware that using sudo can do major damage to your computer if you aren't careful.
  • If you are using Xcode 4 version 4.3 or later, I'll remind you that before you can proceed with the GNU compiler install you must perform one critical additional step.
    • Launch Xcode. Note that you may be prompted to uninstall earlier versions if they exist or to update various components.

    • In the menu bar, click on Xcode→Preferences...
    • Click on the Downloads tab and under Components, click on the Install button to the right of Command Line Tools. This will install the tools needed to proceed with the compiler installation below.
  • Note that after installing Xcode 5 (or later), you also need to perform a critical additional step.
    • Launch Xcode and install any available updates. Quit Xcode.

    • Launch Terminal.app (in /Applications/Utilities, hopefully you know that)
    • Install the Command Line Tools for OS X by typing

      xcode-select --install 

      This will open a dialog box to install the Command Line Tools for Xcode package. Install following the standard procedure.

  • If the correct version of gcc is not being found at the command line after installation, it is possible that you're using a terminal emulator that reads the .bashrc file rather than the .bash_profile file. To confirm, do the following:

    • Open a new terminal window using your terminal emulator of choice. This will reread the either the .bash_profile file or the .bashrc. If the new gcc version is not returned when typing

      gcc -v

      then your terminal emulator may be reading the .bashrc file.

    • You can fix this one of two ways*

      Create a symbolic link called .bashrc that points to .bash_profile by typing

      ln -s ~/.bash_profile ~/.bashrc
      • Modify the order of directories that are searched for commands by typing

        echo 'export PATH=/usr/local/bin:$PATH' >> ~/.bashrc
Related