How to create a quick installer to deploy your apps using Null Soft Scriptable Installer System (NSIS) – It is Small, Free and flexible

By Anoop Madhusudanan

Vote on HN

imageBased on a recent announcement from Microsoft, Visual Studio Installer will be retiring after VS2010. So it seems that in future versions of Visual Studio, Visual Studio installer won’t be available.

Background Information

From ClickOnce and Setup & Deployment projects thread

In Visual Studio 2010, we have partnered with Flexera, makers of InstallShield, to create InstallShield Limited Edition 2010 just for Visual Studio 2010 customers. The InstallShield Limited Edition 2010 offers comparable functionality to the Visual Studio Installer projects. In addition, you can build your deployment projects using Team Foundation Server and MSBuild. For more information, see this post. With InstallShield available, the Visual Studio Installer project types will not be available in future versions of Visual Studio.

So, you are expected to switch to Install Shield for your setup and deployment, if you are presently relying on Visual Studio Installer. In this post, let us explore Nullsoft Scriptable Installer System or NSIS as an alternate, open source option for packaging and deploying your applications.

Nullsoft Scriptable Installer System or NSIS – A Quick Look

If you’ve ever installed WinAmp, NSIS is the installer used by WinAmp. I used Null Soft Installer for the first time way back in 2004, when I was doing some shareware business. I had a product called Smart Editor Professional (Still available as a freeware in sites Tucows) which was developed in VC++ and VB – and I used Nullsoft NSIS for scripting the installation procedure, which included copying files, showing a splash screen, registering ActiveX dlls, providing optional install components, Creating Program menu entries, Associating file types etc. One of the most interesting aspect of NSIS is the small installer footprint, and the easy to understand scripting aspect.

Now, to start with, you need to download and install NSIS from here. Once  you finish the installation, select NSIS from your StartMenu->Programs, to bring up the NSIS menu.

image 

A lot of resources are already listed there, but I’ll provide a quick start. You’ll find two options under the compiler – to (1) Compile NSI scripts, or to (2) Create an installer based on a Zip file. If your requirements are minimum or if you need to create an installer that’ll simply extract files from a Zip file to some where, you can try the second option. Otherwise, you can write a NSIS script (a simple text based script file with extension *.nsi) to configure the installer as you need.

Once you have an NSIS file, you can create the installer by ‘compiling’ it. Click ‘Compile NSI scripts’, to bring up NSI Script compiler.

image

Now, As you can imagine, you need to create an *.NSI script that directs your installer how to package your components, that can be compiled with MakeNSISW. There are tones of examples available in Program Files\NSIS\Examples folder and the User Manual is pretty comprehensive - so I’m not going to actual scripting part here. Basically, an NSIS script file has the following components.

  • Attributes – Defines the basic attributes like Name, UI etc of your installer
  • Pages – Predefined Wizard pages like directory (to allow users to select target folder for installation), components (for allowing users to select components/sections to install), etc
  • Sections – Lets you logically group your installation components, so that users can choose optional items to install. The script inside a Section is executed only if the user select that component.
  • Functions – Functions contains code like Sections, and can be called using the ‘Call’ keyword from with in sections. Also, there are callback functions with special names that the installer will invoke by itself (eg: .onInit);

Creating a simple script and an Installer

With that information, let us create a simple NSI file. Open notepad, and copy and paste the below script. If you want a decent editor for NSIS scripts, Notepad++ already supports NSIS syntax. The below script demonstrates most of the points I described earlier. Basically, it’ll create an installer by packaging all files from your c:\inst folder, and will deploy those files to a target folder specified by your user. Note that in NSIS a ; is for specifying a single line comment.

;--------------------------------
; Installer Attributes
;--------------------------------

; Name of installer
Name "NsisDemo"

; The file to write
OutFile "NsisDemo.exe"

; The default installation directory
InstallDir $PROGRAMFILES\NsisDemo

; Request application privileges for Windows Vista
RequestExecutionLevel user

;--------------------------------
; Pages
;--------------------------------

Page components
Page directory
Page instfiles

;--------------------------------
;Sections
;--------------------------------

; The stuff to install
Section "Stuff" 
 
 ; Set output path to the installation directory.
  SetOutPath $INSTDIR
  
  ;Include files from this location, and copy that to the current
  ;out path
  
  File c:\inst\*.*
  
SectionEnd ; end the section

;--------------------------------
; Functions
;--------------------------------
Function .onInit
  MessageBox MB_YESNO "This will install Xyz. Do you want to continue?" IDYES startnow
    Abort
  startnow:
FunctionEnd

Note that we’ve three ‘Pages’ - components, directory and instfiles specified. Also, we’ve a section “Stuff”, where we use File directive for informing the NSIS compiler which all Files to package (all files in your c:\inst\*.*) - so that those files will be carried by our installer. Also, there is a callback function .onInit, which will be called automatically by installer, to get an assertion from the user – Whether to continue or not - when the installer is initialized .

Now,

  • Save the above script to a *.NSI file.
  • Place few files in the c:\inst folder (Create that folder if it is not there), so that the MakeNSIS can pick files from there to package.
  • Then compile the NSI file either by right clicking and choosing ‘Compile with NSIS’ or by opening it in the MakeNSISW Script compiler (see above).

You’ll see the generated installer exe, NsisDemo.exe, as we specified in the Installer Attributes. Now, if you run the Exe, you’ll see the following screens.

1 - This is triggered by our script in .onInit

image

2 – Once you click Yes, this is the ‘component’ Page. See the section ‘Stuff’ inside that.

image 

3 – Click Next to see the ‘directory’ Page. See that what ever we specified using InstallDir installer attribute has appeared as the default destination folder.

image

Please note that NSIS is customizable and open source. A lot of other UIs and Plugins are also available – so don’t get deceived by the above ‘minimal’ look of the installer.

Also, you should, see this post where I explain How to use NSIS to create an offline installer for a Silverlight application. Happy Coding!!

© 2012. All Rights Reserved. Amazedsaint.com