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?

The Oxford Dictionary tells us the following “A piece of software that installs a program on a computer’s hard disk”.

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

An installer has extra options and takes care of the integration in the OS.

There are many install frameworks on the market, some open source and some proprietary. The most commonly know installers are Windows Installer, InstallShield and NSIS (Nullsoft scriptable install system). We are going to use NSIS, simply because it is relatively easy to use and open source.

Installer example

Creating a basic installer

NSIS is a scriptable installer, so we need to create a script file. NSIS uses the .nsi file extension.

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.

To declare variables we need to do the following.

!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"

Now you need to use the previous variables to fill in some basic program information like the name, installer name and branding text.

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

The installer has of course a user interface to interact with. NSIS has a few years ago released a new installer interface called modern UI 2. We need to include the configuration for this interface in the installer.

!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"

We will now see the most important part of the installer, the sections. Sections are viewed as component in the componet page of the installer. We can also make section groups to create subsections.

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

To remove the software, we need to create an uninstallation section for the previously mentioned uninstaller.

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store