35

I successfully built/installed NumPy on my mac os x for python 2.7.3. Now I would like to build/install scipy as well. I downloaded it from git hub. Went into the directory. Ran python setup.py build and it seemed to be working until it came across this error:

customize Gnu95FCompiler
Could not locate executable gfortran
Could not locate executable f95
customize NAGFCompiler
customize AbsoftFCompiler
Could not locate executable f90
Could not locate executable f77
customize IBMFCompiler
Could not locate executable xlf90
Could not locate executable xlf
customize IntelFCompiler
Could not locate executable ifort
Could not locate executable ifc
customize GnuFCompiler
Could not locate executable g77
customize G95FCompiler
Could not locate executable g95
customize PGroupFCompiler
Could not locate executable pgfortran
don't know how to compile Fortran code on platform 'posix'
building 'dfftpack' library
error: library dfftpack has Fortran sources but no Fortran compiler found

I thought that I had Fortran installed for NumPy...guess not? How would I download it?

Megan
  • 622
  • 1
  • 10
  • 16
  • Do you need the latest version? Why not use easy_install or pip? – BenDundee Feb 11 '13 at 21:40
  • I tried pip when I was installing NumPy and I got confused, I downloaded homebrew and after that Numpy worked. – Megan Feb 11 '13 at 21:42
  • 2
    Let me say: if you plan on doing anything in Python, you probably want to figure pip out. – BenDundee Feb 11 '13 at 21:43
  • Are you using Apple's pre-installed Python, or a different one (python.org, Homebrew, etc.)? The answer is different in different cases (although I'm pretty sure there's a dup for each case). – abarnert Feb 11 '13 at 22:12
  • Also, do you actually _need_ whichever Python 2.7.3 you installed from some other source, as opposed to Apple's pre-installed 2.7.2? Because life will be a lot easier if you uninstall it and stick with just one Python 2.7 on your path. – abarnert Feb 11 '13 at 22:56
  • i dont necessarily need python 2.7.3 i did for my class last semester, i downloaded it i believe. I did know that mac came with a pre-installed version but I don't think there are 2 paths, I think it removed the other version before updating. – Megan Feb 12 '13 at 00:10
  • oh...i actually just ran $brew doctor, and it looks like i may have 2 paths, which is probably why i have ran into so many errors – Megan Feb 12 '13 at 00:13
  • i have python2.7 and pythonw2.7 – Megan Feb 12 '13 at 00:13

10 Answers10

94

Your problem is that you need to install a Fortran compiler to build scipy.

Also, if you already have a numpy that's built with Fortran support disabled, you may have to replace it. Some of Apple's pre-installed Python versions have such a numpy build pre-installed.


