Home >Backend Development >Python Tutorial >SageMath Installation

SageMath Installation

PHPz
PHPzOriginal
2024-08-16 18:01:291043browse

SageMath Installation

SageMath is a free open-source mathematics software system that builds on many existing open-source packages, including NumPy, SciPy, matplotlib, Sympy, Maxima, GAP, FLINT, R, and more. Unlike proprietary software like Magma, Maple, Mathematica, and MATLAB, Sage is free to use and allows you to view and modify the source code. If you're familiar with Python, you'll feel right at home with Sage as it extends Python with a robust set of mathematical tools.

The easiest way to get started with Sage is by running code in a browser-based workbook on a platform called CoCalc. CoCalc's free tier offers several benefits, including collaboration, automatic dependency management, and the convenience of executing Sage code without installing the software locally. However, after using CoCalc for a while, I found myself wanting a faster setup, free from the limitations of a cloud environment. This led me to install Sage locally on my machine.

If you're running Windows, installing Sage requires a few extra steps.

  1. Download Windows Subsystem for Linux. Make sure you are using WSL2.
  2. Reading Set up a WSL development environment is also helpful.

You can find instructions for local installation in the Sage Installation Guide.

There are several ways to install Sage, each with its advantages. Installing Sage from source offers the most flexibility, but it's a lengthy process that may present some challenges. Installing the necessary dependencies can take a considerable amount of time, and the make command — used to build Sage — will also require some patience, depending on your computer's resources.

For those seeking a simpler and quicker installation process, using conda-forge is an excellent alternative. Conda-forge is a community-driven collection of packages for conda, making it easy to install and manage Sage without dealing with the complexities of a source build. Here's how to get started:

curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
bash Miniforge3-$(uname)-$(uname -m).sh
conda create -n sage sage python=3.11

After installing Sage via conda, you'll see a message similar to the following. Pay attention to the instructions provided, as they guide you on how to activate and manage your new Sage environment:

Transaction finished

To activate this environment, use:

    micromamba activate /home/samuel-lubliner/miniforge3

Or to execute a single command in this environment, use:

    micromamba run -p /home/samuel-lubliner/miniforge3 mycommand

installation finished.
Do you wish to update your shell profile to automatically initialize conda?
This will activate conda on startup and change the command prompt when activated.
If you'd prefer that conda's base environment not be activated on startup,
   run the following command when conda is activated:

conda config --set auto_activate_base false

You can undo this by running `conda init --reverse $SHELL`? [yes|no]
[no] >>> no

This prompt asks whether you want conda to automatically initialize every time your shell starts. I prefer to keep my environment clean, so I chose no. This choice keeps the base environment inactive on startup, which helps prevent cluttering the command prompt with unnecessary environment activations. If you prefer this setup, type no when prompted.

You have chosen to not have conda modify your shell scripts at all.
To activate conda's base environment in your current shell session:

eval "$(/home/samuel-lubliner/miniforge3/bin/conda shell.YOUR_SHELL_NAME hook)"

To install conda's shell functions for easier access, first activate, then:

conda init

Thank you for installing Miniforge3!

Manually activating your environment ensures that you maintain control over when and how your conda environments are loaded. This approach is beneficial if you use multiple environments.

Since I opted not to have conda modify my shell scripts, I run Sage manually using the following commands. This method ensures a clean environment and gives me control over when to activate Sage:

samuel_lubliner@DESKTOP-QGSGOAI:~$ eval "$(/home/samuel_lubliner/miniforge3/bin/conda shell.bash hook)"
(base) samuel_lubliner@DESKTOP-QGSGOAI:~$ conda activate sage
(sage) samuel_lubliner@DESKTOP-QGSGOAI:~$ sage -n jupyter
  • The first command initializes conda in your current shell.
  • The second command activates the sage environment.
  • The third command starts Sage with the Jupyter notebook interface.

Notice that (base) indicates the conda base environment is activated, and (sage) shows that the sage environment is active. Running these commands each time can become repetitive, so let's automate this process by creating a bash script. We can create the file sage_nb.sh. If you are using WSL, the docs recommend:

#!/bin/bash
# Switch to desired windows directory
cd /mnt/c/path/to/desired/starting/directory
# Start the Jupyter notebook
SAGE_ROOT/sage --notebook
# Alternatively you can run JupyterLab - delete the line above, and uncomment the line below
#SAGE_ROOT/sage --notebook jupyterlab

In my case, I installed JupyterLab because I prefer its more modern interface. Here is my script:

#!/bin/bash
# Start JupyterLab
/home/samuel_lubliner/miniforge3/envs/sage/bin/sage --notebook jupyterlab

To run the script, you first need to make it executable:

chmod ug+x ~/sage_nb.sh

Now you can run the script whenever you want to start Sage:

cd ~
./sage_nb.sh

If everything is set up correctly, running the script will start the Jupyter server in your terminal. From here, you can begin working with Sage in your browser.

If you prefer a more user-friendly approach, you can take this bash script a step further by creating a clickable shortcut on your desktop. This allows you to start Sage and JupyterLab with a double-click. You can find detailed instructions on how to create this shortcut in the SageMath docs.

The above is the detailed content of SageMath Installation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to scrapeNext article:How to scrape