Build xmrig on Linux

Hi all,

So, I am getting into the crypto-mining space a bit and I wanted to share a few things I've learned. This whole experience has been teaching me a lot as of late!
In this post, we'll be talking about xmrig what it is and how to compile it on Linux. I'm going to skip the wallet and other details as I wanted this post to be as context-specific and straight to the point as possible. Let's dive right in.


What is xmrig???

My take on this is xmrig is a swiss army knife of a mininig tool that can be used for many algorithms and various coins. I mostly found and used this to mine Monero (XMR), so we'll take that as an example. xmrig supports both CPU and GPU minining, but not all algorithms are CPU- or GPU- friendly. Some are specific to either, so be mindful of what you are doing when you are setting this up.
Let's dive into the build and running instructions and examples.

Building xmrig

I happen to be using debian-based systems to compile stuff. So you'll see some aptitude references in the installation scripts.
I also happen to have this handy script I used to install xmrig on various machines I have without having to think about much. Here's the script for reference: [1].

Script Notes

RUNNING AS ROOT: One thing to note about scripts that I write: I dislike things running as root. There were a lot of posts out there which said the problem was resolved once they ran the command as root. I don't know about you, but I really don't feel comfortable running some untrusted code as root in my system. I like to understand what its doing and give it just enough capabilities to do its job. So, I found out what those capability requirements were and baked them into this script.
As long as you use the resulting mine.sh script (or concepts from it), you should have a little more confidence that this does not require escalated privileges.

CUSTOMIZATION: Be sure to customize this with your preferences. For example, the mining pool of your choice, along with your wallet address. I noted these below in the script.

SUDO: No need to run the build script as root since it will sudo its way to privilege when necessary. Just make sure the user who runs the script has ability to run sudo commands.
However, you do need to run the resulting mine.sh script as root since it will set some options and drop privileges before executing your miner.

SETCAP: This is one of those low-level operations I discovered that normally requires root privileges. However, if there's one thing I have learned about Linux: If you have the right capabilities, you don't require root for anything (except to grant said permission, even then, you can dedicate a user to just that action). If you know what you are doing, you can avoid the root user for just about anything. One of the greatest strengths of Linux I love so much. I think this is why people just flatly said "just run it as root" and left it at that. With this capability set on the binary, it tells the system that it's okay for this particular process to perform this action without escalated privileges.

Building/Execution

It should be as simple as executing the script. However, if months or years later you are coming across this blog post and the version has since changed, I left an easy add to that via an environment variable. So you can use:

    
xmrig_version=6.12.0 ./xmrig-build-all.sh
    
  

In order to compile this.
After the script executes and the code is built, I install the results to /usr/local/share/xmrig/xmrig-$xmrig_version and name the file accordingly. If you want CUDA support, run the script with that variable set to something non-empty as such:

    
WITH_CUDA=1 xmrig_version=6.12.0 ./xmrig-build-all.sh
    
  

Donations

Donations are greatly appreciated if you found this to be helpful!

  • BTC: 3KpGAGUFEbdpXZvLtPRRdzpVATwEHVCkRJ
  • ETH: 0xBfF52D9A244A1C333573f903737ec5eDb5a99150
  • DOGE: D89MZHmmFewdyksCs8EnXwj8XFn7AXrNsj
  • XMR: 49NE1L9WVJLcg7susCGSTAcbT5Fi8Ukq1JthDSjWkLdM6VrnvtZEdm7Vh36gFmdfR2hqKQ1pUKXT71BY9rKsUW3GJUL3Cbr
  • RVN: RMvGF8qpY3XjrGMuzfAMkwveehmqgZrhcF
I plan on posting several new pages about what is crypto and getting into these miners as I complete my research.
Thanks for taking the time to read through this!

[1]: xmrig-build.sh

    
#!/bin/bash -e

# This is the script we will be using to mine later.
# Stored as a string for us to write to a script file later in this installation script.
XMR_MINE_SCRIPT=$'#!/bin/bash

# While root, relax the memory limits and set permissions on the MSR stuff
#  for the CPU since it seems to revert at each reboot.
test "$UID" -eq 0 && {
  ulimit -l 10240
  modprobe msr
  chgrp msr /dev/cpu/*/msr
  chmod g+rw /dev/cpu/*/msr
}

RUNAS_USER=xmr
RUNAS_GROUP=apps
test "$UID" -eq `id -u $RUNAS_USER` || {
  echo "Dropping privileges..."
  exec sudo -H -u$RUNAS_USER -g$RUNAS_GROUP \
    MINER_DEBUG=${MINER_DEBUG} \
    MINER_POOL=${MINER_POOL} \
    MINER_WALLET=${MINER_WALLET} \
    MINER_NAME=${MINER_NAME} \
    MINER_HOME=${MINER_HOME} "$0" $@
}

test -z "$MINER_DEBUG" || {
  set -x
}

MINER_POOL=${MINER_POOL:-"my-favorite-mining-pool.org:3333"}
MINER_WALLET=${MINER_WALLET:-"45n1_MY_WALLET_ADDRESS_0x23943"}
MINER_NAME=${MINER_NAME:-`hostname -s`}

cd

exec xmrig --threads=`nproc` \
  -o "${MINER_POOL}" \
  -u "${MINER_WALLET}" \
  -p "${MINER_NAME}" \
  -k --tls --coin monero $@
'

# Setup some variables and build the xmrig software.
xmrig_version=${xmrig_version:-6.12.0}
xmrig=~/git/cryptocurrency/xmrig

