Setting up Gurobi with C++ in the Linux environment

  1. Install Windows Subsystem for Linux and install necessary packages.
  • WSL1 is good enough. No need to update to WSL2. Tutorial: https://docs.microsoft.com/en-us/windows/wsl/install-win10
  • Open the Microsoft Store app, and install Ubuntu.
  • Launch Ubuntu and it will be installed. You will need to set a username and password for your Ubuntu Linux system.
  • Open Ubuntu and type in the following command:
sudo apt-get update
sudo apt-get upgrade
sudo apt install build-essential
  1. Install Gurobi.
  1. Test run the code:
  • Get into the folder where you stored the C++ code using the following command:
cd “the path you stored the files”
  • Use the following command to compile the C++ file (in this case, the C++ filename is CppCode.cpp and I would like the compiled file to be named as BinaryOutputFilename . I installed Gurobi at C:/Linux/gurobi902. You might need to adjust the paths in the command depending on where you installed Gurobi):
g++ -m64 -g -o BinaryOutputFilename CppCode.cpp -I/mnt/c/Linux/gurobi902/linux64/include -L/mnt/c/Linux/gurobi902/linux64/lib -lgurobi_g++5.2 -lgurobi90 -lm
  • Run the code using the following command:
./BinaryOutputFilename

In Step 2, we mentioned that we need to set up a few environment variables after installing Gurobi. We could add a few lines to ~/.bashrc file so that we don’t need to run the environment variable setup commands each time we open bash.

You could open the file with the nano editor using the following command:

sudo nano ~/.bashrc

and then add the following 4 lines to the end of the file:

export GUROBI_HOME="/mnt/c/Linux/gurobi902/linux64"
export PATH="${PATH}:${GUROBI_HOME}/bin"
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${GUROBI_HOME}/lib"
export GRB_LICENSE_FILE="/mnt/c/Linux/gurobi902/gurobi.lic"

Please note the paths are where I installed Gurobi and where I saved the license for Gurobi. You might need to change it accordingly.

If you want to start writing your code now, please see this topic: C++ Gurobi programming for Beginners

The following are steps to set up a Linux Environment in MacOS Catalina.

  1. Install the latest version of Virtual Box for MacOS X with the following link; Downloads – Oracle VM VirtualBox
    • If an error is received that blocks the installation follow this tutorial:
    Allowing Third Party Applications to Install on a MacBook | IT Services
  2. Install the desktop version of Ubuntu with the following link;
    Download Ubuntu Desktop | Download | Ubuntu
  3. Once both Virtual Box and Ubuntu are installed, open Virtual Box and create a new Virtual Machine with the “new” button.
    • The recommended RAM allocated for the Virtual Machine is 4GB, but it can be changed at any time.
    • The recommended hard drive space for the Virtual Machine is 25GB, keep in mind the hard drive space cannot be changed.
  4. After Virtual Machine is created, select the Virtual Machine created and click on “start”. A window will pop out to and select the Ubuntu installed from the beginning.
  5. Ubuntu will start.
    • The Ubuntu installation will begin.
    • Select all default options
    • Installation will wrap up and Ubuntu will restart.
  6. To set up Gurobi in your new Virtual Machine follow step 2, from the original forum.
1 Like