The easiest way to get Fortran is with Homebrew. As the docs say, you need to install Xcode and its Command Line Tools first. (The way to install the Command Line Tools changes with almost each major version of Xcode, so see the linked docs for an up-to-date explanation.) Then install Homebrew. The installation URL has changed a few times, so see the Homebrew home page or installation instructions(http://brew.sh/), but it will be something like:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then:

brew install gcc

(Note that until some time in 2014, gfortran was a separate recipe from gcc, so the command was brew install gfortran. But if you try that now, you'll get an error saying "GNU Fortran is now provided as part of GCC, and can be installed with: brew install gcc".)


You really want to use pip to install scipy, so if you don't have that, get it first. Apple's pre-installed Python, at least in 10.7 and 10.8, includes easy_install but not pip, so the easiest way to do that is:

sudo easy_install pip

However, you may want to consider using a virtualenv instead of a global install (in which case you also want to remove the sudo on the following commands).

Now that you've got gfortran and pip, all you have to do is this:

sudo pip install --upgrade numpy
sudo pip install scipy

Caveats:

  • The instructions above are for Apple's pre-installed version(s) of Python. If you're using a different version of Python, you really should consider not doing so. Keeping the paths, installed packages, etc. in sync is a nightmare. The exception to this is if you want a Python 3.x version, in which case installing it from python.org or Homebrew is perfectly reasonable. There will be no collisions, because python, pip2.7, etc. will be for Apple's Python; python3, pip3.3, etc. for the 3.x version.

  • If you already have pip, but fear it may be out of date, pip install --upgrade pip. (Besides the security and robustness benefits, this may save you a whole lot of time by making you compatible with binary wheels for some of the scientific stack or other modules.)

  • For most non-Apple Python installations (and maybe even Apple's in 10.9 or 10.10; I haven't checked), you should not use easy_install to install pip. Follow the pip install instructions. But first make sure you don't already have it.

    • If you're using virtualenv/venv, your virtual environments will already include pip.
    • Python 3.4 or later may (and will, if from a python.org installer) include a pip bootstrap. If your 3.4+ doesn't already have pip, you may want to python -m ensurepip to install it.
    • Some third-party installs, like Homebrew or ActiveState, include pip.
  • For Python 3.3 or later, you may want to use the built-in venv instead of virtualenv.

  • If you're using MacPorts, Fink, gentoo-alt, etc., you should install the scipy package that comes with your package manager, and it will drag in whatever else it needs (maybe even including rebuilding Python and GCC).

  • Third-party binary installs like Enthought and ActiveState may already include scipy and everything else you need. If not, the instructions are basically the same as above, but you'll have to guess which steps to skip or follow, whether to sudo, etc.


If you're using a non-Apple build of Python 2.7, and you want to avoid the PATH problems, you have to do two things:

First, do not, ever, install any Python packages that include scripts or binaries (including pip itself) in more than one Python. For example, if you install ipython for both Apple 2.7 and Homebrew 2.7, both will attempt to create scripts named /usr/local/bin/ipython and /usr/local/bin/ipython-2.7. If you're lucky, one install will fail. Otherwise, they'll both succeed, one will end up overwriting the other, and you will have no way of running the overwritten version.

Second, make sure the path to the alternate Python's scripts and binaries comes before Apple's in the PATH. Depending on which alternate Python you've installed and which instructions you followed, this could be:

  • /usr/local/bin
  • /Library/Frameworks/Python.framework/Versions/2.7/bin
  • /usr/local/share/python2.7/bin
  • /usr/local/Cellar/python/2.7.3/bin
  • something else

Whatever the path is, you need to edit your PATH variable.

If you want to affect GUI apps (and LaunchAgents, etc.), there is apparently no longer a supported way to do this, but the deprecated QA1067 does seem to still work in Lion. It's also what the Homebrew FAQ and Python FAQ suggest.

If you only care about command-line sessions (both Terminal.app and remote ssh), you can instead just do the standard Unix thing of editing the appropriate profile file. Which profile file is appropriate depends on what you want to affect. (All users or just one user? bash or any shell? And so on.) If you don't know which one you want, you really should do some research. If you don't want to bother learning, just do ~/.profile and then don't complain if it wasn't what you wanted.

Either way, you need to make sure that the appropriate path comes before /usr/bin in the PATH. So, for example, you could add the following to ~/.profile:

PATH=/usr/local/bin:$PATH
export PATH

(You will of course need to either create a new Terminal shell, or source the script, before it takes effect.)

If you're using homebrew, brew doctor will tell you if you got it right.

KyungHoon Kim
  • 2,859
  • 2
  • 23
  • 26
abarnert
  • 354,177
  • 51
  • 601
  • 671
  • awesomely useful post, had been avoiding installing scipy locally no my laptop until now, the way they advertise on the scipy website (using macports) failed for me but this worked perfectly – qwwqwwq Aug 02 '13 at 20:42
  • 1
    the command to install a fortran compiler is now `brew install gcc` – jjfine Nov 21 '14 at 16:45
  • 1
    @jjfine: That's true now, but it wasn't true 21 months ago when the asker asked this question. (But I'll update the answer for future readers.) – abarnert Nov 21 '14 at 19:25
  • 3
    @jjfine: I updated various out-of-date links, URLs, version numbers, etc. while I was at it. – abarnert Nov 21 '14 at 19:37
  • I know I can use your answer with brew to fix things (or I hope I can), but why does fotran compiler not exist if I separately pip3 installed numpy? Like I did `pip3 install numpy` and it still said fotrand compiler wasn't found. – Charlie Parker Jan 11 '17 at 00:47
  • I have `gcc` installed before, after reinstall `gcc` with homebrew, everything works fine now. – Kxrr Apr 11 '18 at 04:32
3

It looks like your actual problem was just an intermittent download failure from Sourceforge:

curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume. Error: Download failed: downloads.sf.net/project/machomebrew/Bottles/…

Homebrew should just recover automatically from this error if you try brew install gfortran again. So, that's the first thing to try.

If that doesn't work, see if brew doctor finds any problems, then brew install -d gfortran to see where it's storing the partially-downloaded file so you can delete it manually and try again.

If all else fails, you can force it to not use the bottle by using --build-from-source. Of course building from source takes a lot longer than installing a binary bottle, but it should give the same result.

abarnert
  • 354,177
  • 51
  • 601
  • 671
3

As of 5/20/2014, if you're using brew, Fortran is installed as part of gcc. There is no separate Fortran package required. Here's what worked for me to install numpy:

  1. install brew, as per abamert's answer (or see http://brew.sh/ )
  2. install gcc ( brew install gcc )
  3. confirm that brew's gcc is the correct one ( which gcc should point to /usr/local/bin/gcc )
  4. export ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future (see Problems with pip install numpy - RuntimeError: Broken toolchain: cannot link a simple C program )
  5. pip install numpy
Community
  • 1
  • 1
Mark Chackerian
  • 21,866
  • 6
  • 108
  • 99
2

I don't think this problem is too complicated at all.

1) Install pip

cd /Library/Frameworks/Python.framework/Versions/ ln -s 2.7 Current

cd /usr/bin rm -f python ln -s /Library/Frameworks/Python.framework/Versions/Current/bin/python python

2) download egg from https://pypi.python.org/pypi/setuptools#files sudo sh setuptools-0.6c11-py2.7.egg

3) Simply download and install the gfotran from this link: http://r.research.att.com/tools/

