I Have a Program in C Sharp, How to Compile It as an Executable for Linux Server So That I Can Run It on the Server Without Installing Mono

I have a program in C sharp, can I compile it as an executable for Linux server so that I can run it on the server without installing mono?

You need to use mkbundle with the --static option if your target system does not have mono installed.

Bundles in addition support a --static flag. The --static flag causes mkbundle to generate a static executable that statically links the Mono runtime. Be advised that this option will trigger the LGPL requirement that you still distribute the independent pieces to your user so he can manually upgrade his Mono runtime if he chooses to do so

Problems while creating a Linux-executable file from C# sourcecode using mkbundle

mkbundle from 64-bit Mono generates 64-bit executables and you cannot run a 64-bit executable on 32-bit kernel. As mkbundle doesn't have an option to generate 32-bit binaries you apparently need to generate your executable with 32-bit Mono (theoretically you can install 32-bit Mono on your 64-bit system but that is not very easy so I suggest installing a 32-bit system somewhere, maybe in VM).

Creating executable file for Linux using Mono?

1: The first question on the Mono FAQ:

Can Mono run binaries produced by Visual Studio?

Yes, Mono can run binaries produced by Visual Studio, there is no need to recompile.

Use our Mono Migration Analysis tool to check if everything that your application uses has been implemented in Mono or if there are special considerations to keep in mind.

The Mono API today is somewhere in between .NET 2.0 and .NET 4.0 see our Roadmap for details about what is implemented.

2: I can't speak specifically for PHP, but I believe you'll have to run the process using the mono executable (e.g. mono /path/to/exe/SomeTool.exe)

How to compile a .NET source file into executable using PHP?

I'm fairly certain no one has written a C# compiler in PHP, so you're going to have to call a compiler via a command line call from your php code.

On Linux, you'll be using the Mono compiler: http://mono-project.com/CSharp_Compiler

Something along these lines (dependent upon the location of toolbar.cs, your required references, and .NET version to target):

exec("gmcs -pkg:dotnet toolbar.cs");

Cannot execute binary file after xbuild compilation on server

Most Linux distribution does not ship with Mono, so you have to,

  1. Install Mono according to the distribution manuals. Sounds like you are using a Debian derivative, then you should run sudo apt-get install mono-complete.
  2. Run your binary by calling mono your.exe.

How to convert a simple .Net console project a into portable exe with Mono and mkbundle?

I have found a simple how-to here, however, as I have not tested it myself, I cannot guarantee results. As usual YMMV.

Quote from the original article (please follow the thread on the original article as well though):

Mkbundle: bundle Mono with your applications

Did you ever wonder why you need .NET Framework or Mono installed to run your program? Well, it would be much more handy if you could distribute your applications without nagging your clients to install additional frameworks, is it not? So here we are. Lets bundle a .NET-based application with Mono, so you don't need Mono, or .NET installed to run it.

Prepare an environment

First you need to install newest Mono and Cygwin. Installing Mono is very straightforward so you cannot screw up anything. When you start installing Cygwin, go into Full view, then please include 4 additional packages. These are: gcc, mingw, mingw-zlib and zlib.

Now you need a command prompt. Both Mono and Cygwin create shortcuts for command prompts on your desktop, but you need to combine them into one. Here is a batch that does it for me. You may need to change it, if you have other Mono version for example.

Code:

echo Mono version 2.4 Build 6
echo Prepending 'C:\PROGRA~1\Mono-2.4\bin' to PATH
PATH=C:\PROGRA~1\Mono-2.4\bin;%PATH%

chdir C:\cygwin\bin
bash --login -i

Bundle an application with Mono

So we are now in a command prompt, running this Cygwin mode. Notice that this is not a DOS prompt anymore, and "dir" won't work anymore. To list files use linux command "ls". The folder you are browsing now is like the one below. Arek is a username.
Code:

C:\cygwin\home\Arek

Browse to this folder with your explorer. Now you copy 2 files into this folder. 1st is your application exe and 2nd is the file Mono.dll (2MB) that you can find in your Mono folder.
Code:

C:\Program Files\Mono-2.4\bin

For some reason the whole procedure does not work with long file names, so rename your application exe. It should comply with this old DOS 8.3 naming.

Lets go back to command prompt. You need only 1 command to bundle your application, and here is some explanation.

mkbundle is a program within Mono package | -o Bundled1.exe specifies how the Mono-bundled exe will be named | Winform1.exe says what will be included, Mono libraries will be included anyway | --deps is necessary although I am not sure what it does | -z will compress the output exe a lot

Code:

mkbundle -o Bundled1.exe Winform1.exe --deps -z

So now you got your Bundled1.exe, which contains your own app along with Mono itself. You should not need Mono nor .NET to run it. Notice that it will be 4MB or more in size. Those bundled exes are not lightweight.

How do I build and deploy a simple mono C# web api to an ubuntu server?

If you want to do this properly you might want to look into deployment tools such as Capistrano or Chef.

First, you'll want to create your Mono app as a console application. Linux can just run your executable, there isn't an easy web host environment like IIS for Mono apps (yet). This of course means your app could exit on an exception, so ensure you have adequate defensive coding to avoid that happening.

Basically, to move the files you can use SCP, just scp -R <directory> <username>@<server>:/<path_to_copy_to>. Once you've got the files there you then need to start the app, and I'm guessing you want to run it as a service, not just on-demand. In order to do this you'll need to create an Upstart script. You'll want to create a job for starting, stopping and maybe restarting your app.

If you add it to the right runlevels it will automatically start on boot and stop on shutdown.

Mono C#: mkbundle'd Hello World script executes with only the exit code 11 on a web server

I created a virtual machine with the same OS as the Web Server, when running the bundle I got 'Segmentation fault (core dumped)', which has the exit code '11'. mkbundle is not a fully compatible way to deploy Mono written software on Linux machines.

So in the end I installed Mono on the virtual machine, compiled and mkbundle'd the code, and after uploading the file to the Web Server it was able to execute it.



Related Topics



Leave a reply



Submit