Post tutorial Report RSS How-To: Creating a installer with NSIS

This tutorial will explain you how to make a installer with the Nullsoft Scriptable Installer System (or NSIS)

Posted by on - Basic Installers

Hello and welcome to my tutorial on NSIS :)
I will explain you how to create and compile *.NSI scripts for use with NSIS.

First we will need NSIS
You can from THIS page, just click on download.
You might allso need a IDE to make things easier.
I first used Notepad++ for it but the syntax higlighting for NSIS scripts is kinda wunky so i will use HM NIS Edit.
You can download that HERE

After both the installers are completed you may run HM NIS Edit (HMNE from now on)
First thing you should do is go to View>Options or press F11
Under enviroment, click on the big "Register as default editor for NSIS files" button to make things easier :)

So lets create a basic script

Every programmers first program is the famous hello world program and this will be no exception.
so read the following script (pay special attention to the comments ^^)
Its not so hard so i wont explain alot :p
PS: the script is not case sensitive :)

#Set the installer filename
outfile "Hello World.exe"

#Create a default section
section
       #create a popup box with the text "hello world" and a ok button
       messagebox MB_OK "Hello world!"
#closes the section
sectionend

Save the file, compile it and run it and youl see how simple it is :D

Now lets make something more...interesting :)
I wont comment the stuff we allready saw this time :) (allso applies to the next pieces of code :))

outfile "hello world.exe"

section
# This will open up a file called nsisscripttest.txt on your desktop
# This file does not have to exist
# the "w" at the end indicates writing mode
# the "$0" indicates a variable, this will be the short internal name for the script
  fileOpen $0 "$DESKTOP\nsisscripttest.txt" w

  # this will write the string hello world to the textfile
  # we use $0 to indicate the file (read above)
  fileWrite $0 "hello world!"

  # close the file
  fileClose $0

sectionEnd

So now you know the basics, lets continue by making stuff that actualy installs.
When i said when we would create a installer that actualy installs then i didnt mean a good one XD
First simple again.
I will skip a part tough and go directly to a installer with a uninstaller included since we dont create install only stuff :)

outFile "installer.exe"

# this will set the default installation dir to the desktop
# $DESKTOP points to the desktop of the current user, regardless of user
installDir $DESKTOP

section
  # sets the installation path
  # $INSTDIR is defined by the installdir command, so you only should use $INSTDIR
  setOutPath $INSTDIR

  # This will be the file that will be included in the installer, this file has to exist :)
  file test.txt

  # With this command we will define the uninstaller
  writeUninstaller $INSTDIR\uninstaller.exe

sectionEnd

# This will define the uninstall section, everything that has to be uninstalled will be here :)
# This section is allways name uninstalled, there are no excep
section "Uninstall"

  # Always delete uninstaller first
  delete $INSTDIR\uninstaller.exe

  # now we delete installed file
  delete $INSTDIR\test.txt

sectionEnd

Pretty self explanitory :)
pay good attention to the following tough:

  • uninstaller section is ALLWAYS named uninstall
  • ALLWAYS delete the uninstaller first

Up next, start menu shortcuts :o

outFile "installer.exe"
installDir $DESKTOP

section
    setOutPath $INSTDIR
    writeUninstaller "$INSTDIR\uninstall.exe"
    # create a shortcut named "new shortcut" in the start menu programs directory (1st set of "")
    # point the new shortcut at the program uninstaller (2nd set of "")
    createShortCut "$SMPROGRAMS\new shortcut.lnk" "$INSTDIR\uninstall.exe"
sectionEnd

section "uninstall"
    delete "$INSTDIR\uninstall.exe"
    delete "$SMPROGRAMS\new shortcut.lnk"
sectionEnd

So that are the basics
This is only good for silent installers (installs to default location)
If you wish to specify a other directory then just use the following

installDir "C:\whereveryouwantdir"

In the next tutorial i will go more into custom installations using pages like choose directory, choose files, etc...

I have just explained the basics of NSIS
Hope you learned something from it or founded it usefull
Now i all say you goodnight :)

Post comment Comments
JustDaveIsFine
JustDaveIsFine - - 1,545 comments

This will prove very useful.

Reply Good karma Bad karma+1 vote
Guest
Guest - - 690,465 comments

This comment is currently awaiting admin approval, join now to view.

Guest
Guest - - 690,465 comments

This comment is currently awaiting admin approval, join now to view.

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: