31

How do I run Linux binaries under Mac OS X?

Googling around I found a couple of emulators but none for running Linux binaries on a Mac. There are quite a few posts about running Mac OS X on Linux and that kind of stuff - but that's the opposite of what I want to do.

Update:

Thanks for all the answers! I am fully aware of MacPorts and Fink or any of the other things; and no, I do not want any of these utilities, and I do not want any of the package managers, I prefer to compile things myself. I also have Parallels and could set up virtual machines and all that jazz...

The only thing I want to do is to find a way to run a binary that I do not have the source code for and has been compiled for Linux, but I do not want to run it under Linux but under Mac OS X. Therefore my question about emulators.

5
  • 2
    If your just looking to run software from the *nix/POSIX world it will probably compile provided it doesn't depend on OS specific libraries. Fink or MacPorts can help with this somewhat.
    – Matt
    Commented Dec 23, 2009 at 17:40
  • I can't think of any software that's available for Linux, but not for Mac (sadly, since I'm a Linux guy). Why are you looking for such a thing?
    – Davide
    Commented Dec 23, 2009 at 18:12
  • Team Fortress 2 dedicated server is an example that is Windows and Linux only :-/
    – user52898
    Commented Oct 10, 2011 at 14:47
  • @David Cloudera Manager Commented Mar 15, 2013 at 7:45
  • Great question! I too, have a piece of enterprise software that runs in Linux, but my development machine is a Mac. Hopefully someone comes up with a solution - emulation layer for Linux binaries on Mac.
    – Hanxue
    Commented Sep 24, 2013 at 7:52

7 Answers 7

7

Well there is a project introducing something like Linux's binfmt_misc to OS X so now what you need is an ELF loader, a dynamic linker that can load both Mach-O and ELF, and some mechanism to translate Linux calls to OS X ones.

Just for inspiration, you can implement the dynamic linker in the fashion that it ignores filename extension - both libfoo.so.1 (as an Linux ELF) and libfoo.1.dylib (as an Mach-O) can be loaded so that OS X versions of system libraries can be reused so that you do not need to write a "hosted on OS X" libc.so and syscalls can be handled by an kext that translates Linux calls to OS X ones in kernel.

Or, in an more elegant way, implement a stripped down Linux kernel as a kext that makes the OS X kernel a dual-purpose. However that will require you to use two sets of libraries. (Binaries do not clash so it is largely okay)

1
  • 2
    Do you have a link for this? Commented Jul 14, 2014 at 14:34
6

Set up a virtual machine (I personally use VMWare Fusion) and then install whatever distro of Linux you desire on the virtual machine.

Or, if you have the source to the Linux program, chances are you can recompile it on a Mac and run it natively. If you install Fink or MacPorts, you can install a lot of open source programs without much trouble.

2
  • I added links to Fink and DarwinPorts because they're usually easier than downloading source and trying to compile it, realizing you need to install something else first, and downloading it and trying to compile it, etc etc etc. Commented Dec 23, 2009 at 17:44
  • Cool. It's actually MacPorts now, by the way. ;)
    – mipadi
    Commented Dec 23, 2009 at 17:46
6

I recently found Noah, which you can use to run Linux binaries on macOS. You can install Noah via homebrew (brew install linux-noah/noah/noah). Then you should be able to do this:

noah linux_binary

In my experience the behavior of the binary matches what I see on my Ubuntu machine.

4
  • Good suggestion, but I got the error fatal error: runtime: cannot reserve arena virtual address space trying to execute a Go program.
    – Fred Qian
    Commented Apr 28, 2018 at 7:56
  • @Chang Qian, that's no good. I just tried something similar with a small Go program and saw the same error. Maybe Noah should be more specifically said to allow you to run Linux C binaries on macOS. C programs were all I had really tried before.
    – userABC123
    Commented Apr 28, 2018 at 15:00
  • 1
    Maybe Docker for Mac is the ultimate solution.
    – Fred Qian
    Commented Aug 18, 2018 at 10:43
  • I get an error "operation not permitted" when using Noah, Although I have given the terminal full Disk Access
    – Bilal Alam
    Commented Feb 19, 2020 at 20:39
4

You might have some luck with running Linux executables under Mac OS X using Qemu's User Space Emulator

1
  • Userspace emulator only emulates machine codes, not syscalls.
    – Aoba K
    Commented Sep 6 at 16:30
2

If you decide to go the virtualization route, consider also VirtualBox.

Also, if you only need UNIX like command line tools, there is the MacPorts project. This is basically how I set up git on my mac: after having installed MacPorts you just have to run the sudo port install git command to install git on your system.

2

noah does not allow the binaries to execute properly for me. Use Docker Desktop for Mac.

Just do: docker pull centos:latest # 73MB CentOS docker image

Make a folder for what is needed to run your binary, and in your Dockerfile:

FROM centos
COPY your_binary /bin/
ENTRYPOINT ["your_binary"]

and you can build it with

docker build -t image_name

then execute with

docker run image_name as if it were the binary itself. Worked for me. Hope it helps someone else. And if you need specific outputs or to store files somewhere you can mount volumes onto the docker with -v, for example:

docker run -v path_to_my_stuff:/docker_stuff image_name,

though adding a WORKDIR /docker_stuff line to the Dockerfile before ENTRYPOINT is probably best.

If you change ENTRYPOINT to

ENTRYPOINT ["bash", "-c"] and add

CMD ["your_binary"]

underneath it, you can actually pass the command into the image like

docker run -v path_on_local:/in_container_path image_name "your_binary some_parameters -optionrequiringzerowhitespacebeforeinputvalue"
0

Also you can try Actually Portable Executable in Linux-like chroot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.