Setting Up Gurobi Optimizer on UCloud¶
The Gurobi Optimizer is a powerful solver for mathematical optimization problems, including linear, integer, and quadratic programming. It is widely used in industry and academia for its speed, scalability, and robust performance. It has user-friendly APIs in Python, C++, Java, and more.
It is suited to CPU usage as there are is no significant performance enhancement when using GPUs. The software is licensed and users must first register on the Gurobi Optimization website. Personal licenses are free for academics.
This guide shows how to set up and use the Gurobi Optimizer solver on UCloud. There are example use cases of the Gurobi Optimizer for Python and MATLAB, but the installation instructions are suitable for most use cases.
Using Gurobi on UCloud¶
Summary of steps required¶
Create a personal, academic Gurobi WLS license
Add the
gurobi.lic
file to UCloudStart a job on UCloud that needs to have the Gurobi optimizer accessible (e.g., MATLAB, JupyterLab, or Coder)
Set up the Gurobi integration for the specific use case (i.e., Python package, MATLAB setup, etc.)
Step 1: Create a Gurobi license¶
Go to the Gurobi portal and either log in or register a new account.
Navigate to Licenses in the left-hand navigation bar, and click Request to create a WLS Academic license.
Note
Personal named licenses will not work on UCloud, as they do not work within containerized environments.
To generate a WLS licenses the user's machine must be connected to a university network. Being connected via a VPN is usually insufficient.
Step 2: Upload the license file to UCloud¶
To download the license file, gurobi.lic
, log into the Gurobi portal and navigate to Licenses in the left-hand navigation bar.
From there, click the WLS license and then click the Open the license in the Web License Manager icon.Once inside the Web License Manager, click Download to download gurobi.lic
.
Finally, upload gurobi.lic
to an appropriate location in a UCloud drive.
Step 3: Start the UCloud job¶
When specifying the job's parameters from the job submission page be sure to:
Add the folder that contains the
gurobi.lic
fileCreate a
.sh
script (see below) and use it for the Initialization optional parameter
Automatic installation with an initialization script¶
Below is a minimal working example of an initialization script that will automatically set up Gurobi immediately after the job starts.
Note
The script installs Gurobi 12.0.0 which can be modified by changing to the desired version in the strings below.
The script below assumes that a folder containing gurobi.lic
was added to the job. The user should replace <added-folder>
with the actual folder name.
#!/bin/bash
# Fetch Gurobi installation package
wget -v https://packages.gurobi.com/12.0/gurobi12.0.0_linux64.tar.gz
# Move the file to the default location '/opt'
sudo mv gurobi12.0.0_linux64.tar.gz /opt
cd /opt
# Extract the installation file and delete the tarball
sudo tar -xvf gurobi12.0.0_linux64.tar.gz gurobi1200/
sudo rm -rf gurobi12.0.0_linux64.tar.gz
# Return to the work directory
cd /work
# Copy the license file from the location you have stored it on UCloud
sudo cp /work/<added-folder>/gurobi.lic /opt/gurobi1200/
# Change permissions on license file
sudo chmod 777 /opt/gurobi1200/gurobi.lic
# Set and export the Gurobi environment variables
export GUROBI_HOME=/opt/gurobi1200/linux64
export PATH=$GUROBI_HOME/bin:$PATH
export LD_LIBRARY_PATH=$GUROBI_HOME/lib:"$LD_LIBRARY_PATH"
export GRB_LICENSE_FILE='/opt/gurobi1200/gurobi.lic'
# ... and add them to .bashrc
echo 'export GUROBI_HOME=/opt/gurobi1200/linux64' >> ~/.bashrc
echo 'export PATH=$GUROBI_HOME/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=$GUROBI_HOME/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
echo 'export GRB_LICENSE_FILE="/opt/gurobi1200/gurobi.lic"' >> ~/.bashrc
source ~/.bashrc
# Check if the license is activated in the terminal
gurobi_cl && echo "Successfully tested the license file!" || echo "Failed to identify license file!"
Step 4: Using Gurobi Optimizer¶
The Gurobi Optimizer can be integrated with most popular programming languages, such as Python, R, Julia, C++ etc. How to integrate with Gurobi depends on the programming language.
Example 1: Python¶
To use Gurobi Optimizer with Python the gurobipy
package is required. This package can be installed from a terminal inside the job with:
$ pip install gurobipy
When writing a Python script that uses Gurobi, the following lines must be present:
import gurobipy
# Set access credentials for API.
# All three values can be found in the license file 'gurobi.lic'
options = {
"WLSACCESSID": "<wls-access-id>",
"WLSSECRET": "<wls-secret>",
"LICENSEID": <license-id>,
}
Be sure to replace <wls-access-id>
, <wls-secret>
, and <license-id>
with the corresponding values from the license.lic
file.
Example 2: MATLAB¶
If the installation has been completed successfully (see above), the user can setup Gurobi by running the following commands from the MATLAB console:
cd /opt/gurobi1200/linux64/matlab/
run gurobi_setup.m
The user can choose to save the Gurobi path for frequent use of the optimizer. This can be done with the following commands:
addpath('/opt/gurobi1200/linux64/matlab') % Adjust path if needed
savepath
The setup should now be completed. The user can run the following MATLAB script to verify that the optimizer is running:
model.A = sparse([1, 2; 3, 4]);
model.obj = [1; 1];
model.rhs = [1; 1];
model.sense = '<>';
params.outputflag = 1;
result = gurobi(model, params);
disp(result)
Contents