## Compiling Sage on Linux and Mac OS X

Compiling Sage can be a difficult process. The difficulty is compounded by the many versions of the GNU/Linux and Mac OS X operating systems. In this post, I don’t want to cover every possible ways of compiling Sage on each operating system. My goal here is to provide a general overview of how to compile Sage on Ubuntu 10.10 and Mac OS X 10.6.x.

**Ubuntu 10.10**

For Linux, it should be sufficient to explain how to compile Sage under Ubuntu. The following commands were tested under Ubuntu 10.10. First, make sure you have the necessary tools for compiling Sage. To install these tools, execute the following command from a terminal window:

$ sudo apt-get install build-essential gfortran m4 readline-common libreadline-dev

Suppose you have downloaded the Sage source distribution and it is saved as the file named sage-x.y.z.tar. The file extension “.tar” signifies that sage-x.y.z.tar is a tar archive, also called a tar file or a tarball. To compile Sage, you need to launch a terminal window, extract the Sage source files from the tarball, and use development tools that come with Ubuntu to compile Sage. Suppose the Sage source tarball is located at

/home/username/sage-x.y.z.tar

where “username” is your username, the one you use to login to your Ubuntu system; you should replace “username” your actual username. Say you want to compile Sage under the directory /home/username/. From your terminal window, navigate to your home directory and extract the source tarball:

$ cd $ tar -xf sage-x.y.z.tar

which will produce a directory called

/home/username/sage-x.y.z/

that contains all the packages that are distributed with the Sage source distribution. You are now ready to compile Sage as contained under the latter directory. Note that you can rename the latter directory to, say, /home/username/mysage/ as follows:

$ mv sage-x.y.z mysage

If you have a directory, say, /home/username/bin/ and you want to extract the Sage source tarball to that directory, you can do so as follows:

$ tar -xf sage-x.y.z.tar -C /home/username/bin

Here, it is assumed that you want to compile the Sage source as contained under

/home/username/sage-x.y.z/

This top-level directory is usually referred to as SAGE_ROOT and is where the compiled version of Sage resides. The next step is to navigate to the latter directory and start the automated compilation process:

$ cd /home/username/sage-x.y.z/ $ make

Usually, that is all you need to do. The rest of the compilation process is non-interactive and takes at most a few hours, depending on your system.

Note that compiling Sage is very taxing on your system’s resources, i.e. CPU, RAM, disk space, and so on. Make sure you have sufficient system resources; for example, you could close all running applications and ensure you have at least 2 GB of free disk space for Sage. Furthermore, the compilation process can take from a dozen or so minutes to a few hours, depending on your system’s hardware and how you customized the compilation process. By default, the compilation process uses one thread to compile Sage, which means that every component is compiled one after the other, i.e. serial compilation. Serial compilation of Sage is known to take up to an hour or more to complete.

On a multi-core or multi-CPU system, the compilation process can be parallelized. This means that many components of Sage can be compiled in parallel, hence reducing the time it takes for the compilation process to complete. The general steps to compile Sage in parallel are as follows. Determine the number of cores on your system and decide on how many threads you want to devote to a parallel compilation of Sage. On Ubuntu, detailed information about your system’s CPU is contained in the file

/proc/cpuinfo

Read through that file to determine the number of cores on your system. Say your system has two cores and you want to use two threads for compiling Sage in parallel. Then the necessary setup to compile Sage with two threads is:

$ SAGE_PARALLEL_SPKG_BUILD="yes"; export SAGE_PARALLEL_SPKG_BUILD $ MAKE="make -j2"; export MAKE $ make

This should initiate the process of compiling Sage in parallel. If your system allows for compiling Sage with more threads, e.g. 4 or 8 threads, then change “-j2” to “-j4” or “-j8” accordingly.

Once all components of Sage are successfully compiled, the compilation process will automatically build the HTML version of the Sage standard documentation. This documentation includes a tutorial, an FAQ, a collection of thematic tutorials, a reference manual, an installation guide, among other documents. Building the HTML version of the Sage standard documentation does not require . However, for best results, it is recommended that you have installed on your system. Furthermore, is a prerequisite for building the PDF version of the Sage standard documentation. The following command installs the full distribution that comes with Ubuntu:

$ sudo apt-get install texlive-full

This will also install necessary tools for building both the HTML and PDF versions of the Sage documentation. To manually build the HTML version of the documentation, do the following from within the SAGE_ROOT directory:

$ ./sage -docbuild all html # or $ ./sage -docbuild --no-pdf-links all html

To build the PDF version of the documentation, do

