VHB

Installing gnuplot on macOS

A quick—and popular, if Google is to be believed—guide to installing gnuplot on macOS

Hardly anyone has gone through a college mathematics or physics course without meeting the wonderful gnuplot. However, it turns out that installing gnuplot (or Octave, for that matter—but let us leave that for another day) on a Mac is a pain in the neck. At a time when installing games take a two clicks, it simply is not straightforward to install gnuplot.

After scratching my head over it for two days straight, I finally installed gcc, gnuplot, Octave and LaTeX on my new Mac (OS X 10.10.3, Yosemite) and decided to note some points/instructions down here for anyone else looking for a simple solution from start to finish contained in one place.


Update, 6 December 2019 Since this article was written about five years ago Homebrew has changed its gnuplot installation system and does not allow picking terminals. This makes this a little inconvenient, not to mention useless. Thankfully MacPorts still supports a robust full gnuplot installation which makes me change my recommendation from ‘always install Homebrew’ to ‘always install both Homebrew and MacPorts’. Install MacPorts by picking a suitable package on their installation page. Then proceed as follows:

  1. Check what terminals are available (optional) with sudo port variant gnuplot
  2. Install gnuplot with required terminals e.g. to install gnuplot with QT5, x11, AquaTerm and WXT—
sudo port install gnuplot +qt5 + x11 +aquaterm +wxwidgets

Click here to skip older updates and go to the main (legacy) article which runs you through XCode, GCC etc. Specifically, the above two steps replace steps 7 and 8 in the main article below.


Update, 9 August 2017 Since this article was written about two years ago a new version of gnuplot has been released, as have two new versions of macOS. A couple of my views have also changed as a result of this: I think AquaTerm is good enough for gnuplot and the decision to use X11 should only be a matter of specific needs or taste.

This article has turned out to be more popular than I hoped, with several universities and academic institutions sending lots of traffic towards it, all the way from these interesting lecture notes from Stony Brook University, NY, to this Chinese (I think) forum. In view of public demand, below is a small update.

If all you are interested in doing is to install gnuplot save yourself the trouble of scrolling down and follow these instructions (tested on macOS High Sierra) to install both AquaTerm and X11, omitting commands as necessary if you only wish to use one and not the other.

  1. Install homebrew via the Terminal
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  1. Install xquartz if you want X11
brew cask install xquartz
  1. Install aqua if you want AquaTerm
brew install Caskroom/cask/aquaterm
  1. Install gnuplot (remove the --with-name for terminals you do not need
brew install gnuplot --with-aquaterm --with-qt4 --with-x11

That is all. Enjoy plotting with gnuplot.


1. Install Xcode

If you have been trying to dodge your way out of Apple’s enormous 2.6GB Xcode app, get ready to face the demon: almost nothing related to coding will work on your Mac without Xcode and its libraries, so head to the App store and download Xcode.

2. Download command line tools

Download command line tools for Xcode. You can do this via the Terminal.app (Applications → Utilities → Terminal) with the following command (which you may copy and paste):

xcode-select --install

This gives you a message saying command line tools have not been installed and will offer to install them for you now; proceed and install them. It should take some time, though not as much as downloading Xcode.

3. Accept Xcode licences

You need to have accepted the Xcode licence agreements to use many related programmes, so open Terminal and type in the following (you can copy and paste it as usual):

sudo xcodebuild

This gives you a message requiring you to press return/enter to view the license. Hit enter, read through it and type in agree at the end when prompted. Type in cancel to disagree, meaning you should probably have been reading some other article now, not this. In any case, once you read and agree, proceed to the next step.

4. GCC

GCC should work straight after installing Xcode. Open Terminal and do the following:

vi Hello.c

In the page that opens, copy and paste these lines:

#include<stdio.h>
int main() {
printf("\n\n\t\t Hello, world.\n\n");
}

Then press Esc and type :wq and you will return to the first Terminal screen. There, type the following two lines (press Return after each line):

gcc Hello.c -o Hello
./Hello

You should get an output on screen saying ‘Hello, world.’ if all is well. And that is all for GCC, which is actually simple, unlike gnuplot and Octave.

5. LaTeX

MacTeX is LaTeX for Mac. For basic—and mostly all general purposes—I did not see the need to install the entire MacTeX package, which is 2.5GB in size. As an alternative, I opted for the smaller, yet feature-rich, BasicTeX package which I usually install on all my systems.

You can go with the full package or opt for the smaller one as per your needs. Both files are .pkg and involve the usual GUI installation so this step should be no problem for anyone.

6. Install XQuartz

XQuartz, or colloquially the X11.app, is a Mac version of Windows’ X Server. It is necessary for Homebrew, which we will install next, as well as to use the (somewhat) standard xterm display in gnuplot. Download XQuartz, which comes as a disk image which you can easily mount and install via a package.

The catch here is that the installed X11.app (check Applications → Utilities → X11 after installing) must be started before the Terminal is fired up to run gnuplot. That is the only way to make gnuplot start with the x11 terminal by default. If you find gnuplot aborting, you have probably not started x11.

7. Get Homebrew

Since Macs do not come with a package manager built-in like Linux, you will have to opt for alternatives often developed by the community. While MacPorts has been a popular one, I have found it buggy, especially so with xterm (x11) display for gnuplot, which is both the standard and more convenient than Aqua, the default for Mac—do not ask me why.

Installing Homebrew, an increasingly more preferred MacPorts alternative for good reasons, is simple. Open Terminal and use the command given below:

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

This should install Homebrew without a problem. Homebrew commands begin with brew and are fairly easy to follow.

8. Install gnuplot with brew

Installing gnuplot once you have come this far, thankfully, is fairly simple:

brew install gnuplot --with-x11

Remember to specify --with-x11 instead of the older (now deprecated) --x11 or brew install gnuplot without x11.

9. gnuplot

Now it is time to check gnuplot. Fire up gnuplot:

gnuplot

Then type in a simple plotting command and hit return/enter.

plot sin(x)

This should immediately display a generic sine graph (not saved permanently anywhere, of course) on your screen.

How to save and use plots is beyond the scope of this article, but for now, if you find the output graph giving out an error, make sure you have the x11.app running before you open Terminal.


Hat tip to Michael Budd After writing this article, I came across Michael Budd’s on installing gnuplot straight from the source files. I tried in vain to get x11 to work well after installing in this method, but perhaps I was not good enough in trying. That said, if you are not particular about x11, this is a really quick method to get gnuplot to work.

Note that when trying the third make install command, it failed on my system several times, so I had to work around that using sudo make install and entering my administrator password. If you do not have, administrator rights Mr Budd’s method may not work.


10. Setting a default display environment

Some machines may still have problems with gnuplot, in which case you could try editing your bash profile. Please try this at your own risk, and if you have no idea what this does, I strongly insist that you do not do it. In any case, I cannot be held responsible for whatever happens to your system.

touch ~/.bash_profile; open ~/.bash_profile

Enter the above command in your Terminal to open your .bash_profile for editing in TextEdit (or whatever else is your default editor). Next, in the open file, type the following and hit S to save it, then close it.

if [ -z ${DISPLAY} ]
then
  export DISPLAY=:0.0
fi

Restart Terminal and it should work fine now. This has something to do with gnuplot sending data to the wrong display. You can find more details and a fair bit of discussion on this around the web.

That should be all. You should have a working copy of gnuplot on your machine now. Have fun with it: it supports eps export as well as links to c programmes via the pfile command. So far, in my use, I have not come across any problems after installing it this way, but if you do, let me know and I will try to help you as best as I can.