Chenyo's org-static-blog

Posts tagged "linux":

07 Sep 2024

Install Doom Emacs with Lisp native compilation in WSL2

Today I installed Doom Emacs for my husband on his WSL2. Although the entire process was guided by Claude, there were some back-and-forth during the interaction. Therefore I would like to record the full commands I have used here in sequence for any potential reference.

1. Assumptions

This installation guide assumes a fresh installation of WSL2 Ubuntu 22.04 on Windows 11 in 2024 September.

2. Install prerequisite packages

According to the Doom Emacs documentation, the following packages are recommended:

  • Git 2.23+: this is already installed by default.
  • Emacs 29.4 with Lisp native compilation: this is finicky and will be elaborated later.
  • ripgrep 14.0+: the documentation says 11.0+ suffices, but doom doctor still complains the latest version (13.0) installed from apt is not advanced, so we need to install it from its Github released package later.
  • GNU find: also installed already.
  • fd: sudo apt install fd-find suffices.

3. Install Emacs 29.4

3.1. Before building

First, let’s install some build dependencies:

sudo apt update
sudo apt upgrade  # update packages
sudo apt install build-essential libjansson4 libjansson-dev \
    libgnutls28-dev libtree-sitter-dev libsqlite3-dev
sudo apt install texinfo  # to generate documentation

build-essnetial should install necessary tools to build C/C++ programs such as gcc, g++, make, gdb and dpkg. The rest packages install pre-compiled libraries.

Besides these packages, there are two important packages to support List native compilation:

sudo apt install ibgccjit0 libgccjit-11-dev  # 11 is my gcc version

After installing them, make sure to export the path in the current session, otherwise the compiler will not realize it.

export LD_LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/11:$LD_LIBRARY_PATH

The last thing to do is install a bunch of X and GTK-3 development libraries for Emacs GUI, and another bunch of image processing libraries.

sudo apt install libx11-dev libtiff-dev libgtk-3-dev libncurses-dev
sudo apt install libtiff5-dev libgif-dev libjpeg-dev libpng-dev libxpm-dev

Without the above packages, one may encounter the following error when configuring the Emacs build:

You seem to be running X, but no X development libraries were found. You should install the relevant development files for X and for the toolkit you want, such as Gtk+ or Motif. Also make sure you have development files for image handling, i.e. tiff, gif, jpeg, png and xpm.

3.2. Build Emacs 29.4 with native-comp

At this moment, we can start to download Emacs source code:

wget https://ftp.gnu.org/gnu/emacs/emacs-29.4.tar.xz
tar xvf emacs-29.4.tar.xz
cd emacs-29.4

Then we can configure the build (i.e., generate Makefile) with the following command.

./configure --with-native-compilation --with-x-toolkit=gtk3 --without-pop
  • --with-native-compilation: with this flag the Emacs source code is compiled to native machine code to achieve better performance.
    • Otherwise it is compiled to bytecode and then interpreted by Emacs virtual machine during runtime.
  • --with-x-toolkit=gtk3: this is recommended by Claude.
  • --without-pop: if we are not using Emacs as the email client, we don’t need to bother configure the protocol.

If everything goes well, one should see the following line in the output. If not, make sure libgccjit has been installed and exported.

Does Emacs have native lisp compiler? yes

Now we can finally start compiling the Emacs:

make -j$(nproc)

If some error occurs, we may want to start again, to do this:

sudo apt install autoconf automake
rm -f Makefile
./autogen.sh  # regenerate the configuration file
# then rebuild
make -j$(nproc)

Finally, install Emacs globally:

sudo make install

To confirm the Emacs indeed used the native Lisp compiler, one can evaluate inside the vanilla Emacs with M-: (M is Alt in WSL2):

(native-comp-available-p) ;; should return t

Congratulations! You have now installed the latest and fastest Emacs on WSL2.

4. Install ripgrep

As mentioned in ripgrep documentation, for Debian/Ubuntu users, one should install the latest ripgrep 14.0+ with the following commands.

curl -LO https://github.com/BurntSushi/ripgrep/releases/download/14.1.0/ripgrep_14.1.0-1_amd64.deb  # check the latest version on its documentation
sudo dpkg -i ripgrep_14.1.0-1_amd64.deb  # dpkg has been installed before

