Sage

From YobiWiki
Jump to navigation Jump to search

Installation

Some notes on the installation of Sage in a vserver:

Create a vserver, remove tmpfs /tmp in /etc/vservers/sage/fstab as 16M is too short for compilation

Create an account for the compilation

# adduser --disabled-password --shell /bin/false sage

Install packages needed for the compilation + some for runtime

# apt-get install gcc-4.1-base make m4 bison flex tar perl binutils \
           libstdc++6-dev g++ openssh-client \
           less screen imagemagick

Download and compile sources

# cd /opt
# export http_proxy=http://proxy....:8080 
# wget http://www.sagemath.org/dist/src/sage-2.10.tar
# tar xf sage-2.10.tar
# chown -R sage:sage *
# su -s /bin/bash - sage
sage$ cd /opt/sage-2.10/
sage$ make

Time for triple coffee...

To maintain the installation and install optional packages:

sage$ export http_proxy=http://proxy....:8080 
sage$ ./sage -upgrade
sage$ ./sage -optional
sage$ ./sage -i extra_docs-
sage$ ./sage -i openssl-0.9.8d.p1
sage$ ./sage -i pyopenssl-0.6
sage$ ./sage -i gap_packages-4.4.10_3
sage$ ./sage -i database_gap-4.4.10
...

Create an account to run sage as webserver

# adduser --disabled-password --shell /bin/false saged
# cp /opt/sage-2.10/sage /home/install

Edit the launch script /home/install/sage to point to the right directory:

SAGE_ROOT="/opt/sage-2.10"
# ln -s /home/install/sage /usr/local/bin

To start & stop the webserver as a real daemon, in a screen session, save the following script as /home/install/saged:

#! /bin/sh
# Author: Philippe Teuwen

# Do NOT "set -e"

PATH=/usr/sbin:/usr/bin:/sbin:/bin
DESC="Launching SAGE webserver"
NAME=sage
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
DAEMON=/usr/local/bin/sage
HOST="fqdn.of.the.server"
PORT="8000"
ARGS="-c \"notebook(address=\\\"$HOST\\\",port=$PORT,accounts=true)\""
USER=saged
# Exit if mysql client is not installed
[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
        if [ -e $PIDFILE ]; then
            if ps -p $(cat $PIDFILE) >/dev/null; then
                echo -n -e "\nError: $NAME seems to be already running!"
                return 1
            else
                rm -f $PIDFILE
            fi
        fi
        screen -d -m -S sage su -s /bin/bash - $USER -c "$DAEMON $ARGS"
        # PID of screen
        PID=$(screen -ls sage|grep sage|sed 's/[[:space:]]*\([0-9]\+\)\..*/\1/')
        # PID of su
        PID=$(ps --ppid $PID -o pid --no-headers)
        # PID of sage
        PID=$(ps --ppid $PID -o pid --no-headers)
        echo $PID > $PIDFILE
}

#
# Function that stops the daemon/service
#
do_stop()
{
        if [ -e $PIDFILE ]; then
            PID=$(cat $PIDFILE)
            if ps -p $PID > /dev/null; then
                # kill script
                kill $PID
                rm -f $PIDFILE
                return 0
            else
                echo -e "\nWarning: $NAME was not running."
                echo -n -e "\nCleaning PID file"
                rm -f $PIDFILE
                return 1
            fi
        else
            echo -n -e "\nWarning: $NAME was not running"
            return 1
        fi
}

case "$1" in
  start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
        do_start
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
        do_stop
        case "$?" in
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
  restart|force-reload)
        #
        # If the "reload" option is implemented then remove the
        # 'force-reload' alias
        #
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
        exit 3
        ;;
esac

:

If you don want anybody to be able to create an account (which gives access to bash!!), remove ",accounts=true" from ARGS.

Install the script:

# ln -s /home/install/saged /etc/init.d
# update-rc.d saged defaults 99 01

Client tuning: jsMath fonts

If you want to install the jsMath fonts locally on your client for a faster rendering:
Download TEX fonts and install them either in ~/.fonts/ or C:\Windows\Fonts

SAGE console via ssh

Some notes to give a console access with python autocompletion:
Put ssh keys in /home/saged/.ssh/authorized_keys
Set /usr/local/bin/sage as shell of user saged in /etc/passwd
Then on the client side:

Host sage
  Hostname blabla
  User saged

SAGE console via ssh with X11 forwarding

Some notes to enable X11 forwarding with the sage console:

apt-get install xbase-clients
vi /etc/ssh/sshd_config.conf
    X11Forwarding yes
    #if you're in a vserver:
    X11UseLocalhost no 
/etc/init.d/ssh restart

Then on the client side:

Host sage
  Hostname blabla
  User saged
  ForwardX11 yes

Maxima plot via X11

Some notes to enable tcl/tk interactive plotting with maxima:
You need X11 forwarding, cf above

apt-get install tk8.4

Test:

ssh sage
Last login: blabla
----------------------------------------------------------------------
| SAGE Version 2.10, Release Date: 2008-01-18                        |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------

sage: maxima.plot2d('cos(2*x) + 2*cos(x)','[x,0,1]','[plot_format,openmath]')

or

sage: maxima.eval('load("plotdf")')
sage: maxima.eval('plotdf(x+y,[trajectory_at,2,-0.1);')

octave & gnuplot via X11

Some notes to enable octave and gnuplot:
You need X11 forwarding, cf above

apt-get install octave gnuplot
sage -i gnuplotpy-1.7.p3

Test:

ssh sage
Last login: blabla
----------------------------------------------------------------------
| SAGE Version 2.10, Release Date: 2008-01-18                        |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------

sage: octave.de_system_plot(['x+y', 'x-y'], [1, -1], [0,2])

singular & surf via X11

Some notes to enable surf for singular:
You need X11 forwarding, cf above

We need to compile surf from the sources:

apt-get install zlib1g-dev libjpeg62-dev libtiff4-dev flex \
   libgmp3-dev libgtk1.2-dev libx11-dev libxmu-headers libxmu-dev
cd /opt
wget "http://downloads.sourceforge.net/surf/surf-1.0.5.tar.gz"
tar xzf surf-1.0.5.tar.gz 
cd surf-1.0.5/
./configure 
make
ln -s  /opt/surf-1.0.5/surf /usr/local/bin

Test:

ssh sage
Last login: blabla
----------------------------------------------------------------------
| SAGE Version 2.10, Release Date: 2008-01-18                        |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: s = singular.eval
sage: s('LIB "surf.lib";')
'// ** loaded /opt/sage-2.10/local/LIB/surf.lib (1.28,2007/07/13)'
sage: s("ring rr0 = 0, (x1,x2),dp;")

sage: s("ideal I = x1^3 - x2^2;")

sage: s("plot(I);")
"Press q to exit from 'surf'"

Maple

Some notes if you want to use your Maple 32-bit on a 64-bit arch:

apt-get install ia32-libs
/bin/maple -binary IBM_INTEL_LINUX

Or from Sage:

maple.fsolve('x^2=cos(x)+4', 'x=0..5')