Emulating Linux binaries under Mac OS X - linux

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.

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)

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.

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.

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

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.

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"

Related

Run a linux executable on windows

I got an executable file created with linux. I also have the files and libraries that created this exe linux file.
Two questions:
Is there a way to run this linux exe in windows or it has to be done with linux?
The files are from around 15 to 18 years ago. If can't run it from windows, would linux be able to run it even if the files are somewhat old?
Could someone advice? Thanks
You can use WSL available on Windows 10 and 11.
You can also setup a Linux virtual machine using Virtual Box, Hyper-V, VMware Workstation Player, or any other virtualization solution, copy your files to the VM and execute there.
Another option is to install and use Docker. With docker you could run a command like this: docker run -it --rm -v $PATH_TO_EXE_DIRECTORY:/app ubuntu:latest /app/name_of_your_exe from command line.
Will it actually run if it's old? Your mileage may vary. It depends if it's a statically linked executable or dynamically linked executable. Statically linked executable may run, or may complain about incompatible kernel version (I've seen it once). Dynamically linked executable may fail to run due to missing dependent libraries or incompatible versions.
To check if the file is dynamically linked you can use Linux file command file name_of_your_exe, it will print the information regarding the data in the file, including whether it's dynamically or statically linked. To investigate the dependencies you can use Linux command ldd name_of_your_exe which will print the list of libraries (.so extension per Linux convention) your executable is dependent upon.
Your best option to ensure that it will run is to try and figure out which Linux distribution and version it was intended for and find its VM image or installation media online (it should still be possible, IMO), setup a VM and try to run it there.

Can't execute .run file in Ubuntu container

there.
I'm trying to install the Microchip XC8 compiler on a Ubuntu container to make a pipeline for building the project with Gitlab CI. But there's no response after I run the "xc8-v1.45-full-install-linux-installer.run" file.
Here is the environment I have:
Official Ubuntu 18.04 LTS image on a Docker container
Docker version 19.03.13
Windows 10 as Docker host
Microchip XC8 v1.45 compiler
And the commands I used for downloading and installing are as following:
# Download XC8 from the Microchip official site
wget http://ww1.microchip.com/downloads/en/DeviceDoc/xc8-v1.45-full-install-linux-installer.run
# Change the access permission
chmod +x xc8-v1.45-full-install-linux-installer.run
# Execute the ".run" file
./xc8-v1.45-full-install-linux-installer.run
After I did them all, there's no response. Obviously, something went wrong.
I have tried the installation process above on a native Ubuntu computer, and it just works fine.
Is there any prerequisite I missed? Or there have some ways for me to achieve the same purpose?
Thanks!
I was having this problem on 64 bit Ubuntu 20.04 as well.
I had several problems, could not change execution bit because it was on an NTFS partition and the executable required 32 bit libraries to run.
First I had to move the file from an NTFS partition so that I could set the file to executable. In my case I moved it to my downloads directory and then in that folder executed:
sudo chmod +x ./xc8-v1.42-full-install-linux-installer.run
It still would not run, so I checked its type by executing:
file ./xc8-v1.42-full-install-linux-installer.run
which resulted in the response:
./xc8-v1.42-full-install-linux-installer.run: ELF 32-bit LSB executable, Intel 80386, version 1 (GNU/Linux), statically linked, no section header
Eventually the main solutions was to install 32 bit libraries:
sudo apt-get install lib32z1
Finally I could install install that 32 bit library. Then running this worked:
sudo ./xc8-v1.42-full-install-linux-installer.run
Using existing MPLAB Docker repo
This GitLab.com project exists:
MPLAB X IDE/IPE podman/docker container
This may not help with your .run file problem, but maybe switching to an existing docker container might make it easier for you.
They also work with .run files, so you may find your solution over there as well.
Features:
Has a general-purpose installation of MPLAB X and the toolchain.
X11 forwarding for working in the IDE is supported.
Can use USB from inside the container.
Requires some setup. See readme for installation instructions.
MIT License
Need to test this still myself, but just wanted to share here, might just as well.
Posted in the microchip forums by the creator:
Dockerfile for MPLAB X IDE/IPE and toolchains

How can I access my WSL2 files from my natively installed Ubuntu?

I'm really new to Ubuntu and WSL.
My problem is simple: I want to access from Ubuntu which I have installed in my computer (dual boot alongside with Windows) to my WSL2 filesystem that I have in Windows. I located a file named ext4.vhdx which I suppose is my entire wsl drive, but I'm not really sure, it is in
c:\Users\USER\Appdata\Local\Packages\CanonicalGroupLimited.Ubuntu20.04o...\LocalState\
I'm currently into web development and I want to share that environment within WSL2 and Ubuntu, I noticed that using the linux fs is way faster than windows fs and it works better with things like watchers. So, is it possible?
I'm currently running Windows 10 19041 (2004), Ubuntu LTS 20.04
I've also encountered similar issues when doing this WSL Ubuntu sharing thing, and I've finally found a solution on the internet that works out perfectly.
Reference Link:
https://www.nicholasmelnick.com/2020/07/sharing-your-wsl2-environment-with-linux/
So basically these are the steps,
First of all, the WSL's "ext4.vhdx" file should be accessible inside your Ubuntu system (So you must mount your windows drive inside your linux OS)
Install libguestfs-tools package with APT
And finally just create a folder and guestmount the drive with following commands.
$ sudo mkdir -p /mnt/wsl
$ sudo guestmount -o allow_other \
--add /mnt/c/Users/username/AppData/Local/Packages/CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc/LocalState/ext4.vhdx \
-i /mnt/wsl
And done! Hope this would solve your problem. :)

Cross-compiling in Linux Alpine targeting Ubuntu

I have a virtual machine running Alpine Linux with gcc 8.2.0, and I would like to use it as a sandbox to compile programs to be installed on an Ubuntu machine. What is the procedure for doing so? I guess I will need to perform cross-compilation, but haven't found anything about it.
You can create an Ubuntu chroot with debootstrap, and compile your sources in the chroot. I doubt there is an easy way to cross-build directly from Alpine Linux.
It is also not clear why you would want to do this. If you have packaged your sources for Debian/Ubuntu, you should use a VM for that operating system and one of the native build tools (such as pbuilder or sbuild) to build in clean build environments.

How can I install Git to my server which does not have apt-get?

My host is Bluehost. My server is on Linux.
I have tried to follow the tutorial.
You can quite easily compile it from source, with the usual ./configure && make && sudo make install commands.
See "How to install git". Specifically the Mac OS X section (which applies to Linux also)
If the machine doesn't have apt-get, then chances are it isn't a Debian or Ubuntu machine, which means that using a tutorial designed for Debian or Ubuntu is unlikely to get you very far.
Either use the packaged releases for whatever Linux distribution you are running, or build from source.
Get the source from http://git.or.cz/
Maybe you have the same problem I have that I cannot have an outgoing connection but I can have an incoming connection, that´s why I cannot use apt-get. What I do to move files is just use WinSCP and move the files there and after do whatever I want with them.

Resources