Home > build, documentation, LaTeX, Linux, Mac OS X, Sage > Compiling Sage on Linux and Mac OS X

## 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
```

```\$ 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 $\LaTeX$. However, for best results, it is recommended that you have $\LaTeX$ installed on your system. Furthermore, $\LaTeX$ is a prerequisite for building the PDF version of the Sage standard documentation. The following command installs the full $\LaTeX$ 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
```

```\$ ./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
```

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

or view it online at

http://www.sagemath.org/doc

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
```

```\$ 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 $\LaTeX$. However, for best results, it is recommended that you have $\LaTeX$ installed on your system. Furthermore, $\LaTeX$ 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
```

```\$ ./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
```

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

or view it online at

http://www.sagemath.org/doc

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
```
1. 14 November 2010 at 12:46 pm

When compiling Sage on an intel mac, I think you generally have to reset the path,
don’t you? IIRC, the default path in the terminal points to directories that the compiler doesn’t like. The command

export PATH=:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin

is an example of such a reset that works.

2. 14 November 2010 at 10:55 pm

Thanks, that’s useful. I didn’t realize that you could build in parallel. On OS X 10.6.4 I built Sage 4.6 from source recently and I didn’t have to change my PATH. My default in Terminal is “/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:/usr/X11/bin”, I’m sure I didn’t change this myself.

3. 15 November 2010 at 5:24 pm

Clarification: I have built Sage-x.y.z from source on dozens of OS X boxes and hundreds of x.y.z versions, and don’t have to change my PATH as you suggest above. The reason is that I didn’t install anything from FINK or DarwinPorts. If you do use fink or DawrinPorts, then you do have to change your PATH, since Fink contains conflicting versions of libraries and other tools.

4. 21 November 2010 at 4:09 am

Also just FYI, most Mac users will not need to follow all the steps above, if they have XCode, or at least they are easier to do than you describe. You should (while in Finder, the default) be able to double-click the tarball to unpack it, and do Command+Shift+U for the Utilities folder to find Terminal, which can be double-clicked. In particular, this works on OS X 10.4.