cuda_version=${cuda_version:-$xmrig_version}
cuda=~/git/cryptocurrency/xmrig-cuda

# If you use RHEL/Arch/Gentoo/Other, replace this with your pkg-mgr and names of the respective packages.
sudo apt-get install -y git build-essential cmake libuv1-dev libssl-dev libhwloc-dev msr-tools

# https://stackoverflow.com/questions/18661976/reading-dev-cpu-msr-from-userspace-operation-not-permitted
# Found out why I get the MSR error. This is the fix.
modprobe msr || true
grep msr /etc/group || sudo groupadd --system msr
sudo chmod g+rw /dev/cpu/*/msr
sudo chgrp msr /dev/cpu/*/msr

test -d $xmrig || git clone --depth 1 --branch v$xmrig_version git@github.com:xmrig/xmrig $xmrig
test -d $xmrig/build || mkdir $xmrig/build

test -z "$WITH_CUDA" || {
  test -d $cuda  || git clone --depth 1 --branch v$cuda_version git@github.com:xmrig/xmrig-cuda $cuda
  test -d $cuda/build  || mkdir $cuda/build
}

test -d /usr/local/share/xmrig || sudo install -d -oroot -gstaff -m2755 /usr/local/share/xmrig

(
  cd $xmrig/build
  cmake ..
  make -j`nproc`
  sudo install -v -oroot -gstaff ./xmrig /usr/local/share/xmrig/xmrig-$xmrig_version
  # This sets a capability on the binary itself in order to have raw IO operations against the CPU for MSR to work.
  sudo setcap cap_sys_rawio=ep /usr/local/share/xmrig/xmrig-$xmrig_version
)

test -z "$WITH_CUDA" || {(
  cd $cuda/build
  # If you have issues with cuda, add the following to specify it directly:
  #  -DCUDA_LIB=/usr/local/cuda/targets/x86_64-linux/lib/stubs/libcuda.so -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda
  if [ `gcc -dumpversion` -ge 10 ]; then
    # xmrig-cuda does not appear to support gcc-10 just yet. If this changes,
    # remove this if(){} block and the subsequent variable from `cmake'.
    CMAKE_OPTS='-DCMAKE_C_COMPILER=gcc-9 -DCMAKE_CXX_COMPILER=g++-9'
  fi
  # Change CUDA_ARCH to what works for your installation: https://developer.nvidia.com/cuda-gpus
  cmake .. -DCUDA_ARCH=61 $CMAKE_OPTS
  make -j`nproc`
  sudo install -v -oroot -gstaff ./libxmrig-cuda.so /usr/local/share/xmrig/
  slaves="--slave /usr/lib/libxmrig-cuda.so libxmrig-cuda.so /usr/local/share/xmrig/libxmrig-cuda.so"
)}

# Make the software live and available to the rest of the system thru update-alternatives.
sudo update-alternatives --install /usr/local/bin/xmrig xmrig /usr/local/share/xmrig/xmrig-$xmrig_version 100 $slaves

# Enable hugepage support.
sudo sysctl -w vm.nr_hugepages=1280

# setup the non-privileged account that will enable us to mine!
getent group crypto >/dev/null || groupadd --system crypto
group_gid=`getent group crypto | cut -d: -f3`
id xmr || sudo useradd --gid=$group_gid --shell /usr/sbin/nologin --system --home /var/lib/crypto/xmr -m -G msr xmr
test -d ~xmr/bin || sudo install -d -oxmr -gapps ~xmr/bin
test -f ~xmr/bin/mine.sh || echo "$XMR_MINE_SCRIPT" | sudo install -oxmr -gapps /dev/stdin ~xmr/bin/mine.sh

echo "You can now run sudo /var/lib/crypto/xmr/bin/mine.sh"
echo "Complete!"
    
  

Comments

  1. I have noticed that when I get this error at runtime:

    [2021-07-02 06:16:50.976] net new job from mine.myfavoritepool.com:3333 diff 431M algo kawpow height 1823602
    [2021-07-02 06:16:50.976] nvidia use profile kawpow (2 threads) scratchpad 32 KB
    | # | GPU | BUS ID | INTENSITY | THREADS | BLOCKS | BF | BS | MEMORY | NAME
    | 0 | 0 | 0f:00.0 | 1572864 | 32 | 49152 | 0 | 0 | 2968 | NVIDIA GeForce RTX 3070 Ti
    | 1 | 1 | 04:00.0 | 1572864 | 32 | 49152 | 0 | 0 | 2968 | NVIDIA GeForce GTX 1080 Ti
    [2021-07-02 06:16:52.057] nvidia READY threads 2/2 (1082 ms)
    [2021-07-02 06:16:58.625] miner KawPow light cache for epoch 243 calculated (6567ms)
    Program compile log: nvrtc: error: failed to open libnvrtc-builtins.so.11.3.
    Make sure that libnvrtc-builtins.so.11.3 is installed correctly.
    [2021-07-02 06:17:05.389] nvidia KawPow failed to initialize DAG: :197 "NVRTC_ERROR_BUILTIN_OPERATION_FAILURE"


    In this case I had: It pointed to the libxmrig-cuda.so loader I was using.
    I recompiled my loader to be consistent with the system in which it was built.
    Verifying this detail made this error go away in case anyone else experiences the same.

    ReplyDelete

Post a Comment

Popular posts from this blog

Setup and Install Monero(d) -- p2pool -- xmrig

Pulseaudio: Multi-User Setup