Skip to main content

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

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

Popular posts from this blog

Top 7 Coding Standards & Guideline Documents For C#/.NET Developers

Some time back, I collated a list of 7 Must Read, Free EBooks for .NET Developers, and a lot of people found it useful. So, I thought about putting together a list of Coding Standard guidelines/checklists for .NET /C# developers as well.As you may already know, it is easy to come up with a document - the key is in implementing these standards in your organization, through methods like internal trainings, Peer Reviews, Check in policies, Automated code review tools etc. You can have a look at FxCop and/or StyleCop for automating the review process to some extent, and can customize the rules based on your requirements.Anyway, here is a list of some good Coding Standard Documents. They are useful not just from a review perspective - going through these documents can definitely help you and me to iron out few hidden glitches we might have in the programming portion of our brain. So, here we go, the listing is not in any specific order.1 – IDesign C# Coding StandardsIDesign C# coding stand…

Hack Raspberry Pi – How To Build Apps In C#, WinForms and ASP.NET Using Mono In Pi

Recently I was doing a bit of R&D related to finding a viable, low cost platform for client nodes. Obviously, I came across Raspberry Pi, and found the same extremely interesting. Now, the missing piece of the puzzle was how to get going using C# and .NET in the Pi. C# is a great language, and there are a lot of C# developers out there in the wild who are interested in the Pi.In this article, I’ll just document my findings so far, and will explain how develop using C# leveraging Mono in a Raspberry Pi. Also, we’ll see how to write few minimal Windows Forms & ASP.NET applications in the Pie as well.Step 1: What is Raspberry Pi?Raspberry Pi is an ARM/Linux box for just ~ $30. It was introduced with a vision to teach basic computer science in schools. How ever, it got a lot of attention from hackers all around the world, as it is an awesome low cost platform to hack and experiment cool ideas as Pi is almost a full fledged computer.  More About R-Pi From Wikipedia.The Raspberry Pi

5 Awesome Learning Resources For Programmers (To help you and your kids to grow the geek neurons)

Happy New Year, this is my first post in 2012. I’ll be sharing few awesome learning resources I’ve bookmarked, and will be pointing out some specific computer/programming related courses I've found interesting from these resources.Also, thought about saving this blog post for my kids as well - instead of investing in these Child education schemes (though they are too small as of today, 2 years and 60 days respectively ). Anyway, personally my new year resolution is to see as much videos from this course collections (assuming I can find some free time in between my regular job && changing my babies diapers).1 – Khan AcademyAs I mentioned some time back, you and your kids are missing some thing huge if you havn’t heard about Khan Academy.  It is an awesome learning resource, especially if you want to re-visit your basics in Math, Science etc.With a library of over 2,600 videos covering everything from arithmetic to physics, finance, and history and 268 practice exercises, th…