4) After that, you need to type: sudo pip install -U scipy sudo pip install -U numpy sudo pip install -U matplotlib

Hopefully, you should have everything you want to have.

BITTNT
  • 91
  • 1
  • 1
1

I met the same problem, using following steps:
1. brew install gfortran
2. pip install scipy

then it's ok.

rchardzhu
  • 51
  • 2
1

I had gfortran installed and the binary was gfortran-4.2. So, everytime I was trying to install SciPy the compiler couldn't be found because it was trying to use gfortran.

What I did was create a symlink of gfortran-4.2 to gfortran.

To find where you should create the symlink run:

$ which gfortran-4.2
/usr/local/bin/gfortran-4.2

Then symlink it:

ln -s /usr/local/bin/gfortran-4.2 /usr/local/bin/gfortran
André Augusto
  • 1,345
  • 1
  • 7
  • 10
0

For OSX yosemite and possibly older osx releases too, you may want to download and install the fortran compiler before running pip install scipy. To download the latest fortran compiler, just go to the following link:

https://gcc.gnu.org/wiki/GFortranBinaries#MacOS

Masum
  • 1,678
  • 15
  • 19
0

I think your version of Xcode and OSX matter. I had Xcode: 6.4 and Os X 10.11.1 and was still getting the same error messages (could not locate gfortran, etc). I tried many things, including the thorough response by @abarnert, but the solution for me was upgraded Xcode (to 7.1.1).

dhltp
  • 49
  • 1
  • 7
-1

i have homebree installed so I'm going to try: $brew install gfortran hopefully this works

