Please note that due to a recent update to Google Colab, the WLS licensing process described below may not currently work. We are working on a fix for this. In the meantime, you can still use Colab by following the instructions in the "Using a Local Runtime" section below.
Google Colab (https://research.google.com/colaboratory/) provides an easy way to use Gurobi with Python + Jupyter notebooks, with no local software installation required. The Gurobi solver can be used in Colab notebooks either with the free size-limited trial license that is installed by default or with a Web License Service (WLS) license which enables the full features of Gurobi. Below, we will walk you through the following:
- Size-Limited Trial License
- Full Gurobi License using WLS
- Usage Limit Issues
- Using a Local Runtime
- Gurobi + Colab for Teaching
!pip install gurobipy # install gurobipy, if not already installed
import gurobipy as gp # import the installed package
Size-Limited Trial License
model_size_limited = gp.Model()
Restricted license - for non-production use only - expires 2022-01-13
Full Gurobi License using WLS
- Find or create your WLS license.
- As an academic, follow the Web License Service instructions on the Gurobi for Academics and Researchers page.
- As a commercial user, your WLS will be visible at license.gurobi.com for the owner of the license if you have the WLS license available.
- Go to https://license.gurobi.com/manager/keys and create an API key for your WLS license. Give it an informative name to help you manage keys. You will need to download the API key at the time you create it, as this is the only time you can access it directly. The download button downloads a license file gurobi.lic with contents like the below.
- Enter your API access ID, secret key, and license ID into the environment in your notebook code.
Downloaded gurobi.lic license file contents:
# Gurobi WLS license file
# Your credentials are private and should not be shared or copied to public repositories.
# Visit https://license.gurobi.com/manager/doc/overview for more information.
# Create environment with WLS license
e = gp.Env(empty=True)
e.setParam('WLSACCESSID', 'your wls accessid (string)')
e.setParam('WLSSECRET', 'your wls secret (string)')
e.setParam('LICENSEID', <your license id (integer)>)
# Create the model within the Gurobi environment
model = gp.Model(env=e)
Usage Limit Issues
Once a gurobipy environment is created in Colab, it will not release the WLS tokens until the environment and all associated models are disposed of or the container shuts down. So, be aware that if you navigate away from the Colab notebook page without disposing of the environment (using model.dispose() and env.dispose()) or shutting down the runtime (colab menu -> runtime -> restart) then the WLS token will remain active. You may then run into usage limit issues, especially if you create many notebooks each using Gurobi. Hitting your usage limit would result in the following (or similar) error when creating a Gurobi environment:
Error 10030: Too many containers, 7 active containers for a baseline of 3
If you run into usage limits unexpectedly, you can check how many Colab notebooks you have running by opening a new notebook and selecting Runtime -> Manage Sessions from the menu bar. This will show all running notebooks in your Google user account. Terminating running notebooks will ensure that any WLS license tokens they are using are released. Note that it may take up to 5 minutes for the usage limit to reset and enable you to use Gurobi again.
Using a Local Runtime
Google Colab allows you to run notebook code locally, instead of via Google Cloud infrastructure, provided you have the right software installed. This alternative approach will then use your local machine's Gurobi license (for example, a named-user license or floating license) instead of WLS. To use Colab + Gurobi via this method:
- follow the Colab local runtime instructions to install and start a local runtime, then
- once connected to a local runtime, follow the gurobipy installation steps above.
Note that you will need a Gurobi license for your local machine for this to work. If you eligible for a free academic license, please see the instructions for creating a named-user academic license.