$ ./sage -docbuild all pdf

For further options to customize your build of the Sage standard documentation, see the output of the following command:

$ ./sage -docbuild

You can also download the documentation from

http://www.sagemath.org/help.html

or view it online at

To customize the compilation process to your particular needs, refer to the file

SAGE_ROOT/Makefile

Once the compilation process completes, there is no separate installation process for your newly compiled Sage. You can think of Sage as being compiled and installed under SAGE_ROOT. To begin using your freshly compiled version of Sage, navigate to SAGE_ROOT and launch Sage as follows:

$ cd /home/username/sage-x.y.z/ $ ./sage

**Mac OS X 10.6.x**

For Mac OS X, most of the prerequisites for compiling Sage are bundled with XCode, which is freely available from Apple’s website. Install XCode as directed by Apple’s installation instructions and download the latest Sage stable source distribution from the Sage website. If you have XCode installed on your system, then the following commands should not result in any errors:

$ which gcc /usr/bin/gcc $ which g++ /usr/bin/g++ $ which make /usr/bin/make $ which m4 /usr/bin/m4 $ which perl /usr/bin/perl $ which tar /usr/bin/tar $ which ranlib /usr/bin/ranlib

Suppose you have downloaded the Sage source distribution and it is saved as the file named sage-x.y.z.tar. The file extension “.tar” signifies that sage-x.y.z.tar is a tar archive, also called a tar file or a tarball. To compile Sage, you need to launch a terminal window, extract the Sage source files from the tarball, and use tools that come with XCode to compile Sage. A terminal window or a console is usually launched when you run the program Terminal, which is found at

/Applications/Utilities/Terminal.app

under Mac OS X 10.4.x. For Mac OS X 10.5.x and 10.6.x, use the application finder to locate Terminal and then launch Terminal. Suppose the Sage source tarball is located at

/Users/username/sage-x.y.z.tar

where “username” is your username, the one you use to login to your Mac OS X system; replace “username” with your actual username. Say you want to compile Sage under the directory /Users/username/. From your terminal window, navigate to your home directory and extract the source tarball:

$ cd $ tar -xf sage-x.y.z.tar

which will produce a directory called

/Users/username/sage-x.y.z/

that contains all the packages that are distributed with the Sage source distribution. You are now ready to compile Sage as contained under the latter directory. Note that you can rename the latter directory to, say, /Users/username/mysage/ as follows:

$ mv sage-x.y.z mysage

If you have a directory, say, /Users/username/bin/ and you want to extract the Sage source tarball to that directory, you can do so as follows:

$ tar -xf sage-x.y.z.tar -C /Users/username/bin

Here, it is assumed that you want to compile the Sage source as contained under

/Users/username/sage-x.y.z/

This top-level directory is usually referred to as SAGE_ROOT and is where the compiled version of Sage resides. The next step is to navigate to the latter directory and start the automated compilation process:

$ cd /Users/username/sage-x.y.z/ $ make

Usually, that is all you need to do. The rest of the compilation process is non-interactive and takes at most a few hours, depending on your system.

Note that compiling Sage is very taxing on your system’s resources, i.e. CPU, RAM, disk space, and so on. Make sure you have sufficient system resources; for example, you could close all running applications and ensure you have at least 2 GB of free disk space for Sage. Furthermore, the compilation process can take from a dozen or so minutes to a few hours, depending on your system’s hardware and how you customized the compilation process. By default, the compilation process uses one thread to compile Sage, which means that every component is compiled one after the other, i.e. serial compilation. Serial compilation of Sage is known to take up to an hour or more to complete.

On a multi-core or multi-CPU system, the compilation process can be parallelized. This means that many components of Sage can be compiled in parallel, hence reducing the time it takes for the compilation process to complete. The general steps to compile Sage in parallel are as follows. Determine the number of cores on your system and decide on how many threads you want to devote to a parallel compilation of Sage. On Mac OS X, detailed information about your system’s hardware is obtained via the command

$ system_profiler

Say your system has two cores and you want to use two threads for compiling Sage in parallel. Then the necessary setup to compile Sage with two threads is:

$ SAGE_PARALLEL_SPKG_BUILD="yes"; export SAGE_PARALLEL_SPKG_BUILD $ MAKE="make -j2"; export MAKE $ make

This should initiate the process of compiling Sage in parallel. If your system allows for compiling Sage with more threads, e.g. 4 or 8 threads, then change “-j2” to “-j4” or “-j8” accordingly.

Once all components of Sage are successfully compiled, the compilation process will automatically build the HTML version of the Sage standard documentation. This documentation includes a tutorial, an FAQ, a collection of thematic tutorials, a reference manual, an installation guide, among other documents. Building the HTML version of the Sage standard documentation does not require . However, for best results, it is recommended that you have installed on your system. Furthermore, is a prerequisite for building the PDF version of the Sage standard documentation. To manually build the HTML version of the documentation, do the following from within the SAGE_ROOT directory:

$ ./sage -docbuild all html # or $ ./sage -docbuild --no-pdf-links all html

To build the PDF version of the documentation, do

$ ./sage -docbuild all pdf

For further options to customize your build of the Sage standard documentation, see the output of the following command:

$ ./sage -docbuild

You can also download the documentation from

http://www.sagemath.org/help.html

or view it online at

To customize the compilation process to your particular needs, refer to the file

SAGE_ROOT/Makefile

Once the compilation process completes, there is no separate installation process for your newly compiled Sage. You can think of Sage as being compiled and installed under SAGE_ROOT. To begin using your freshly compiled version of Sage, navigate to SAGE_ROOT and launch Sage as follows:

$ cd /Users/username/sage-x.y.z/ $ ./sage

## Compile Sage 4.1 in 64-bit mode on OS X 10.5.8

Compiling Sage 4.1 on Mac OS X 10.5.8 is fairly easy, i.e. if you want to build Sage in 32-bit mode. You download a source version of Sage, uncompress the tarball, navigate to the top level directory of the source version, and issue make. But if you want to build in 64-bit mode, you need to replace the version of Fortran that is shipped with Sage with gfortran. For example, say you want to compile Sage 4.1 in 64-bit mode. Here is what you should do:

- Download the source tarball of Sage 4.1 and a custom-built Fortran spkg. The Sage source tarball can be found at the Sage download page. The custom-built Fortran spkg can be found at my development home directory on sage.math. It was built by Michael Abshoff (thank you very much, Michael).
- Uncompress the source tarball, delete the Fortran spkg that’s shipped with Sage, and move the custom-built Fortran package to the standard packages repository:
- Now compile in 64-bit mode:

$ tar -xf sage-4.1.tar $ rm sage-4.1/spkg/standard/fortran-20071120.p5.spkg $ mv fortran-OSX64-20090120.spkg sage-4.1/spkg/standard/

$ export SAGE64=yes $ cd sage-4.1/ $ make

Wait a while for Sage to build. You can use these steps to build Sage 4.1.1 in 64-bit mode under OS X 10.5.8. However, Sage 4.1.1 is known to fail to compile properly as documented in the release tour. This is due to a 32- versus 64-bit issue in cliquer. One way to get around this is to wait for make to finish running. Open the file sage/graphs/all.py and comment out the line that imports cliquer functionalities, like so:

#from sage.graphs.cliquer import *

From the top level Sage directory, issue make again. This would resume the build process where it left off when failing to compile cliquer.

## Clickable Mac OS X app for Sage 3.4

It has been a while since I built a clickable Mac OS X app for Sage. But with the release of Sage 3.4, I thought I revisit the joy of doing so. I’m using OS X 10.4.8 and, like some people, I’ve been too lazy to update my Mac. Many of the steps for building an app for Sage 3.4 haven’t changed since Sage 3.3. The only major thing is that I now have to type in more stuff to build a .dmg for Sage 3.4. However, after getting over my fear of typing, I managed to enter a few extra characters and followed the building instructions as per Sage 3.3. Yes, I’m on a mission to type as little as possible.

With the source release of Sage 3.4, building a clickable Mac OS X app is still as easy as 1-2-3:

- Download and extract sage-3.4.tar to a directory that you have write access:
tar -xvf sage-3.4.tar -C /path/to/preferred/dir

- Navigate to the top level directory of the uncompressed source distribution, and compile Sage 3.4 from there:
cd SAGE_ROOT/ make # be patient here. Go get a drink or watch TV. make test # this is optional. If you do this, be # very patient here. Go practice zen or yoga ðŸ™‚

- Now create a binary OS X clickable app:
SAGE_APP_BUNDLE=yes export SAGE_APP_BUNDLE ./sage -bdist 3.4 # this can take a while

But hang on. Why did I have to type in the two lines containing the SAGE_APP_BUNDLE variable? For the gory details, see this sage-support thread. In case the curiosity cat has bitten you, here’s my terminal session while building the clickable app:

After the system has completed building the binary version, you can find the binaries in SAGE_ROOT/dist. But what does it look like when it’s running? Well, here it is in action:

Have a nice Sage… I mean day.