How To Install Swig On MacOS, Linux And Windows

Swig is the abbreviation of Simplified Wrapper and Interface Generator, it can give script language such as python the ability to invoke C and C++ libraries interface method indirectly.

It is implemented by compiling the C/C++ declaration file (.i file) into the C/C++ wrapper source code (.c or.cxx) file. By calling such wrapper interfaces directly, scripting languages can indirectly call C/C++ programming interfaces.

Using swig do not require modification of the wrapped C/C++ source code, and it takes only a few minutes to generate the wrapper source code file that you need.

Before you can use swig, you should make sure it is installed already. If you meet error messages like unable to execute ‘swig’: No such file or directory, this means swig is not installed in your os.

1. Install Swig On MacOS.

MacOS use homebrew to manage software installation, so you need to install homebrew first and then install swig.

  1. Run below command to install homebrew in MacOS.
    ruby -e "$(curl -fsSL" < /dev/null 2> /dev/null
  2. Install swig use homebrew.
    brew install swig
  3. Run swig -version in terminal to verify swig installation.
    $ swig -version
    SWIG Version 3.0.12

2. Install Swig On Linux.

  1. Install g++ if you do not install it already.
    sudo apt-get install g++
  2. Run g++ -version to verify it.
  3. Install pcre.
    sudo apt-get install libpcre3 libpcre3-dev
  4. Go to swig download page to download linux version swig installation package.
  5. Unzip the swig zip source code to a local directory.
    // Change zip file permission.
    chmod 777 swig-3.0.12.tar.gz
    // Unzip the tar file.
    tar -xzvf swig-3.0.12.tar.gz
  6. Specify swig install directory.
    ./configure --prefix=/home/Jerry/library/swigtool
  7. Compile and install.
    sudo make
    sudo make install
  8. Add SWIG_PATH environment variable, also add it in PATH environment variable.
    sudo vim /etc/profile
    export SWIG_PATH=/home/Jerry/library/swigtool/bin
    export PATH=$SWIG_PATH:$PATH
  9. Make SWIG_PATH, PATH environment variable settings take effect.
    source /etc/profile
  10. Verify swig installation.
    $ swig -version
    SWIG Version 3.0.12

3. Install Swig On Windows.

  1. Go to swig-win download page to download swig installer windows version.
  2. Unzip the zip file into a local folder such as C:\\swig-3.0.12.
  3. Add above swig unzip directory path to system environment variable Path values.
  4. Right click Computer in the file explorer, then click Properties menu item in the popup menu list.
  5. Click Advanced System Settings in opened window left panel.
  6. Click Environment Variables button, then double click Path system variable and add above swig unzip folder path value at the variable value end. Remember use ; to separate.
    change windows PATH system environment variable
  7. Open dos window and execute swig -version to verify.

1 thought on “How To Install Swig On MacOS, Linux And Windows”

  1. Hi Jerry,
    Thanks for the article.
    Found a code typo: “Run g++ -version to verify it.” should be –version

    Best regards,

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.