Instead, if one installs it with apt, a 13.0+ version is installed and running doom doctor later returns the warning:

The installed ripgrep binary was not built with support for PCRE lookaheads.

5. Install Doom Emacs

Installing Doom Emacs is straightforward, but before that, one should first remove the default Emacs configuration folder:

rm -rf ~/.emacs.d

Then, clone and install Doom Emacs, it could take a while.

git clone --depth 1 https://github.com/doomemacs/doomemacs ~/.config/emacs
~/.config/emacs/bin/doom install

Don’t forget to export ~/.config/emacs/bin to PATH:

echo 'export PATH="$HOME/.config/emacs/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Now one can run doom doctor to check any missing dependencies, e.g., shellcheck. One common issue is the Nerd font is not installed by default so that some icons are not properly displayed. To fix that, run M-x nerd-icons-install-font inside the Emacs, then update the font cache with:

fc-cache -fv
# fc-list | grep Nerd  # to verify the font is installed

6. Some issues with running Emacs in WSL2

  • The first thing is I cannot reload the configuration with M-x doom/reload as running this command always gives me the following error message so that I need to restart the Emacs every time the configuration is changed.

    %s sync -B -e /bin/bash: line 1: fg: no job control

  • I really dislike the white border that surrounds any application launched by WSL!
Tags: linux emacs tool
24 Jun 2024

A stupid debugging experience

1. What happened

  • Servers SA and SB have the same docker installation, and the same running container CA and CB.
  • A Go file G can be built on CA, but on CB it reports this error:

    runtime: failed to create new OS thread (have 2 already; errno=11)
    runtime: may need to increase max user processes (ulimit -u)
    fatal error: newosproc

2. What did I do

  1. I compared any related configurations between SA and SB. and between CA and CB, e.g., ulimit -a, /etc/security/limits.conf. They all look the same.
  2. I created a new container CN on SA with the same docker image, CN can compile G.
  3. I looked into the (complex) docker run script for CA/CB and figured out it was due to a resource constraint --pids-limit 100.
    • Increasing this limit to 200 seems resolve the issue, but I had no idea why the Go compiler needed so many resources (perhaps due to package I imported).
  4. Until this point, I realized, since the container did not support the compilation, why not just only transfer the compiled binary!
    • How silly that I didn’t even try this in the beginning!
  5. Since the program imports the net package, and there is a known issue of Alpine image running a Go binary file, I followed the post and disabled CGO on SA, then docker cp the binary to CA, and it worked.

3. Another issue of running RPC in docker

  • The other day, I also spent hours debugging a route unreachable error when I want to send a request from CA to SA.
  • The CA is using the bridge network, so it should talk to SA via SA’s interface docker0 within the subnet 172.17.0.0/16.
  • However, in my case, the docker by default rejects packages from any container as shown in SA’s tcpdump result:

    172.17.0.1->172.17.0.3 ICMP host unreachable- admin prohibited, length 68

  • By checking SA’s iptables, I found this rule:

      -A INPUT -j REJECT --reject-with icmp-host-prohibited
    
    • Strangely, the ping still works with this rule.
  • In the end, I need to append a new rule to make the RPC work.

      iptables -I INPUT 1 -i docker0 -p tcp --dport <port> -s 172.17.0.0/16 -j ACCEPT
    
Tags: docker go linux alpine
24 Jun 2024

Linux use tips

1. i3

1.1. Move specific workspaces between different monitors (ref)

  1. Adjust the monitor relative positions.
  2. Use i3-msg -- move workspace to output right to move the current workspace to the monitor on the right

2. Org

2.1. Format code blocks

  • Use the shortcut < s <TAB> to create a code block.
  • Use C-c ' to enter the code environment to use the language major mode.

3. Emacs

3.1. Prefix argument

  • When the function includes (interactive "P"), it means one can add a prefix argument C-u 1 (or any integer) to trigger some effect.

3.2. Help functions

  • Ctrl-h K get the function name given a shortcut

4. Vim

4.1. Delete a word backwards

  • diw or daw

5. Firefox

5.1. Cache bypass refresh

  • Ctrl+Shift+R
Tags: tool linux i3 arch
Other posts