TroublingFox125

Sign in

We want that our newly created program is easily delivered to the client and is easy to install. We need to provide an installer.

Photo by Patrick Lindenberg on Unsplash

What is an installer?

An installer has much in common with a compressed file, it contains the program components and drops it somewhere in the desired directory.

What is the difference?

Which install framework to use?

Installer example

Creating a basic installer

We are going to use Visual Studio Code because it has the necessary plugins for NSIS syntax highlighting and debugging tools.

code basic.nsi

We now have an empty file, lets start by entering some details of our application in the form of variables.

Variables

!define <VARIABLE_NAME> <”VALUE”>

We want to declare the product name, version and publisher.

!define PRODUCT_NAME "SimpleServe"
!define PRODUCT_VERSION "1.0"
!define PUBLISHER "Covert Barnacle Developments"

UI and basic info

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "${PRODUCT_NAME}-installer.exe"
BrandingText "${PUBLISHER}"

We also want to use compression on our installer to make it smaller, and we want the installer to be Unicode, so we add the following.

Unicode True
SetCompressor lzma

The program needs to be installed in the program files folder. Depending on your program it will be in the 32bit folder (C:\Program Files (x86)) or in the 64bit folder (C:\Program Files). We also want to register the program in the register and request admin privileges to install it.

InstallDir "$PROGRAMFILES64\${PRODUCT_NAME}"
InstallDirRegKey HKCU "Software\${PRODUCT_NAME}" ""
RequestExecutionLevel admin

Interface

!include "MUI2.nsh"

To apply different installer pages we can use macros, these are page snippets that can be used to add the desired pages to the installer. Some macros can have variables.

Most of the installer contain the following pages.

!insertmacro MUI_PAGE_LICENSE "${NSISDIR}\Docs\Modern UI\License.txt"
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES

The last thing we need to do is set the language. We will use English, other languages are available. We can also make a multi-language installer.

!insertmacro MUI_LANGUAGE "English"

Installation sections

In our simple example were going to create an installer for a simple Python based file server. We create a section Webserver.

We can make sections required for installation by adding SectionIn RO.

A registry key is also added to let Windows know the program is installed.

We are going to set the following parameters:

  • SetOutPath → Place where the program needs to be installed.
  • File → File that needs to be copied to SetOutPath.
  • WriteUninstaller → Creating an uninstaller for the program, more about this later.

Shortcuts are also necessary to easily open the program, we are going to add one for the python script and the uninstaller.

Section "Webserver" Sec_Simpleserve
SectionIn RO
WriteRegStr HKCU "Software\${PRODUCT_NAME}" "" $INSTDIR
SetOutPath "$INSTDIR"
File ".\simpleserve.py"
WriteUninstaller $INSTDIR\uninstall.exe
CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME}.lnk" "python" '"$INSTDIR\simpleserve.py"'
CreateShortCut "$SMPROGRAMS\${PRODUCT_NAME} Uninstaller.lnk" "$INSTDIR\uninstall.exe"
SectionEnd

Now the description is added for the section.

LangString DESC_Sec_Simpleserve ${LANG_ENGLISH} "A test section."!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${Sec_Simpleserve} $(DESC_Sec_Simpleserve)
!insertmacro MUI_FUNCTION_DESCRIPTION_END

Uninstallation sections

We need to do the opposite of the installer, notice that un is in the frond of this section.

Section "Un.Webserver" un.Sec_Simpleserve
RMDir /r "$INSTDIR"
Delete "$SMPROGRAMS\${PRODUCT_NAME}.lnk"
Delete "$SMPROGRAMS\${PRODUCT_NAME} Uninstaller.lnk"
RMDir "$INSTDIR"
DeleteRegKey /ifempty HKCU "Software\${PRODUCT_NAME}"
Delete "$INSTDIR\Uninstall.exe"
SectionEnd