The following manual will guide you through the installation of the optimization software Gurobi, using Python (in version 3.11) as an interface. We will write our optimization models in Python code and then pass the model to Gurobi to solve it. The manual also explains how to install Gurobi and afterwards multiple Solutions to install Python. With this software you can, for instance, solve linear and integer linear programs, which you know from the lecture. We recommend to use an IDE of your choice.
A detailed manual and additional examples can be found in the quickstart guides for the corresponding operating systems:
In order to use gurobi, a license is required. As a student, the license can be obtained for free here (you need a "Named-User Academic" license). When creating an account, select "Academic" and specify your RWTH mail address. Make sure that the mail address is spelled correctly.
Here, the current Gurobi version for the corresponding operating system can be obtained. Finally, Gurobi should be installed.
Select the license, copy
(as shown below) the grbgetkey
-command, and
execute it in the console.
Under Windows, open the start menu and search for cmd.exe
in the search field.
Under Mac or Linux, open a terminal.
The output should look like the following:
$ grbgetkey 6b085692-feba-11e8-83af-02e454ff9c50
info : grbgetkey version 8.1.0, build v8.1.0rc1
info : Contacting Gurobi key server...
info : Key for license ID 288374 was successfully retrieved
info : License expires at the end of the day on 2019-12-13
info : Saving license key...
In which directory would you like to store the Gurobi license key file?
[hit Enter to store it in /home/fibis]:
info : License 288374 written to file /home/fibis/gurobi.lic
Now you can download here Python for your operation system. Please make sure that the downloaded version is exactly 3.11. Now install Python via the downloaded package. Select "Add Python 3.X to PATH" during the installation.
python
but
python3
.
You can test this by typing python3 -V
into your terminal. If this is the case, please use
python3
instead of python
everywhere in the guide.
To test if the installation was successful, you can run the command python -V
in your
terminal. This will return the installed Python version. If your operating system
didn't recognize the python
command, you have to reinstall Python and make sure that the
option "Add Python 3.X to PATH" is checked.
Now you have to install the Gurobi-Python package.
To do this, open a terminal and execute python -m pip install gurobipy
.
python -m pip install gurobipy-stubs
We recommend you to edit the code and solve the tasks with an IDE, as this makes many things easier. We will guide you through the installation and configuration of Visual Studio Code. You can also use a development environment of your choice.
Download here Visual
Studio code for your
operating system and follow the installation instructions. Also download the file
diet.py.
Open Visual Studio Code and open the diet.py file. Visual Studio Code will directly suggest you to
install the Python extension. Confirm this.
After the installation is complete, you must select the correct Python interpreter. Follow this guide. to achieve this. If you have chosen the interpreter, you can start.
At the top right you will find the play symbol . Press it to execute your code. The Terminal will open and execute the code. You will directly see the output or any errors. The output should look like this:
/usr/bin/python3 "diet.py
Academic license - for non-commercial use only
Optimize a model with 3 rows, 4 columns and 12 nonzeros
Coefficient statistics:
Matrix range [3e-01, 6e+02]
Objective range [7e-02, 8e-01]
Bounds range [0e+00, 0e+00]
RHS range [2e+01, 2e+03]
Presolve time: 0.00s
Presolved: 3 rows, 4 columns, 12 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 0.0000000e+00 6.250000e+01 0.000000e+00 0s
3 4.1055830e+00 0.000000e+00 0.000000e+00 0s
Solved in 3 iterations and 0.00 seconds
Optimal objective 4.105583027e+00
eat 4.501006178644742 units of apple
eat 0.18808967630373452 units of peanutbutter
eat 0.0 units of milk
eat 8.813922680985751 units of bread
Total cost: 4.1055830274348
If Pylint has been installed, it is possible that a message will appear saying that the Gurobi commands cannot be found. The following steps solve this problem:
--extension-pkg-whitelist=gurobipy
Happy coding!
No such file or directory / "Der Systempfad kann nicht gefunden werden, da er nicht vorhanden
ist."
If you get this message when trying to navigate to the gurobi folder in the terminal (e.g. with
cd /Library/gurobi951/mac64
), then the corresponding folder does not exist. Solutions:
/Library/gurobiXXX
is often not called
/mac64
but rather macos_universal2
. Then the correct command is
cd /Library/gurobi951/macos_universal2
.
cd
command or
install gurobi again but at the default location.python
/python3
python setup.py install
is not working, then try python3 setup.py install
. In
rare cases, commands like python3.9 setup.py install
(with the correct version) might be
necessary....\WindowsApps
),
deinstall this version and install over the official python website.