Megan
  • 622
  • 1
  • 10
  • 16
  • Did you brew your Python? If so, you get pip for free... It also sounds like you're on a Mac. In that case, you can get the Fortran compiler from XCode, I think. – BenDundee Feb 11 '13 at 21:41
  • Yes i have brew on my python, I've had pip for awhile. I used it to install Django, I just havent used it in awhile. Yea I just got Xcode still working on learning how to navigate and use that. I'll get it all figured out eventually, there are just so many different components to use. – Megan Feb 11 '13 at 21:48
  • Tried downloading Fortran using brew got this error: curl: (33) HTTP server doesn't seem to support byte ranges. Cannot resume. Error: Download failed: https://downloads.sf.net/project/machomebrew/Bottles/gfortran-4.7.2.lion.bottle.tar.gz – Megan Feb 11 '13 at 21:52
  • Yeah---I think you need Command Line Tools at least. I think you can install it with Command + , -> Downloads. – BenDundee Feb 11 '13 at 21:55
  • Command Line tools for Xcode? I installed that last night in Xcode->preferences->downloads->components->command line tools, would I need to take another step? – Megan Feb 11 '13 at 21:56
  • @Megan: Actually, that's just a download error from Sourceforge. At some point, either your internet connection went down or their site got overloaded, and you ended up with a partial download that it's trying to resume. You may be able to just `brew install gfortan` again. If not, you may have to `brew doctor`, or `brew install -d gfortran` and then look at where the partial file is and delete it, before you can get `brew` to start the download over again. – abarnert Feb 11 '13 at 23:01
  • @Megan: Or, you can force it to not use the bottle by using `--build-from-source`, but that will take a lot longer. – abarnert Feb 11 '13 at 23:05
  • i ran $brew doctor and this is what I got: Warning: /usr/bin occurs before /usr/local/bin This means that system-provided programs will be used instead of those provided by Homebrew. The following tools exist at both paths: 2to3 2to3-2.7 idle idle2.7 pydoc pydoc2.7 python python-config python2.7 python2.7-config pythonw pythonw2.7 smtpd.py smtpd2.7.py Consider amending your PATH so that /usr/local/bin occurs before /usr/bin in your PATH. how would i ammend it? – Megan Feb 12 '13 at 00:14
  • @Megan: This is why I suggest _not_ installing a second Python 2.7 unless you know what you're doing. Anyway, the Homebrew [FAQ](https://github.com/mxcl/homebrew/wiki/FAQ) and [Python FAQ](https://github.com/mxcl/homebrew/wiki/Homebrew-and-Python) have some information, but it's not complete. The answer is that it depends on whether you want the PATH to affect your current login, all terminal/ssh logins, or all logins (including GUI apps). I'll update my answer with more details. – abarnert Feb 12 '13 at 00:33
  • ==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/gfortran-4. ######################################################################## 100.0% ==> Pouring gfortran-4.7.2.lion.bottle.tar.gz ==> Caveats – Megan Feb 12 '13 at 00:39
  • Brews that require a Fortran compiler should not use: depends_on 'gfortran' The preferred method of declaring Fortran support is to use: def install ... ENV.fortran ... end ==> Summary /usr/local/Cellar/gfortran/4.7.2: 288 files, 59M – Megan Feb 12 '13 at 00:39
  • just ran python setup.py build in scipy directory in terminal, fortran is working!!! yayy! – Megan Feb 12 '13 at 00:40
  • So the only problem was the interrupted download, and brew not recovering automatically until you tried again? – abarnert Feb 12 '13 at 00:50
  • scipy successfully built & installed thank you both for all your help! – Megan Feb 12 '13 at 01:00
  • I guess so, I think pandora was interfering with the download because once I killed that it worked. – Megan Feb 12 '13 at 01:02
-2

Here are detailed instructions for a clean install on OSX 10.7. If I were you, I'd go this route instead of trying to download and build the sources yourself.

http://www.thisisthegreenroom.com/2011/installing-python-numpy-scipy-matplotlib-and-ipython-on-lion/

BenDundee
  • 4,389
  • 3
  • 28
  • 34
  • That page explicitly says at the top that it's out of date. It also suggests using Homebrew to install another Python 2.7 alongside Apple's, something that Homebrew specifically recommends against doing. And it requires manual hacking up of the PATH and links. Overall, I don't think this is a good answer. – abarnert Feb 11 '13 at 22:40
  • Whatevs. She already has python installed, other than that the instructions seem to be more or less the same as what you posted. – BenDundee Feb 11 '13 at 22:47
  • 1
    Unless you just want scipy to be "more or less" installed and don't need it to actually, say, work, "whatevs" is not a very helpful attitude. There are some significant differences between what I suggested and what that article suggests, and if you can't tell the difference (or if you're too lazy and just tl;dr the whole thing), you're not going to be able to help someone who runs into problems (which anyone who follows that page will). – abarnert Feb 11 '13 at 22:55
  • These are exactly the same instructions I used to install Python scipy and numpy, which I use pretty much every day without any problems---I was directing her towards a resource that I found helpful. And I don't think I'm alone on stackoverflow when it comes to thinking that having a non-Apple version of Python installed is a good idea. So, really...whatever, take it or leave it. – BenDundee Feb 11 '13 at 23:00
  • 1
    I shouldn't have responded so negatively--apologies @abarnert. – BenDundee Feb 11 '13 at 23:09
  • I do have a windows computer with python installed, if I couldn't figure it out on my mac I was going to try it on there. Its just my mac has longer battery life and isn't so loud as to disrupt the class, much better for school. So I wanted to have all the extensions installed on my Mac first if I could. – Megan Feb 12 '13 at 00:35