Skip to content

Instantly share code, notes, and snippets.

@Vindaar
Last active February 15, 2021 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vindaar/6cb4e93baff3e1ab88a7ab7ed1ae5686 to your computer and use it in GitHub Desktop.
Save Vindaar/6cb4e93baff3e1ab88a7ab7ed1ae5686 to your computer and use it in GitHub Desktop.
Notes on getting =ggplotnim= to work on Windows

Getting ggplotnim to work on Windows

I’ll note down my steps to get ggplotnim to work on Windows starting from a default Windows 10 installation without any development tools.

For me the first steps to get set up my development environment is to:

  1. install git
  2. install emacs and clone my settings
  3. and then set up additional tools, in this case nim and ggplotnim.

Installing git

Since I don’t want to use tools like msys2 or similar, I decided to install plain git from git’s website:

https://git-scm.com/download/win

Just run the installer and allow it to add git to the Windows PATH, so that we can use it from PowerShell.

After installation we can verify that it was added to PATH manually:

  • Windows key
  • path
  • “edit system environment variables”
  • “Environment Variables” in the bottom right corner
  • check “System variables” (bottom window)
  • click on “Path”

and there should be a new line, by default:

C:\Program Files\Git\cmd

Launching PowerShell we can verify that it indeed works.

Install emacs

Again I don’t use any third party software to install emacs, but go straight to the emacs page of GNU:

https://www.gnu.org/software/emacs/download.html

and download the current version from the linked FTP:

https://mirrors.kernel.org/gnu/emacs/windows/emacs-26/emacs-26.3-x86_64.zip

(download the signature and check it!)

Once installed, we have to add emacs to the Windows PATH manually (which is why I explained above where the PATH can be changed).

Instead of adding to the system PATH, I only added to my user’s PATH (top window in the “Environment Variables” window from above). Just add a new field for the PATH variable with the path to where you extracted the emacs archive.

Customize emacs

With git and emacs working, it’s time for me to download my configuration. Launch a PowerShell:

cd ~/Documents/config/ 
git clone https://github.com/vindaar/emacs.d

Once that is done we have to copy the contents to the location where emacs expects the files.

To avoid having to figure out where the emacs configuration is supposed to be on Windows, I just launch emacs and then open an eshell (M-x eshell).

In eshell:

cd ~
pwd
# should point to c:/Users/<UserName>/AppData/Roaming
cp -r c:/Users/<UserName>/Documents/config/emacs.d/* .emacs.d/

Note that you can’t use ~/Documents here, since c:/users/<UserName> is not considered the home directory by emacs!

Once done, close and relaunch emacs (you could just load the new config, but well). Installation should only take a minute.

To the fun part, install Nim

Our next step should be to install Nim.

We just download the current release manually from Nim’s website:

https://nim-lang.org/install_windows.html

(Manual installation)

Download and put somewhere you want Nim to live, in my case:

<a href=”c:/Users//Documents/src/nim-1.0.6/”>c:/Users/<UserName>/Documents/src/nim-1.0.6/

And as Araq always says ( ;) ), don’t forget to run that finish.exe in the root of the Nim directory. It will add Nim and Nimble to the PATH and also install mingw64.

Once the installation of mingw64 is done, launch another PowerShell to see if Nim is found:

nim --version
Nim Compiler Version 1.0.6 [Windows: amd64]
Compiled at 2020-01-23
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: 89b39ee8fe271d0e1b75c15a4c6cf82eb9c13aea
active boot switches: -d:release

should work.

Install ggplotnim

Now we use Nimble to install ggplotnim and its dependencies. In PowerShell:

nimble install ggplotnim

However, in order to properly test the package and since I obviously work / test on ggplotnim, I’ll actually clone the git repository and link that repository to Nimble via nimble develop:

cd ~/Documents/src/
git clone https://github.com/vindaar/ggplotnim
cd ggplotnim
nimble develop

Once done, let’s see what happens when we compile and run one of the recipes:

cd ggplotnim
nim c -r recipes/rSimpleLinePlot.nim

And, uhm, I’m rather surprised to see that it both compiled and ran successfully.

I was actually expecting to see something along the lines of:

could not load libcairo-2.dll

or something.

Weird.

Let’s try a different example in which we save as a PDF. Create the following file in the playground directory in the ggplotnim directory:

import ggplotnim

let df = toDf(readCsv("data/mpg.csv"))
ggplot(df, aes("hwy", "cyl")) + 
  geom_point() + 
  ggsave("test.pdf")

Also works.

I figured it out…

The “issue” (read: solution) is that by installing emacs and adding its bin directory to my PATH, I provide required dependencies.

Since emacs on Windows is built on top of GTK (cairo is a GTK dependency) I installed cairo properly without realizing it.

So, to figure out the depencies that are actually required for users who don’t use emacs (which is certainly the majority of people who want to use ggplotnim on Windows, I imagine), let’s go over how to install it properly.

Before I do this, I remove the emacs bin directory from my PATH. Once done, I do indeed get the following error running the code above:

Hint: C:\Users\basti\Documents\src\ggplotnim\playground\testPdf.exe  [Exec]
could not load: libcairo-2.dll
Error: execution of an external program failed: 'C:\Users\basti\Documents\src\ggplotnim\playground\testPdf.exe '

Installing cairo

According to the cairo website, the recommended way of installing it on Windows is to get it as a side effect of installing GTK+, since cairo is a dependency.

https://www.cairographics.org/download/

The important part however is the following:

You probably want at least the zlib, cairo, and libpng run-time archives (you can search on those strings to find them in the page). That should be it. Just pop libcairo-2.dll, libpng13.dll and zlib1.dll into your working directory or system PATH, and away you go!

which tells us which libraries we will actually need.

Nim already ships:

  • zlip1.dll
  • libpng3.dll
  • libpng12.dll=

So for zlib we should be good. Maybe we need a different libpng version though.

Unfortunately, the GTK+ website is offline right now, but people on stack exchange say the prebuilt GTK version for Windows is outdated anyways.

Maybe we should simply use msys2 to install gtk+ after all…

So do that, but we only use it to get access to the required DLLs. We will copy those to some sane place, which we add to PATH.

https://www.msys2.org/

Once installed, run

pacman -Syu

and then I’m explicitly told to kill the window (wtf?) and do it again. Once the second round is done, install gtk:

pacman -S mingw-w64-x86_64-gtk3

Given the installation size of 500 MB it’s probably faster and easier to just download emacs for Windows even if you don’t use it sigh.

Once that is done, take a look into:

cd C:\msys64\mingw64\bin

it will contain the libcairo-2.dll.

Originally I wanted to just extract the DLLs that are required, but well. Let’s say it’s more than the cairo website makes us believe. libpng and libcairo alone are not sufficient. So instead let’s add the above directory to Windows’ PATH after all.

Once that is done the ggplotnim samples should compile just fine, i.e. there is no need to copy any DLLs next to the binaries.

@pietroppeter
Copy link

Hi, I was able to find the complete list of dll needed for libcairo, see details in this gist: https://gist.github.com/pietroppeter/80266c634b22b3861273089dab3e1af2

I also added a link to a google drive public rar file containing all dlls, which I report also here for convenience:
https://drive.google.com/file/d/1MCGtVHoCEzIuqzma8jgmgr2bzYgxfeqW/view?usp=sharing

Using the above the simplified instruction to have ggplotnim work on windows can be: download the rar and extract it in ~\.nimble\bin

@Vindaar
Copy link
Author

Vindaar commented Feb 11, 2021

Ah, thanks for doing this! That's really useful.

For the Github Actions CI I've now switched over to using this standalone libcairo shared library here:
https://github.com/preshing/cairo-windows/

See the build instructions used in the CI here:
https://github.com/Vindaar/ggplotnim/blob/master/.github/workflows/ci.yml#L61-L64

I'll add a reference to your gist as well as the above to the readme!

@pietroppeter
Copy link

Ah well, the approach with the single cairo.dll seems actually better than the one I outline above, I will try it! I agree that a note added to the Readme would be helpful.

@Vindaar
Copy link
Author

Vindaar commented Feb 14, 2021

I've just updated the README:

https://github.com/Vindaar/ggplotnim/#windows

Thanks again!

@pietroppeter
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment